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:
docker and docker-compose installed properly.maven 2 (hosted) repository in Sonatype Nexus Repository on the startup script.
/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.build the Java artifact and push it to Jenkinsnexus-credentials to login to use the rest API and the Nexus Jenkins plugin to push artifactsMaven 3.6.0
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. 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:
http://localhost:8080/job/nexus-push
and build it! Enjoy the logs 😀😀
The code that is executed is below:
Download the code
https://github.com/danielalejandrohc/jenkins-examples/tree/master/nexus
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
[ Editor's Note: The repository manager installation includes an administrative user with full access. Its username is admin and the initial password can be found in an 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:
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.