In this article, we are going to explore how you can 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 take into consideration these assumptions and details about how the example works:
- You have
docker
anddocker-compose
installed 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/*.groovy
will 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
build
the 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
- nexus-push: Jenkins pipeline example which will
- Credential
nexus-credentials
to 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
Let's start
Clone the project:
Start docker-compose application:
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:
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.
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:
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:
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 to 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
admin.password
file in the $data-dir
directory. You can sign in with the button on the top right corner of the user interface.Next steps after successfully accessing the user interface are detailed in User Interface, Configuration and setting up whatever Formats you will use. Read more here.]
-
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 that we have changed from the default value is
Deployment policy
since is a test environment you might want toredeploy
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.