Jenkins 2, Maven 3 and Nexus 3.

2019-11-07 jenkins nexus docker jcasc

In this article, we are going to explore how you can publish your java artifacts (.ear, .jar, .war) to Nexus 3 using Jenkins and Maven.

For this, I have created a docker compose file which comes with Nexus and Jenkins. Let’s take into considerations these assumptions and details about how the example works:

  • You have docker and docker-compose installed properly.
  • Jenkins will create the maven 2 (hosted) repository in Nexus on the startup script.
    • How does it work?
      • 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 of creating the Nexus repository. This is a chance to review how to use Jenkins Rest API operations and Script operations in Nexus.
  • Jenkins already has defined these items defined in the configuration as code file:
    • 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
    • Two jobs defined using DSL:
      • 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 Nexus repository

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 creating the Nexus repository

Jenkins has a capability which allow you to run Groovy code whenever Jenkins is started. This what it does is to create a script (Do 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:

Download the code

Optional: How to manually create 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 Nexus is below. Please, take into consideration for newer versions, you need to extract the credentials from the container. That’s why I picked this version because the default login works straight, and we can make this article straight to the point. Username: admin password: admin123