Using Maven Artifact Repositories in Jenkins

Last modified by DevOps-as-a-Service Operator on 2025/02/05 11:33

DevOps-as-a-Service offers the functionality Artifact Repository, which is used to store artifacts built by Jenkins delivery pipelines.

Jenkins configuration

Both for pushing and pulling from the Maven repository associated with your project, you will need to set up a Maven settings.xml in your Jenkins Project. This needs to be done just once per project. Afterward, all delivery pipelines in the project can use it.

Configure Credentials

The Automatically provided Credentials should be used to push or pull from a repository of the same project. This is the normal case.

If you want to use the repository of another project, you have to do some prerequisites first:

  1. Create a technical user for Gitea or Nexus in the DevOps Portal.
  2. If you just want to pull artifacts, add a VIEWER role for the matching project to the previously created technical user.
  3. If you want to push artifacts, add a DEVELOPER role for the matching project to the previously created technical user.
  4. Now add the credentials (username and password) of the technical user to the Jenkins project, which will perform the push or pull. Use the link Project Credentials in the Jenkins tile of the dashboard in the DevOps Portal to get to the right place.

Add Config File

Go to the home of the DevOps Portal and click on Project Folder in the Jenkins tile.

image-2024-3-15_20-39-14.png

The Jenkins project will be opened in a new tab of your browser. Now click on Config Files in the menu on the left-hand-side.

image-2023-2-21_9-59-26.png

Now click on Add a new Config and select the type Maven settings.xml. Replace the provided ID with something more meaningful, like maven-settings.

image-2023-2-21_10-2-51.png

Then click NEXT.

It's suggested to use the ID also for the Name of the config file. Now click the Add button in the Server Credentials section. In the example below two servers are defined.

You can choose yourself what ServerIds you want to use. The important thing is, that the same ServerIds are used later in your pom.xml.

image-2024-3-15_20-50-31.png

The example content for the settings.xml provided by Jenkins can be adjusted to your needs, but it's not required. It's enough that you have added server credentials in the step before. Jenkins will automatically overwrite the <servers> section in the settings.xml for you.

That's all you have to configure here. The URLs for the defined servers will be set somewhere else. Therefore, click now Submit to save your changes.

Adapt Jenkinsfile

Now, you have to make sure that the maven-settings defined above are used when Jenkins is calling maven.

When using the Jenkins Shared Library adapt it like this:

Jenkinsfile
 
@Library('sdcloud') _

sdcPipeline(
   mvnCommand: 'mvn clean deploy',          // Overwrite the default of mvn verify to let maven really push something to nexus
   mavenSettingsConfig: 'maven-settings'    // The ID of the Config File which was set up above   
)

If you are not using the Jenkins Shared Library you can call maven yourself like in the example below:

Jenkinsfile
 
stage('deploy') {
    withMaven(
       maven: 'maven',
       mavenSettingsConfig: 'maven-settings') // The ID of the Config File which was set up above.
   {
        sh 'mvn clean deploy'
   }
}

Pushing Maven Artifacts to a Project Repository

The following changes are required in the git repository of your maven project.

Adapt pom.xml

Set up the URL of the Project repository for in the pom.xml of your Maven Java project like shown below by adding a <distributionManagement> section.

PKEY is the  "Project Key" as defined in the DevOps Portal for the project repository you want to push to. Please note that this sub string is case-sensitive. So, include the project key with uppercase letters.

Example for Gitea

pom.xml - distributionManagement
 
<distributionManagement>
 <repository>
   <id>gitea-push</id> <!-- The repository id has to match the server id in your settings.xml. -->
   <url>https://gitea-CUSTOMER.devops.t-systems.net/api/packages/PKEY/maven</url> <!-- Replace CUSTOMER and PKEY with the right values -->
 </repository>
 <snapshotRepository>
   <id>gitea-push</id> <!-- The repository id has to match the server id in your settings.xml. -->
   <url>https://gitea-CUSTOMER.devops.t-systems.net/api/packages/PKEY/maven</url> <!-- Replace CUSTOMER and PKEY with the right values -->
 </snapshotRepository>
</distributionManagement>

Example for Nexus

pom.xml - distributionManagement
 
<distributionManagement>
 <repository>
   <id>nexus</id> <!-- The repository id has to match the server id in your settings.xml. -->
    <url>https://CUSTOMER.devops.t-systems.net/nexus/repository/PKEY/</url> <!-- Replace CUSTOMER and PKEY with the right values -->
 </repository>
 <snapshotRepository>
   <id>nexus</id> <!-- The repository id has to match the server id in your settings.xml. -->
   <url>https://CUSTOMER.devops.t-systems.net/nexus/repository/PKEY/</url> <!-- Replace CUSTOMER and PKEY with the right values -->
 </snapshotRepository>
</distributionManagement>

If you use mvn deploy for a release version like 1.0.0 it will use the URL defined in <repositoryRepository>.

If you use mvn deploy for a snapshot version like 1.0.0-SNAPSHOT it will use the URL defined in <snapshotRepository>.

As you can see in the example, it's not a problem to use the same URL for both.

Pulling Maven Artifacts from a Project Repository

The following changes are required in the git repository of your maven project.

Adapt pom.xml

Set up the URL of the Project repository in the pom.xml of your Maven Java project like shown below by adding a <repository> element to the <repositories> section.

PKEY is the  "Project Key" as defined in the DevOps Portal for the project repository you want to push to. Please note that this sub string is case-sensitive. So, include the project key with uppercase letters.

Example for Gitea

pom.xml - repositories
 
<repositories>
 <repository>
   <id>gitea-pull</id> <!-- The repository id has to match the server id in your settings.xml. -->
   <url>https://gitea-CUSTOMER.devops.t-systems.net/api/packages/PKEY/maven</url> <!-- Replace CUSTOMER and PKEY with the right values -->
 </repository>
</repositories>

Example for Nexus

pom.xml - repositories
 
<repositories>
 <repository>
   <id>nexus</id> <!-- The repository id has to match the server id in your settings.xml. -->
   <url>https://CUSTOMER.devops.t-systems.net/nexus/repository/PKEY/</url> <!-- Replace CUSTOMER and PKEY with the right values -->
 </repository>
</repositories>

The repository id has to match the server id in your settings.xml.

For pulling a dependency which was pushed before, you will need to also add a <dependency> to the <dependencies> section. See below for an example that you have to adapt to the real groupId, artifactId and version you used during pushing.

pom.xml - dependencies
 
<dependency>
 <groupId>com.tsystems.devops</groupId>
 <artifactId>maven-test</artifactId>
 <version>1.0.0-SNAPSHOT</version>
</dependency>

For using a dependency, it is not required to run mvn deploy. mvn verify or mvn install will also do it.