Jenkins - Maven Setup
Jenkins - Maven Integration
Section titled “Jenkins - Maven Integration”Apache Maven is a widely used build automation and project management tool, primarily for Java projects. Jenkins provides excellent integration with Maven.
Step 1: Ensure Maven is Available
Section titled “Step 1: Ensure Maven is Available”Jenkins needs access to a Maven installation to build Maven projects. You have two main options:
- Option A: Pre-install Maven: Install Maven manually on the Jenkins controller and any agents that will run Maven builds. Ensure the
M2_HOMEorMAVEN_HOMEenvironment variable is set, and Maven’sbindirectory is in the systemPATH. - Option B: Jenkins Auto-Installation (Recommended): Let Jenkins download and manage Maven installations automatically.
To download Maven manually (if choosing Option A):
- Visit the official Apache Maven download page: https://maven.apache.org/download.cgi
- Download the binary archive (e.g.,
apache-maven-X.Y.Z-bin.ziportar.gz). - Extract the archive to a suitable location (e.g.,
/opt/mavenorC:\Tools\Maven). This is yourMAVEN_HOME. - Optionally, add
MAVEN_HOME/binto your system’sPATHenvironment variable.
Step 2: Configure Maven in Jenkins
Section titled “Step 2: Configure Maven in Jenkins”Tell Jenkins where to find Maven installations or configure auto-installation.
- Navigate to Manage Jenkins → Global Tool Configuration.
- Scroll down to the ‘Maven’ section.
- Click ‘Add Maven’.
- Name: Give this Maven installation a descriptive name (e.g.,
Maven 3.8orDefault Maven). This name is used in Pipeline configurations. - MAVEN_HOME: If you pre-installed Maven (Option A), enter the path to your
MAVEN_HOMEdirectory here. Leave ‘Install automatically’ unchecked. - Install automatically: If you want Jenkins to manage the installation (Option B), check this box. Choose an installer from the dropdown (usually ‘Install from Apache’) and select the desired Maven version.
- Repeat ‘Add Maven’ if you need to configure multiple Maven versions.
- Click ‘Save’.
Step 3: Using Maven in a Pipeline Job
Section titled “Step 3: Using Maven in a Pipeline Job”The recommended way to build Maven projects is using a Jenkins Pipeline (Jenkinsfile).
pipeline { agent any // Or agent { label 'maven-agent' }
tools { // Specify the Maven installation configured in Global Tool Configuration maven 'Maven 3.8' // Use the name you defined jdk 'JDK 11' // Also specify JDK if needed }
stages { stage('Checkout') { steps { git url: 'your-repo-url', branch: 'main' } }
stage('Build') { steps { echo 'Building with Maven...' // Jenkins ensures the configured Maven is in the PATH // Use 'mvn' directly sh 'mvn clean package' } }
stage('Test') { steps { echo 'Running tests...' sh 'mvn test' } post { always { // Publish test results junit 'target/surefire-reports/**/*.xml' } } }
// Optional: Stage for deploying artifacts // stage('Deploy Artifact') { // steps { // // Example: Deploy to Nexus/Artifactory // // Requires configuration in pom.xml and settings.xml // // sh 'mvn deploy' // } // } }}Explanation:
toolsblock: Specifies which pre-configured tools (by name from Global Tool Configuration) should be available in thePATHfor this pipeline.sh 'mvn ...': Executes standard Maven commands. Jenkins ensures the correct Maven version specified in thetoolsblock is used.
Step 4: Using Maven in a Freestyle Job (Legacy)
Section titled “Step 4: Using Maven in a Freestyle Job (Legacy)”While Pipelines are preferred, you can still use Freestyle jobs:
- Create a new ‘Freestyle project’ or configure an existing one.
- Configure Source Code Management (e.g., Git).
- In the ‘Build’ section, click ‘Add build step’.
- Select ‘Invoke top-level Maven targets’.
- Maven Version: Choose the named Maven installation you configured in Global Tool Configuration.
- Goals: Enter the Maven goals to execute (e.g.,
clean package,test,deploy). - POM: Specify the path to the
pom.xmlfile (usuallypom.xmlin the root). - Properties/Options: Add any necessary Maven properties (
-Dkey=value) or JVM options. - Add post-build actions as needed (e.g., ‘Publish JUnit test result report’, ‘Archive the artifacts’).
- Click ‘Save’.
Maven Settings Files (settings.xml)
Section titled “Maven Settings Files (settings.xml)”If your builds require custom Maven settings (e.g., proxy configuration, private repository mirrors, server credentials for deployment), you need to provide a settings.xml file.
- Config File Provider Plugin: Install this plugin. Go to Manage Jenkins → Managed files. Add a new ‘Maven settings.xml’ file, paste your settings content, and give it an ID.
- Pipeline: Use the
withMavenwrapper step to provide the settings file: withMaven(mavenSettingsConfig: 'your-settings-file-id') { sh 'mvn deploy' }- Freestyle: In the ‘Invoke top-level Maven targets’ step, click ‘Advanced’ and select the configured settings file.
Proper Maven integration allows Jenkins to reliably build, test, and package your Java applications.