Maven - Deploy Artifacts to Nexus3

We will learn how to deploy a artifact to Nexus3 server using maven.

Start nexus Server

You can install Nexus in your machine or use docker-compose. For convenience, we will use docker-compose in this demo.

First create a data directory and create docker-compose.yaml file

1
2
mkdir data
touch docker-compose.yaml

docker-compose.yaml

1
2
3
4
5
6
7
8
9
10
11
version: "2"
services:
nexus:
image: sonatype/nexus3
volumes:
- "./data:/nexus-data"
ports:
- "8081:8081"

volumes:
nexus-data: {}

Then start Nexus server using command

1
docker-compose up -d

You can check the status of nexus server using docker-compose ps command. Status will be ‘up’ when the server is ready.

1
2
3
4
$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------
nexus_nexus_1 sh -c ${SONATYPE_DIR}/star ... Up 0.0.0.0:8081->8081/tcp

Now broser to http://localhost:8081, You should be able to view the Nexus page. From this page, you should be able to get the repository urls. The repository url for maven snapshots should be http://localhost:8081/repository/maven-snapshots/. The repository url for maven releases should be
http://localhost:8081/repository/maven-releases/.

An alternative way is to run nexus3 docker image from command line. However, you need to use docker exec to go into the docker contains to grab the password in nexus-data/admin.password file.

1
docker run -d -p 8081:8081 --name nexus sonatype/nexus3

Maven Configuration

Then we need to set the maven configuration. It can be change at the global level or user level. The user level configuration file is located at ~/.m2/settings.xml. You will need to create the file if it doesn’t exist yet. This configuration file should contain credentials to connect to the nexus server. The default user is ‘admin’ and password is store admin.password under the data folder.

~/.m2/settings.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>snapshots</id>
<username>admin</username>
<password>4f26a39c-e723-4356-acf6-0ca9a02dced5</password>
</server>
<server>
<id>releases</id>
<username>admin</username>
<password>4f26a39c-e723-4356-acf6-0ca9a02dced5</password>
</server>
</servers>
</settings>

Project Deployment

The maven projects needs to specify the repository where the artifacts will be deployed. The repository information is provided in the distributionManagement section of the pom file. If you don’t specify distributionManagement section, the deployment will fail.

pom.xml

1
2
3
4
5
6
7
8
9
10
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
<repository>
<id>releases</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>

By default, Maven handles the deployment mechanism via the maven-deploy-plugin – this mapped to the deployment phase of the default Maven lifecycle.

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>

Then you can deploy the artifact by executing command

1
mvn clean deploy

If the deployment is successful, the artifact will show up in nexus’s maven repository.

Reference