How to Publish Java Artifacts to Nexus Using Jenkins and Maven

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 (Read more...)

*** This is a Security Bloggers Network syndicated blog from Sonatype Blog authored by Daniel Hernández. Read the original post at: