Skip to content

Jenkins - Maven Setup

Apache Maven is a widely used build automation and project management tool, primarily for Java projects. Jenkins provides excellent integration with Maven.

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_HOME or MAVEN_HOME environment variable is set, and Maven’s bin directory is in the system PATH.
  • Option B: Jenkins Auto-Installation (Recommended): Let Jenkins download and manage Maven installations automatically.

To download Maven manually (if choosing Option A):

  1. Visit the official Apache Maven download page: https://maven.apache.org/download.cgi
  2. Download the binary archive (e.g., apache-maven-X.Y.Z-bin.zip or tar.gz).
  3. Extract the archive to a suitable location (e.g., /opt/maven or C:\Tools\Maven). This is your MAVEN_HOME.
  4. Optionally, add MAVEN_HOME/bin to your system’s PATH environment variable.

Tell Jenkins where to find Maven installations or configure auto-installation.

  1. Navigate to Manage Jenkins → Global Tool Configuration.
  2. Scroll down to the ‘Maven’ section.
  3. Click ‘Add Maven’.
  4. Name: Give this Maven installation a descriptive name (e.g., Maven 3.8 or Default Maven). This name is used in Pipeline configurations.
  5. MAVEN_HOME: If you pre-installed Maven (Option A), enter the path to your MAVEN_HOME directory here. Leave ‘Install automatically’ unchecked.
  6. 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.
  7. Repeat ‘Add Maven’ if you need to configure multiple Maven versions.
  8. Click ‘Save’.

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:

  • tools block: Specifies which pre-configured tools (by name from Global Tool Configuration) should be available in the PATH for this pipeline.
  • sh 'mvn ...': Executes standard Maven commands. Jenkins ensures the correct Maven version specified in the tools block 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:

  1. Create a new ‘Freestyle project’ or configure an existing one.
  2. Configure Source Code Management (e.g., Git).
  3. In the ‘Build’ section, click ‘Add build step’.
  4. Select ‘Invoke top-level Maven targets’.
  5. Maven Version: Choose the named Maven installation you configured in Global Tool Configuration.
  6. Goals: Enter the Maven goals to execute (e.g., clean package, test, deploy).
  7. POM: Specify the path to the pom.xml file (usually pom.xml in the root).
  8. Properties/Options: Add any necessary Maven properties (-Dkey=value) or JVM options.
  9. Add post-build actions as needed (e.g., ‘Publish JUnit test result report’, ‘Archive the artifacts’).
  10. Click ‘Save’.

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 withMaven wrapper 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.