Setting Up Jenkins CI with Nexus Repository Manager and Maven Pipeline
This guide walks through installing Jenkins and Nexus Repository Manager in Docker, configuring a Maven hosted repository, adding credentials, and creating a Jenkins pipeline that builds a Java project and publishes artifacts to Nexus, illustrating a complete DevOps CI/CD workflow.
This tutorial explains how to use Jenkins as a continuous integration server together with Nexus Repository Manager to build, store, manage, and monitor compiled artifacts using Maven.
First, ensure Jenkins is running in a Docker container (e.g., docker run -d --name jenkins-ci -p 8080:8080 jenkins/jenkins:lts ) and retrieve the initial admin password with docker exec -i jenkins-ci cat /var/jenkins_home/secrets/initialAdminPassword .
Next, pull and start the Nexus image ( docker pull sonatype/nexus3 and docker run -d --name nexus_repo -p 8081:8081 sonatype/nexus3 ), monitor startup logs ( docker logs nexus_repo -f ) until the message Started Sonatype Nexus OSS 3.20.1-01 appears, and obtain the admin password via docker exec -i nexus_repo cat /nexus-data/admin.password .
In Nexus, create a Maven hosted repository named maven-nexus-repo , enable the “allow redeploy” policy, and create a user with the nx-admin role.
Back in Jenkins, install the “Nexus Artifact Uploader” plugin, add the Nexus credentials, and configure Maven under Global Tool Configuration.
Then define a Jenkins pipeline (see code below) that sets environment variables for Nexus connection, clones a Git repository, builds the project with Maven, and uploads the resulting artifact to Nexus using nexusArtifactUploader :
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" }
} }
}
}
}The summary of environment variables explains that NEXUS_VERSION selects the Nexus version (e.g., nexus3 ), NEXUS_PROTOCOL is typically HTTP (HTTPS in production), NEXUS_URL is the host and port, and NEXUS_CREDENTIAL_ID references the Jenkins‑stored Nexus user.
After triggering the pipeline, the build logs show successful artifact upload, and the artifact appears in the Nexus repository UI, demonstrating a centralized, automated artifact management solution that can be extended to cloud storage back‑ends.
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.