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:
-
In projects with collaborative development, the build cache can reduce the time and network bandwidth for each developer to repeatedly download the same dependencies.
-
In CI/CD pipelines, the build cache can reduce build and deployment time, enhancing the efficiency of the entire development process.
-
In environments without internet access, the build cache can help you build projects offline.
-
In resource-constrained environments, the build cache can reduce resource consumption and improve build speed.
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:
-
Facilitates sharing dependencies in enterprise internal development environments: Configuring Nexus as an internal Maven repository in large enterprises ensures sharing and managing the same dependencies between projects, reducing external network requests, and improving the availability and security of dependencies.
-
Meets security and compliance requirements: Nexus can be used to enforce security and compliance standards, ensuring that the dependencies used have been audited and approved.
-
Manages custom dependencies: Nexus allows the management of custom dependency strategies, including caching, version control, and security checks, improving the control and maintainability of the development process.
-
Optimizes performance: Configuring Nexus as a local Maven proxy can improve the speed of dependency retrieval, reducing reliance on the public Maven registry, especially for teams across geographical regions.
Configure Maven build cache
Prerequisites
-
There is a deployed Jenkins instance on the platform.
-
Prepare a PVC (Persistent Volume Claim) for caching under the namespace where the Jenkins instance is located.
Procedure
-
After entering the Platform Management view, click DevOps Toolchain > Instances in the left navigation bar.
-
Click on the name of the Jenkins instance.
-
Under the Build Node tab, click on the Java node to be configured for caching.
-
Click on Update Build Node.
-
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/repositoryis the default directory Maven uses to store all project dependencies, typically used as the local cache for Maven project dependencies. After specifying dependencies in thepom.xmlfile of a Maven project, Maven will attempt to retrieve these dependencies from the.m2/repositorydirectory, 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/cacheis a feature introduced in Maven 3.6.0 and higher, used to store cache data from remote repositories..m2/cacheintroduces 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. -
Click Update.
Verify cache effectiveness
-
Switch to the DevOps view, click CI/CD > Jenkins in the left navigation bar.
-
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:
-
Refer to Assigning Platform Projects to assign the Maven repository in Nexus to the specified project in the platform.
-
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:
-
After entering the Platform Management view, click DevOps Toolchain > Instances in the left navigation bar.
-
Click on the name of the Jenkins instance.
-
Under the Build Node tab, click on the Java node where you want to configure the dependency repository.
-
Click on Update Build Node.
-
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> -
Click Update.