Repository Management with Nexus

13.3. Nexus M2Settings Maven Plugin

Once you have defined a set of Maven templates, you can use the Nexus M2Settings Maven Plugin to distribute changes to the settings file to the entire organization.

13.3.1. Running the Nexus M2Settings Maven Plugin

To invoke a goal of the Nexus M2Settings Maven Plugin, you will initially have to use a fully qualified groupId and artifactId in addition to the goal. An example invocation of the download goal is:

mvn org.sonatype.plugins:nexus-m2settings-maven-plugin:download

In order to be able to use an invocation with the simple plugin prefix like this

mvn nexus-m2settings:download

you have to have the appropriate plugin group org.sonatype.plugins configured in your Maven Settings file:

<settings>
  ...
  <pluginGroups>
    <pluginGroup>org.sonatype.plugins</pluginGroup>
  </pluginGroups>
  ...

An initial invocation of the download goal will update your settings file with a template from Nexus Professional. The default template in Nexus Professional adds the org.sonatype.plugins group to the pluginGroups, so you will not have to do this manually. It is essential that you make sure that any new, custom templates also include this plugin group definition. Otherwise, there is a chance that a developer could update his or her Maven Settings and lose the ability to use the Nexus Maven plugin with the short identifier.

[Tip]

This practice of adding pluginGroups to the settings file is useful for your own Maven plugins or other plugins that do not use the default values of org.apache.maven.plugins or org.codehaus.mojo as well, since it allows the short prefix of a plugin to be used for an invocation outside a Maven project using the plugin.

The download goal of the Nexus M2Settings Maven Plugin downloads a Maven Settings file from Nexus Professional and stores it locally. The default file name for the settings file is the Maven default for the current user of ~/.m2/settings.xml file. If you are replacing a Maven Settings file, this goal can be configured to make a backup of an existing Maven Settings file.

[Note]

The download with the Nexus Maven Plugin is deprecated and has been replaced with the Nexus M2Settings Maven Plugin.

13.3.2. Configuring Nexus M2Settings Maven Plugin

The download goal of the Nexus M2Settings Maven plugin prompts the user for all required parameters, which include the Nexus server URL, the username and password, and the template identifier.

[Note]

For security reasons, the settings download requires an HTTPS connection to your Nexus instance. If you are running Nexus via plain HTTP you will have to set the secure parameter to false.

The required configuration parameters can either be supplied as invocation parameters or when prompted by the plugin and are:

nexusUrl
Points to the Nexus server installation’s base URL. If you have installed Nexus on your local machine, this would be http://localhost:8081/nexus/. Access via HTTP only works with the secure configuration parameter set to false.
username
The username to use for authenticating to Nexus. Default value is the the Java System property user.name.
password
The password to use for authenticating to Nexus.f
templateId
The Template ID for the settings template as defined in the Nexus user interface.

Additional general configuration parameters are related to the security of the transfer and the output file:

secure
By default set to true, this parameter forces a Nexus URL access with HTTPS. Overriding this parameter and setting it to false allows you to download a settings file via HTTP. When using this override it is important to keep in mind that the username and password transfered via HTTP can be intercepted.
outputFile
Defines the filename and location of the downloaded file and defaults to the standard ~/.m2/settings.xml.
backup
If true and there is a pre-existing settings.xml file in the way of this download, back up the file to a date-stamped filename, where the specific format of the datestamp is given by the backupTimestampFormat parameter. Default value is true.
backup.timestampFormat
When backing up an existing settings.xml file, use this date format in conjunction with SimpleDateFormat to construct a new filename of the form: settings.xml-$(format). Date stamps are used for backup copies of the settings.xml to avoid overwriting previously backed up settings files. This protects against the case where the download goal is used multiple times with incorrect settings, where using a single static backup file name would destroy the original, preexisting settings. Default value is: yyyyMMddHHmmss.
encoding
Use this optional parameter to define a non-default encoding for the settings file.

As a Maven plugin, the Nexus M2Settings Maven Plugin relies on Apache Maven execution and on the fact that the Central Repository can be contacted for downloading the required plugins and dependencies. If this access is only available via a proxy server you can configure the proxy related parameters proxy, proxy.protocol, proxy.host, proxy.port, proxy.username and proxy.password.

13.3.3. Downloading Maven Settings

You can download the Maven Settings from Nexus Professional with a simple invocation, and rely on the plugin to prompt you for the required parameters:

$ mvn org.sonatype.plugins:nexus-m2settings-maven-plugin:download
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] -----------------------------------------
[INFO]
[INFO] --- nexus-m2settings-maven-plugin:1.6.2:download (default-cli) @ standalone-pom ---
Nexus URL: https://localhost:8081/nexus
Username [manfred]: admin
Password: ********
[INFO] Connecting to: https://localhost:8081/nexus (as admin)
[WARNING] Insecure protocol: https://localhost:8081/nexus/
[INFO] Connected: Sonatype Nexus Professional 2.4.0-07
Available Templates:
   0) default
   1) example
Select Template: 0
[INFO] Fetching content for templateId: default
[INFO] Backing up: /Users/manfred/.m2/settings.xml to: /Users/manfred/.m2/settings.xml-20130404120146
[INFO] Saving content to: /Users/manfred/.m2/settings.xml
[INFO] -----------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------
[INFO] Total time: 29.169s
[INFO] Finished at: Thu Apr 04 12:01:46 PDT 2013
[INFO] Final Memory: 12M/153M
[INFO] -----------------------------------------

If your Nexus server is hosted internally and does not use HTTPS you can download a settings file with

$ mvn org.sonatype.plugins:nexus-m2settings-maven-plugin:download -Dsecure=false

As displayed, the plugin will query for all parameters and display a list of the available templates. Alternatively, you can specify the username, password, Nexus URL, and template identifier on the command line.

$ mvn org.sonatype.plugins:nexus-m2settings-maven-plugin:download \
-DnexusUrl=https://localhost:8443/nexus \
-Dusername=admin \
-Dpassword=admin123 \
-DtemplateId=default

Enabling proxy access with -Dproxy=true will trigger the plugin to query the necessary configuration:

[INFO] Connecting to: https://localhost:8443/nexus (as admin)
Proxy Protocol:
   0) http
   1) https
Choose: 1
Proxy Host: myproxy.example.com
Proxy Port: 9000
Proxy Authentication:
   0) yes
   1) no
Choose: 0
Proxy Username [manfred]: proxy
Proxy Password: ******
[INFO] Proxy enabled: proxy@https:myproxy.example.com:9000

In some scenarios you have to get an initial settings file installed on a computer that does not have internet access and, therefore, cannot use the Maven plugin. For this first initial configuration that connects the computer to Nexus for following Maven invocations, a simple HTTP GET command to retrieve an unmodified settings file can be used:

curl -u admin:admin123 -X GET "http://localhost:8081/nexus/service/local/templates/settings/default/content" > ~/.m2/settings.xml

Modify the commandline above by changing the username:password supplied after -u and adapting the url to Template URL visible in the Nexus user interface. This invocation will however not replace parameters on the client side, so you will have to manually change any username or password configuration, if applicable.