Skip to main content

Upload artifacts to Sonatype Nexus

You can use the Nexus Publish Drone plugin in your CI pipelines to upload artifacts to Sonatype Nexus Repository Manager.

You can also upload artifacts to S3, upload artifacts to GCS, and upload artifacts to JFrog.

Requirements

  • You have access to a Sonatype Nexus Repository Manager instance.
  • You have a CI pipeline with a Build stage. If you haven't created a pipeline before, try one of the CI tutorials.
  • Your pipeline has steps that generate artifacts to upload, such as by running tests or building code. The steps you use depend on what artifacts you ultimately want to upload.

Add the Plugin step

The following YAML example describes a Plugin step in a CI stage that updates the Jira Build field when there is a successful build.

              - step:
type: Plugin
name: upload_sonatype
identifier: upload_sonatype
spec:
connectorRef: account.harnessImage ## Docker Hub container registry connector
image: harnesscommunity/publish-nexus-repository:1.1.1
settings:
username: deploy-user ## Nexus Repository Manager username
password: <+secrets.getValue("nexus_password")> ## Nexus Repository Manager password
server_url: http://34.235.128.201:8081/ ## Nexus Repository instance URL
filename: ./target/example-1.0.jar ## Path to the artifact to upload
format: maven2 ## Repository format
repository: maven-releases ## Destination repository name
attributes: "-CgroupId=org.dronetest -CartifactId=example -Cversion=1.0 -Aextension=jar -Aclassifier=bin" ## Key-value pairs providing additional metadata
Plugin step specifications
  • type: Plugin
  • name: Specify a step name.
  • identifier: Specify a unique step ID.
  • connectorRef: Specify a Docker connector.
  • image: harnesscommunity/publish-nexus-repository:1.1.1
  • settings: Configure the Nexus Publisher plugin's properties, as described in the following table.
KeysTypeDescriptionValue example
usernameStringA username for accessing Nexus Repository Manager.
  • admin
  • test-user
passwordStringAn expression referencing a secret containing the password for the specified username.<+secrets.getValue("nexus_password")>
server_urlPublic URLThe URL of your Nexus Repository Manager instance.http://11.222.333.444:8000/
filenameStringThe path to the target artifact that you want to upload../target/example-1.0.jar
formatStringThe repository format.
  • maven2
  • raw
repositoryStringThe name of the repository where you want to upload the artifact.maven-releases
attributesString of key-value pairsComponent and asset attributes providing additional artifact metadata. "-CgroupId=org.dronetest -CartifactId=example -Cversion=1.0 -Aextension=jar -Aclassifier=bin"
Tips

You can use variable expressions for Settings values. For example, password: <+stage.variables.nexus_password> uses a stage variable.

Create text secrets for sensitive information, such as passwords.

Pipeline YAML example

The following YAML example is for a pipeline that includes steps to build an artifact and then upload it to Sonatype Nexus.

pipeline:
name: YOUR_PIPELINE_NAME
identifier: YOUR_PIPELINE_ID
projectIdentifier: default
orgIdentifier: default
tags: {}
properties:
ci:
codebase:
connectorRef: YOUR_CODEBASE_CONNECTOR
repoName: YOUR_CODE_REPO
build: <+input>
stages:
- stage:
name: stage1
identifier: stage1
type: CI
spec:
cloneCodebase: true
execution:
steps:
- step:
type: Run
name: Build
identifier: Run_3
spec:
shell: Sh
command: "mvn clean package"
- step:
type: Plugin
name: upload_nexus
identifier: upload_nexus
spec:
connectorRef: account.harnessImage
image: harnesscommunity/publish-nexus-repository:1.1.1
settings:
username: test-user
password: <+secrets.getValue("nexus_password")>
server_url: http://11.222.333.444:8000/
format: maven2
filename: ./target/example-1.0.jar
repository: maven-releases
attributes: "-CgroupId=org.dronetest -CartifactId=example -Cversion=1.0 -Aextension=jar -Aclassifier=bin"
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}

Confirm the upload

After you add the steps and save the pipeline, select Run to run the pipeline.

On the build details page, you can see the logs for each step as they run.

After the Plugin step runs, confirm that the artifact uploaded to Sonatype Nexus.

View artifacts on the Artifacts tab

As an alternative to manually finding artifacts on Sonatype Nexus, you can use the Artifact Metadata Publisher Drone plugin to publish artifacts to the Artifacts tab. To do this, add another Plugin step after the Nexus Publisher plugin step.

Add a Plugin step that uses the artifact-metadata-publisher plugin.

               - step:
type: Plugin
name: publish artifact metadata
identifier: publish_artifact_metadata
spec:
connectorRef: account.harnessImage
image: plugins/artifact-metadata-publisher
settings:
file_urls: ## Provide the URL to the target artifact that was uploaded by the Nexus Publisher plugin.
artifact_file: artifact.txt