Archiving an artefact to Nexus

This section describes how to archive an artefact to Nexus.

Method

There are two methods how you can upload your artefacts from Jenkins to Nexus:

  1. Making a call to Nexus API using cURL (or Nexus Artifact Uploader).

  2. Uploading artifact as a part of Maven build lifecycle.

Curl

Nexus API requires user credentials to be provided. We advice you to pass your Nexus credentials to your Jenkins job as environment variables.

Let’s assume your ENV variables have the following names USERNAME and PASSWORD.

The following snippet should go inside Jenkins job definition:

    steps {
        shell('''
            |cd target
            |curl -v -F r=<REPOSITORY_NAME> -F hasPom=false -F e=war -F g=com.spring.test -F a=petclinic -F v=1.0 -F p=war -F file=@petclinic.war -u $USERNAME:$PASSWORD http://nexus:8081/nexus/service/local/artifact/maven/content
              '''.stripMargin()
        )
    }

Parameters

  • r - repository

  • e - extension

  • g - group id

  • a - artifact id

  • v - version

  • p - packaging

  • c - classifier (optional)

Maven

First you need to have the Config File Provider Plugin installed on Jenkins.

Once you have it installed, add a new Maven settings.xml configuration file.

In the file you need to specify your Nexus credentials which will be used to upload artefacts.

Add the following snippet to the settings.xml configuration file:

<servers>
    ...
      <server>
            <id>nexus</id>
            <username>USERNAME</username>
            <password>PASSWORD</password>
      </server>
    ...
</servers>

In place of USERNAME and PASSWORD placeholders insert your real Nexus credentials.

The artefact will be uploaded to Nexus as a part of Maven build lifecycle. Thus, the Nexus properties should be defined in Jenkins Job DSL steps in maven section.

steps {
        ...
        maven {
            goals('clean install -DskipTests')
            goals('deploy:deploy-file')
            property('groupId', 'com.spring.maventest')
            property('artifactId', 'petclinic')
            property('version', '1.0.0')
            property('generatePom', 'false')
            property('packaging', 'war')
            property('repositoryId', 'nexus')
            property('url', 'http://nexus:8081/nexus/content/repositories/<YOUR_REPO_NAME>/')
            property('file', 'target/petclinic.war')
            mavenInstallation("ADOP Maven")
            providedSettings('MySettings')
            
        }
        ...
}

Note: The “repositoryId” parameter is not a Nexus repository ID, it is the ID of the server section in your settings.xml file which has the credentials needed for deployment.

Properties

  • goals() - Maven goals to execute

  • property() - Adds a property for the Maven build

  • mavenInstallation() - Specifies the Maven installation for executing this step

  • providedSettings() - Speficies the settings.xml file to use


More details here: