Integrating Jenkins with Nexus and Artifactory for Artifact Upload and Deployment
This guide explains how to deploy Nexus with Docker, define component concepts, configure Maven settings, and use Jenkins pipelines—both script‑based and Nexus plugin—to upload and publish artifacts to Nexus and Artifactory, including API scripts and naming conventions for repository management.
This article provides a step‑by‑step tutorial on integrating Jenkins with artifact repositories Nexus and Artifactory, covering deployment, configuration, and artifact management.
Deploy Nexus with Docker using the following command:
docker run -id \
--privileged=true --name=nexus3 \
-p 8081:8081 \
-v ${LOCALDIR}/nexus3/nexus-data:/nexus-data \
sonatype/nexus3:3.20.1After installation, retrieve the initial admin password from the data directory and change the credentials.
Basic concepts of components and assets are introduced, explaining how a component can be a JAR, WAR, Docker image, or other package type, and how assets such as POM files or Docker layers are associated with components.
Configure Maven to authenticate against the Nexus repository by adding a server entry to settings.xml :
<server>
<id>maven-hostd</id>
<username>admin</username>
<password>admin123</password>
</server>When running mvn deploy , ensure that server.id matches the repository ID.
Jenkins pipeline (scripted) to upload a JAR:
def jarName = sh returnStdout: true, script: "cd target;ls *.jar"
jarName = jarName - "\n"
def pom = readMavenPom file: 'pom.xml'
pomVersion = "${pom.version}"
pomArtifact = "${pom.artifactId}"
pomPackaging = "${pom.packaging}"
pomGroupId = "${pom.groupId}"
println("${pomGroupId}-${pomArtifact}-${pomVersion}-${pomPackaging}")
def mvnHome = tool "M2"
sh """
cd target/
${mvnHome}/bin/mvn deploy:deploy-file -Dmaven.test.skip=true \
-Dfile=${jarName} -DgroupId=${pomGroupId} \
-DartifactId=${pomArtifact} -Dversion=${pomVersion} \
-Dpackaging=${pomPackaging} -DrepositoryId=maven-hostd \
-Durl=http://192.168.1.200:30083/repository/maven-hostd
"""Using the Nexus Artifact Uploader plugin in a Jenkinsfile:
//use nexus plugin
def repoName = "maven-hostd"
def filePath = "target/${jarName}"
nexusArtifactUploader artifacts: [[artifactId: "${pomArtifact}",
classifier: '',
file: "${filePath}",
type: "${pomPackaging}"]],
credentialsId: 'nexus-admin-user',
groupId: "${pomGroupId}",
nexusUrl: '192.168.1.200:30083',
nexusVersion: 'nexus3',
protocol: 'http',
repository: "${repoName}",
version: "${pomVersion}"After uploading, the logs and repository view can be inspected to verify the artifact.
Nexus REST API helper functions (Groovy) for querying components and assets:
package org.devops
//封装HTTP
def HttpReq(reqType,reqUrl,reqBody){
def sonarServer = "http://192.168.1.200:30083/service/rest"
result = httpRequest authentication: 'nexus-admin-user',
httpMode: reqType,
contentType: "APPLICATION_JSON",
consoleLogResponseBody: true,
ignoreSslErrors: true,
requestBody: reqBody,
url: "${sonarServer}/${reqUrl}",
quiet: true
return result
}
//获取仓库中所有组件
def GetRepoComponents(repoName){
apiUrl = "/v1/components?repository=${repoName}"
response = HttpReq("GET",apiUrl,'')
response = readJSON text: """${response.content}"""
return response["items"]
}
//获取单件组件ID
def GetComponentsId(repoName,groupId,artifactId,version){
result = GetRepoComponents(repoName)
for (component in result){
if (component["group"] == groupId && component["name"] == artifactId && component["version"] == version){
return component["id"]
}
}
}
//获取组件信息
def GetSingleComponents(repoName,groupId,artifactId,version){
componentId = GetComponentsId(repoName,groupId,artifactId,version)
apiUrl = "/v1/components/${componentId}"
response = HttpReq("GET",apiUrl,'')
response = readJSON text: """${response.content}"""
println(response["assets"]["downloadUrl"])
}Artifactory integration follows a similar pattern: install the Artifactory plugin, configure server credentials, and use a Groovy pipeline to run Maven builds and upload artifacts.
def MavenBuild(buildShell){
def server = Artifactory.newServer url: "http://192.168.1.200:30082/artifactory"
def rtMaven = Artifactory.newMavenBuild()
server.connection.timeout = 300
server.credentialsId = 'artifactory-admin-user'
rtMaven.tool = 'M2'
def buildInfo = Artifactory.newBuildInfo()
String newBuildShell = "${buildShell}".toString()
println(newBuildShell)
rtMaven.run pom: 'pom.xml', goals: newBuildShell, buildInfo: buildInfo
server.publishBuildInfo buildInfo
}
def main(buildType,buildShell){
if(buildType == "mvn"){
MavenBuild(buildShell)
}
}Uploading to Artifactory uses an upload spec:
rtUpload (
serverId: "art1",
spec: """{
\"files\": [
{\n \"pattern\": \"target/${jarName}\",\n \"target\": \"${uploadDir}/\"\n }\n ]
}"""
)Finally, the article defines naming conventions for repositories (e.g., demo‑dev ) and artifacts (e.g., demo‑myapp‑service‑1.jar ) and shows directory structures to keep artifact storage organized.
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.