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.
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:
In order to be able to use an invocation with the simple plugin prefix like this
you have to have the appropriate
org.sonatype.plugins configured in your Maven Settings
<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
includes 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.
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
well, since it allows the short prefix of a plugin to be used for an
invocation outside a Maven project using the plugin.
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.
The download with the Nexus Maven Plugin is deprecated and has been replaced with the 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.
For security reason the settings download requires a HTTPS
connection to your Nexus instance. If you are running Nexus via plain
HTTP you will have to set the
secure parameter to
The required configuration parameters can either be supplied as invocation parameters or when prompted by the plugin and are:
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
secureconfiguration parameter set to
the username to use for authenticating to Nexus. Default
value is the the Java System property
- password to use for authenticating to Nexus
- 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:
by default set to
true, this parameter forces a Nexus URL access with HTTPS. Overriding this parameter and setting it to
falseallows 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.
- defines the filename and location of the downloaded file and defaults to the standard ~/.m2/settings.xml
- If true and there is a pre-existing settings.xml file in the way of this download, backup the file to a date-stamped filename, where the specific format of the date-stamp is given by the backupTimestampFormat parameter. Default value is true.
- 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, pre-existing settings. Default value is: yyyyMMddHHmmss.
- 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 therefore 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
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.4.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:8081/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:8081/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 can therefore not 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 do the client side replacement of parameters, so you will have to manually change any username or password configuration, if applicable.