Home / FAQ / How to configure Maven build cache and dependency repository for Java build nodes in Jenkins?

How to configure Maven build cache and dependency repository for Java build nodes in Jenkins?

Overview

After configuring the build cache and dependency repository for the build nodes of a Jenkins instance, the downloaded dependencies can be reused during the Jenkins pipeline build process, reducing build time, improving pipeline efficiency, lowering network bandwidth consumption, and controlling dependencies effectively.

This article explains how to configure Maven build cache and Maven dependency repository for Java build nodes.

Introduction to Maven

Maven is a powerful build tool used for managing dependencies and the build process of Java projects.

When used as a build cache for Java build nodes, the following scenarios can be addressed:

Introduction to Nexus Repository Manager

Nexus Repository Manager provides a centralized repository for storing and managing build artifacts, offering features like access control, security, and version control.

When used as a Maven dependency repository, it provides the following features:

Configure Maven build cache

Prerequisites

Procedure

  1. After entering the Platform Management view, click DevOps Toolchain > Instances in the left navigation bar.

  2. Click on the name of the Jenkins instance.

  3. Under the Build Node tab, click on the Java node to be configured for caching.

  4. Click on Update Build Node.

  5. Add a Build Cache, configure the relevant parameters following the instructions below.

    Scenario Parameter Configuration Explanation
    Scenario 1 Storage Type: Persistent Volume Claim
    Cache Type: Maven Cache
    Cache Path: /root/.m2/repository
    .m2/repository is the default directory Maven uses to store all project dependencies, typically used as the local cache for Maven project dependencies. After specifying dependencies in the pom.xml file of a Maven project, Maven will attempt to retrieve these dependencies from the .m2/repository directory, reducing network requests to remote repositories and improving build efficiency.
    Scenario 2 Storage Type: Persistent Volume Claim
    Cache Type: Custom
    Cache Path: /root/.m2/cache
    .m2/cache is a feature introduced in Maven 3.6.0 and higher, used to store cache data from remote repositories. .m2/cache introduces the concept of Mirror Image, allowing caching of data from remote repositories locally, avoiding redundant network requests. This is beneficial for scenarios where remote repositories are frequently accessed during builds, significantly reducing build time and dependence on the network.
  6. Click Update.

Verify cache effectiveness

  1. Switch to the DevOps view, click CI/CD > Jenkins in the left navigation bar.

  2. Execute the pipeline (with a Java node configured with build cache and containing a Maven build task), and check the pipeline’s Pod.

    Note: If the Pod mounts the PVC configured for the build cache, it indicates that the build cache is effective. An example YAML is as follows:

    volumes:
        - name: volume-0
        secret:
            defaultMode: 420
            secretName: demo-xxxx-2603138448
        - name: volume-2
        persistentVolumeClaim:
            claimName: demo-jenkins-pvc

Configure Maven dependency repository

You can flexibly choose the configuration method for the dependency repository based on the actual business requirements.

Prerequisites

The platform has integrated Nexus, and a Maven repository (Repository) already exists in Nexus.

Method 1: Use the Platform-Integrated Maven Dependency Repository

After assigning the Maven dependency repository to the platform project, you can flexibly configure the dependency repository for any Maven build task created graphically or from a template under different projects.

Configuration Steps:

  1. Refer to Assigning Platform Projects to assign the Maven repository in Nexus to the specified project in the platform.

  2. When creating a pipeline graphically or from a template under the project and adding a Maven build, select the Maven repository assigned by the platform as the Dependency Repository.

Method 2: Configure Dependency Repository via Maven Settings File

After configuring the dependency repository for the Java build node, any pipeline using this build node on the platform can pull dependencies from this repository.

Note: Configuring the dependency repository at the build node level does not affect users configuring other dependency repositories for Maven build tasks. All configured dependency repositories can be used.

Procedure:

  1. After entering the Platform Management view, click DevOps Toolchain > Instances in the left navigation bar.

  2. Click on the name of the Jenkins instance.

  3. Under the Build Node tab, click on the Java node where you want to configure the dependency repository.

  4. Click on Update Build Node.

  5. Enable the Maven Settings File feature, enter the following configuration information in the Content to configure the Maven dependency repository.

    Note: Replace the Username, Password, and Maven repository URL in the configuration information with the username, password, and URL for accessing the Maven repository in Nexus.

    <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
    
    <servers>
        <server>
        <id>my-maven-repository</id>
        <username>{ Username }</username>
        <password>{ Password }</password>
        </server>
    </servers>
    <profiles>
        <profile>
        <id>my-default-profile</id>
        <repositories>
            <repository>
            <id>my-maven-repository</id>
            <name>my-maven-repository</name>
            <url>{ Maven repository URL }</url>
            <layout>default</layout>
            </repository>
        </repositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>my-default-profile</activeProfile>
    </activeProfiles>
    </settings>
  6. Click Update.