Sonatype Introduces Next Generation Dependency Management | Press Release

blog-logo Sonatype Blog

How to Publish Java Artifacts to Nexus Using Jenkins and Maven

February 07, 2020 By Daniel Hernández

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.
    • 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 Nexus repository. This is a chance to review how to use Jenkins Rest API operations and Script operations in Nexus.
    • How does it works?
  • Jenkins already have defined these items defined 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 Nexus repository
    • 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:

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:

Download the code

https://github.com/danielalejandrohc/jenkins-examples/tree/master/nexus

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 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:

    • 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 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.

null

Tags: Nexus Repository, Maven, jenkins, News and Views, Product, Post developers/devops

Written by Daniel Hernández

Daniel Hernández is a Software Engineer working in CI/CD solutions and Java development.