Maven: Deploy Artifacts to Nexus

Declare Maven deploy plugin in the parent POM. It's the same no matter your project is a single module project or a multi-modules project. Then, define the Nexus repository id and url in distributi...

Today I’ll talk about how to deploy artifacts to Sonatype Nexus repository using Maven deploy plugin.

Prerequisite

Prepare Nexus server in local:

  1. Download Nexus Repository OSS
  2. Unzip the downloaded file
  3. Start the server

    $ bin/nexus start
    
  4. Visit http://localhost:8081/
  5. Sign in with username admin and password admin123

Create a Maven project for demo propose:

$ mvn archetype:generate \
  -DgroupId=com.mycompany \
  -DartifactId=demo \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DinteractiveMode=false

Declare Maven Deploy Plugin

Declare Maven deploy plugin in the parent POM. It’s the same no matter your project is a single module project or a multi-modules project. The parent pom.xml file is located in project’s root directory.

Define the version of Maven deploy plugin:

<!-- file: /Users/mincong/demo/pom.xml -->
<project>
  ...
  <build>
    ...
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Use the plugin for deployment:

<!-- file: /Users/mincong/demo/pom.xml -->
<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Configure Nexus

Define the ID and URL of your Nexus repository in the project’s parent pom.xml:

<!-- file: /Users/mincong/demo/pom.xml -->
<project>
  ...
  <distributionManagement>
    <snapshotRepository>
      <id>nexus-snapshots</id>
      <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
  </distributionManagement>
</project>

Add username and password in Maven global settings (~/.m2/settings.xml):

<!-- file: /Users/mincong/.m2/settings.xml -->
<settings>
  <servers>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>
</settings>

IMPORTANT: You should NOT keep your password in plain text in settings.xml. See Maven official page Password Encryption for the encryption guideline.

Build and Deploy Artifacts

There’re 2 possible solutions: one-step deploy or multi-steps deploy.

One-step deploy runs tests, installation, and deploy in a single command:

$ mvn clean deploy

Multi-steps deploy runs different commands in different steps. Firstly, run install command (which implies comile, test, and install). Once done sucessfully, deploy the results:

$ mvn clean install
$ mvn deploy -DskipTests -Dmaven.install.skip=true

Here’s the comparison of these two solutions:

Item One-Step Deploy Multi-Steps Deploy
Maven commands 1 commands ≥ 2 commands
If no test failures, then… All artifacts deployed All artifacts deployed
If test failures, then… Some artifacts deployed ⚠️ No artifacts deployed

Check Deployed Artifacts

Now, go to http://localhost:8081 and check the uploaded results. You can see that the project demo is available:

Nexus search page demo

More detail when you click the page:

Nexus detail page demo

References