How to Use Nexus with Jenkins for Artifact Management
To use
Nexus with Jenkins, install the Nexus Jenkins plugin or configure Jenkins to publish artifacts directly to Nexus via scripts or pipeline steps. This setup allows Jenkins to upload build outputs to Nexus for centralized artifact storage and versioning.Syntax
Here is the basic syntax to upload artifacts from Jenkins to Nexus using a Jenkins Pipeline script:
nexusUrl: URL of your Nexus server.repository: Nexus repository name where artifacts will be uploaded.groupId,artifactId,version: Maven coordinates for the artifact.file: Path to the artifact file to upload.
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
// Build steps here
}
}
stage('Publish to Nexus') {
steps {
script {
def nexusUrl = 'http://nexus.example.com/repository/maven-releases/'
def repository = 'maven-releases'
def groupId = 'com.example'
def artifactId = 'my-app'
def version = '1.0.0'
def file = 'target/my-app.jar'
// Upload artifact using curl
sh """
curl -v -u user:password --upload-file ${file} \
${nexusUrl}${groupId.replace('.', '/')}/${artifactId}/${version}/${artifactId}-${version}.jar
"""
}
}
}
}
}Example
This example demonstrates a Jenkins Pipeline that builds a Java project with Maven and uploads the resulting JAR file to Nexus using a shell command.
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Publish to Nexus') {
steps {
script {
def nexusUrl = 'http://nexus.example.com/repository/maven-releases/'
def groupId = 'com.example'
def artifactId = 'my-app'
def version = '1.0.0'
def file = 'target/my-app.jar'
sh """
curl -v -u admin:admin123 --upload-file ${file} \
${nexusUrl}${groupId.replace('.', '/')}/${artifactId}/${version}/${artifactId}-${version}.jar
"""
}
}
}
}
}Output
[Uploading to Nexus]
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 123k 100 123k 0 0 12345k 0 --:--:-- --:--:-- --:--:-- 12345k
HTTP/1.1 201 Created
Artifact uploaded successfully.
Common Pitfalls
- Incorrect Nexus URL: Using the wrong repository URL will cause upload failures.
- Authentication errors: Ensure Jenkins has correct Nexus credentials.
- File path mistakes: The artifact path must be correct and accessible.
- Missing repository permissions: Nexus user must have rights to upload to the target repository.
bash
/* Wrong way: Missing authentication causes failure */ sh "curl --upload-file target/my-app.jar http://nexus.example.com/repository/maven-releases/com/example/my-app/1.0.0/my-app-1.0.0.jar" /* Right way: Include user and password */ sh "curl -u admin:admin123 --upload-file target/my-app.jar http://nexus.example.com/repository/maven-releases/com/example/my-app/1.0.0/my-app-1.0.0.jar"
Quick Reference
- Use Jenkins Pipeline
shstep withcurlto upload artifacts. - Configure Nexus credentials securely in Jenkins Credentials Manager.
- Use Maven or Gradle plugins for Nexus if preferred for automation.
- Verify Nexus repository URL and permissions before uploading.
Key Takeaways
Configure Nexus repository URL and credentials correctly in Jenkins.
Use Jenkins Pipeline scripts with curl or Nexus plugins to upload artifacts.
Ensure artifact file paths and Maven coordinates are accurate.
Check Nexus user permissions to avoid upload failures.
Securely store Nexus credentials in Jenkins Credentials Manager.