Today I’ll talk about how to deploy artifacts to Sonatype Nexus repository using Maven deploy plugin.
Prerequisite
Prepare Nexus server in local:
- Download Nexus Repository OSS
- Unzip the downloaded file
-
Start the server
$ bin/nexus start
- Visit http://localhost:8081/
- Sign in with username
admin
and passwordadmin123
Create a Maven project for demo propose:
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:
Use the plugin for deployment:
Configure Nexus
Define the ID and URL of your Nexus repository in the project’s parent
pom.xml
:
Add username and password in Maven global settings (~/.m2/settings.xml
):
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:
More detail when you click the page: