How to Publish Java Artifacts to Sonatype Nexus Repository
How to Publish Java Artifacts to Sonatype Nexus Repository Using Jenkins and Maven
February 07, 2020
By Daniel Hernández
In this article, we will explore how to publish your Java artifacts (.ear, .jar, .war) to Sonatype Nexus Repository 3 using Jenkins and Maven.
For this, I have created a docker compose file, which comes with Sonatype Nexus Repository and Jenkins. Let's consider these assumptions and details about how the example works:
- You have
dockeranddocker-composeinstalled properly. - Jenkins will create the
maven 2 (hosted)repository in Sonatype Nexus Repository on the startup script.- Any .groovy file you placed under
/var/jenkins_home/init.groovy.d/*.groovywill be automatically executed. So you will find a job that is executed which is in charge to create the repository. This is a chance to review how to use Jenkins Rest API operations and Script operations in Sonatype Nexus Repository.
- Any .groovy file you placed under
- Jenkins already have defined these items in the configuration as code file:
- nexus-push: Jenkins pipeline example which will
buildthe Java artifact and push it to Jenkins - nexus-create-repo: Jenkins pipeline which will run every time Jenkins is started and will try to create the repository
- Credential
nexus-credentialsto login to use the rest API and the Nexus Jenkins plugin to push artifacts - Maven tool to use it in the Pipeline, I called it
Maven 3.6.0
- nexus-push: Jenkins pipeline example which will
Let's Start
Clone the project:
<script src="https://gist.github.com/danielalejandrohc/43ab14c4ea036f64dd846933039e11d1.js"></script>
Start docker-compose application:
<script src="https://gist.github.com/danielalejandrohc/20103a4a6b318820fcf8aea8e8a2b446.js"></script>
And that's it! You are ready to explore Jenkins in port 8080 and run the job but let's take a minute to review what just happened.
What Happened in Jenkins?
For Jenkins in this example I already prepared everything for you, so let's review what are the changes about and what you might consider If you want to implement it in your environment.
The Dockerfile I am using looks like this:
<script src="https://gist.github.com/danielalejandrohc/fd03c7d1383347422cbd61dfa5fc1616.js"></script>
The configuration as code yaml is below. This will create the credential, jobs required, and configured the maven tool. You can find where those details are in the comments of the file.
<script src="https://gist.github.com/danielalejandrohc/2e58648e61b44035520fa60987d6dd2c.js"></script>
Jenkins has a capability which allow you to run Groovy code whenever Jenkins is started. This what it does is to create a script (Did you guess what language? Yes, Groovy!). The script basically build an existing job which is defined in the Jenkins configuration as code file with name: nexus-create-repo.
You can check the code below:
<script src="https://gist.github.com/danielalejandrohc/38016fd0defb435d24b782a971d8543b.js"></script>
Let's Try It Out!
So easy as going here:
http://localhost:8080/job/nexus-push
and build it! Enjoy the logs 😀😀
The code that is executed is below:
<script src="https://gist.github.com/danielalejandrohc/9e3ed4ead556b32b77e831746257e636.js"></script>
Download the code
https://github.com/danielalejandrohc/jenkins-examples/tree/master/nexus
Optional: How to Manually Create Sonatype Nexus Repository
This is only required If instead of pushing the artifacts in a repository other than the one we have used in this article, which is repository-example. So here we detail the steps to create a repository called maven-repo.
Note: default login for admin user in Sonatype Nexus Repository is below. Please take in consideration the newer versions you need to extract the credentials from the container. That's why I picked this version, because the default login works, and we can make this article straight to the point.
Username: admin password: admin123
- Go to http://localhost:8081/#admin/repository/repositories
- Click on
Create repository - Select
maven2 (hosted) - Fill the form with these values:
- Name: maven-repo
- Layout policy: Strict
- Storage: default
- Deployment policy: Allow redeploy
Basically, the only field we have changed from the default value is Deployment policy since is a test environment, you might want to redeploy the same artifact so many times as you want. Maybe that's the way your organization wants it, though I would not recommend this approach.
Written by Daniel Hernández
Daniel Hernández is a Software Engineer working in CI/CD solutions and Java development.