Setting Up Jenkins CI with Nexus Repository for Maven Artifact Management
This guide walks through installing Jenkins and Nexus via Docker, configuring a Maven hosted repository in Nexus, setting up Jenkins credentials and plugins, and creating a Jenkins pipeline that builds a Maven project and publishes its artifacts to Nexus, enabling automated CI/CD for Java applications.
This tutorial demonstrates how to use Jenkins as a continuous integration server together with Nexus Repository Manager as a Maven artifact storage solution.
Install Jenkins : Run the Jenkins container with Docker using docker run -d --name jenkins-ci -p 8080:8080 jenkins/jenkins:lts , then open http://your-ip-addr:8080 in a browser, retrieve the initial admin password with docker exec -i jenkins-ci cat /var/jenkins_home/secrets/initialAdminPassword , and complete the setup.
Install Nexus Repository : Pull the Nexus image with docker pull sonatype/nexus3 and start it on port 8081 using docker run -d --name nexus_repo -p 8081:8081 sonatype/nexus3 . Verify startup by checking the logs docker logs nexus_repo -f for the message Started Sonatype Nexus OSS 3.20.1-01 , then access http://your-ip-addr:8081 and log in with the default admin user (password obtained via docker exec -i nexus_repo cat /nexus-data/admin.password ).
Create a Maven Hosted Repository in Nexus : In the Nexus UI, create a new hosted repository named maven-nexus-repo , select the Maven2 format, enable the "Allow redeploy" policy, and configure appropriate storage settings.
Configure Jenkins : Install the "Nexus Artifact Uploader" plugin, add Nexus credentials under Credentials → System → Global credentials , and configure Maven under Manage Jenkins → Global Tool Configuration . Optionally, place the Maven binary inside the Jenkins container.
Create a Jenkins Pipeline : Use the following declarative pipeline script (code kept intact): pipeline { agent { label "master" } tools { maven "Maven" } environment { NEXUS_VERSION = "nexus3" NEXUS_PROTOCOL = "http" NEXUS_URL = "your-ip-addr:8081" NEXUS_REPOSITORY = "maven-nexus-repo" NEXUS_CREDENTIAL_ID = "nexus-user-credentials" } stages { stage("Clone code from VCS") { steps { script { git 'https://github.com/javaee/cargotracker.git' } } } stage("Maven Build") { steps { script { sh "mvn package -DskipTests=true" } } } stage("Publish to Nexus Repository Manager") { steps { script { pom = readMavenPom file: "pom.xml" filesByGlob = findFiles(glob: "target/*.${pom.packaging}") artifactPath = filesByGlob[0].path if (fileExists(artifactPath)) { nexusArtifactUploader( nexusVersion: NEXUS_VERSION, protocol: NEXUS_PROTOCOL, nexusUrl: NEXUS_URL, groupId: pom.groupId, version: pom.version, repository: NEXUS_REPOSITORY, credentialsId: NEXUS_CREDENTIAL_ID, artifacts: [ [artifactId: pom.artifactId, classifier: "", file: artifactPath, type: pom.packaging], [artifactId: pom.artifactId, classifier: "", file: "pom.xml", type: "pom"] ] ) } else { error "*** File: ${artifactPath}, could not be found" } } } } } }
Run the pipeline, and upon successful build you will see the artifact uploaded in the Nexus UI and the build logs in Jenkins confirming the deployment.
Using this Jenkins‑Nexus integration provides a reliable, automated way to manage and distribute Maven artifacts, reducing rebuild time and simplifying CI/CD workflows for Java projects.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.