Uploading and Managing Artifacts in Nexus with Maven, Jenkins Plugin, and REST API
This guide explains how to store, upload, and retrieve build artifacts using Nexus repository manager through its UI, Maven configuration, Jenkins Nexus Artifact Uploader plugin, custom pipeline functions, and direct REST API calls, including download examples with curl and wget.
Nexus is a repository manager that stores and retrieves build artifacts in a private, secure repository, supporting common build tools such as maven , ant , and gradle .
Artifact Upload via UI : In Nexus UI, navigate to Upload , select the target repository, and fill in the artifact coordinates and metadata.
Using Maven Tool
Configure authentication in settings.xml :
<server>
<id>mymaven</id>
<username>admin</username>
<password>admin123</password>
</server>Upload with the Maven command:
mvn deploy:deploy-file \
-DgroupId=xxxxxx \
-DartifactId=xxxxxx \
-Dversion=xxxxxx \
-Dpackaging=xxxxxx \
-Dfile=xxxxxx \
-Durl=xxxxxx \
-DrepositoryId=mymavenIf a pom.xml is available, you can upload directly:
mvn deploy:deploy-file \
-DgeneratePom=false \
-DrepositoryId=mymaven \
-Durl=http://192.168.1.200:8081/repository/mymavenrepo \
-DpomFile=pom.xml \
-Dfile=target/demo-0.0.1-SNAPSHOT.jarUsing Jenkins Plugin
Install the Nexus Artifact Uploader plugin and generate DSL with the snippet generator:
nexusArtifactUploader artifacts:[[artifactId: 'devopstest', classifier: '', file: 'target/demo-0.0.1-SNAPSHOT.jar', type: 'jar']]],
credentialsId: '1de05a13-197c-4a72-8c6a-cd330fc45559',
groupId: 'com.jenkins',
nexusUrl: '192.168.1.200:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'mymavenrepo',
version: '1.1.2'For repeated use, wrap the upload logic in a reusable function:
def NexusUploadByPlugin(artifactId, file, type, groupId, version){
nexusArtifactUploader artifacts:[[artifactId: artifactId, classifier: '', file: file, type: type]],
credentialsId: '1de05a13-197c-4a72-8c6a-cd330fc45559',
groupId: groupId,
nexusUrl: '192.168.1.200:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'mymavenrepo',
version: version
}Using Nexus REST API
Upload files directly with curl commands. Example for a PNG file:
curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=myrepo" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "raw.directory=/tmp" \
-F "[email protected];type=image/png" \
-F "raw.asset1.filename=default.png"Similar commands work for tar.gz , zip , and jar files.
Downloading Artifacts
Use curl :
curl -u admin:admin123 http://192.168.1.200:8081/repository/anyops/com/anyops/anyops-devops-service/1.1.1/anyops-devops-service-1.1.1.jar -o anyops-devops-service-1.1.1.jarOr wget :
wget --http-user=admin --http-passwd=admin123 http://192.168.1.200:8081/repository/anyops/com/anyops/anyops-devops-service/1.1.1/anyops-devops-service-1.1.1.jarExample Jenkins Pipeline for Artifact Upload
The pipeline consists of four stages: Checkout, Build, UnitTest, and Upload. The Upload stage calls the reusable NexusUploadByPlugin function.
@Library("mylib@main") _
import org.devops.*
def checkout = new Checkout()
def build = new Build()
def unittest = new UnitTest()
def sonar = new Sonar()
pipeline {
agent { label "build" }
options { skipDefaultCheckout true }
stages {
stage("Checkout") {
steps { script { checkout.GetCode(env.srcUrl, env.branchName) } }
}
stage("Build") {
steps { script { sh "mvn clean package" } }
}
stage("UnitTest") {
steps { script { unittest.CodeTest(env.buildTool) } }
}
stage("Upload") {
steps { script { NexusUploadByPlugin(env.artifactId, 'target/demo-0.0.1-SNAPSHOT.jar', env.type, env.groupId, env.version) } }
}
}
}
def NexusUploadByPlugin(artifactId, file, type, groupId, version){
nexusArtifactUploader artifacts:[[artifactId: artifactId, classifier: '', file: file, type: type]],
credentialsId: '1de05a13-197c-4a72-8c6a-cd330fc45559',
groupId: groupId,
nexusUrl: '192.168.1.200:8081',
nexusVersion: 'nexus3',
protocol: 'http',
repository: 'mymavenrepo',
version: version
}This comprehensive guide demonstrates multiple ways to manage and publish artifacts in a Nexus repository, covering UI, Maven, Jenkins plugin, custom pipeline functions, REST API, and direct download methods.
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.