Operations 9 min read

How to Set Up Jenkins CI with Nexus Repository Manager for Maven Artifact Management

This guide walks you through installing Jenkins and Nexus via Docker, configuring a Maven hosted repository in Nexus, adding necessary Jenkins plugins and credentials, and creating a Jenkins pipeline that builds a Java project and publishes its artifacts to Nexus, enabling automated CI/CD for your applications.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
How to Set Up Jenkins CI with Nexus Repository Manager for Maven Artifact Management

In this tutorial we use Jenkins as the continuous integration server and Nexus Repository Manager as the artifact repository.

The goal is to create a workflow that builds, stores, manages, and monitors compiled artifacts using Maven and a CI server.

First, ensure Jenkins is running. If not, start it with Docker:

docker run -d --name jenkins-ci -p 8080:8080 jenkins/jenkins:lts

Access Jenkins at http://your-ip-addr:8080 and retrieve the initial admin password with:

docker exec -i jenkins-ci cat /var/jenkins_home/secrets/initialAdminPassword

Securely store the username and password for later use.

Install Nexus Repository Manager

Nexus is a repository manager that stores and retrieves artifacts. Pull the Nexus Docker image:

$ docker pull sonatype/nexus3

Run Nexus on the default port 8081:

$ docker run -d --name nexus_repo -p 8081:8081 sonatype/nexus3

Wait a couple of minutes for the service to start, then monitor the logs:

$ docker logs nexus_repo -f

When the log shows Started Sonatype Nexus OSS 3.20.1-01 , open http://your-ip-addr:8081 in a browser. The default login is admin ; obtain the password with:

docker exec -i nexus_repo cat /nexus-data/admin.password

Create a Maven Hosted Repository in Nexus

Create a repository named maven-nexus-repo of type maven2 , enable the “Allow redeploy” option, and configure a user with the nx-admin role.

Install and Configure Nexus Plugin in Jenkins

In Jenkins, go to Manage Jenkins → Manage Plugins → Available and install the Nexus Artifact Uploader plugin. Then add Nexus credentials under Credentials → System → Global credentials .

Configure Maven as a global tool in Jenkins ( Manage Jenkins → Global Tool Configuration → Maven ).

Create a Jenkins Pipeline

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 { git 'https://github.com/javaee/cargotracker.git' }
        }
        stage("Maven Build") {
            steps { 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 environment variables define the Nexus version, protocol, URL, repository name, and credential ID used by the uploader step.

After saving the pipeline, trigger a build. The first build may take some time. Upon success, Jenkins console output shows the uploaded artifact details, and the Nexus UI displays the new artifact in the repository.

This automated pipeline provides a systematic way to distribute project artifacts, reducing rebuild time and simplifying CI tool migration. Nexus can also be backed by cloud storage such as AWS S3 or Google Cloud Storage for additional flexibility.

Dockerci/cdDevOpsmavenJenkinsartifact repositoryNexus
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.