Painless Maven Builds with Jenkins


One of the great things about Maven is that it provides a standard project and build layout, along with a standard set of “goals.” Not only does this make it easier for developers to get up to speed on a new project, but it also allows Jenkins to provide special support for Maven projects, reducing the configuration needed while enhancing the build report automatically. That is the goal of the Maven plugin for Jenkins.

The central feature of the Maven plugin for Jenkins is the Maven 2/3 project type. Thanks to the Maven project object model (POM), this project type can automatically provide the following features:

  • Archive artifacts produced by a build
  • Publish test results
  • Trigger jobs for projects which are downstream dependencies
  • Deploy your artifacts to a Maven repository
  • Breakout test results by module
  • Optionally rebuild only changed modules, speeding your builds

All of the above can be accomplished in Free style builds, but this requires more configuration on your part.

Requirements for Plugin Use

All Jenkins releases have the Maven plugin included. You must also have at least one Maven installation.

How to Use It

First, you must configure a Maven installation (this step can be skipped if you are using DEV@cloud). This can be done by going to the system configuration screen (Manage Jenkins-> Configure System). In the “Maven Installations” section, 1) click the Add button, 2) give it a name such as “Maven 3.0.3” and then 3) choose the version from the drop down.

Now, Jenkins will automatically install this version any time it’s needed (on any new build machines, for example) by downloading it from Apache and unzipping it.
Next, create a new Maven job by 1) clicking “New Job” on the left hand menu, 2) give it a name, and 3) choose the “Build a Maven 2/3 project”.


You will then be presented with the job configuration screen. On this page, you need to provide 1) the SCM and 2) the Maven goals to call. That’s it! Choose the SCM you want to use (we’ll use Git), and then specify what Maven targets to call. We’ll call the “clean site install” goals so we can see the full effect.


Now, just click build now, and click on the progress bar in the left hand “Build Executor Status” to watch Jenkins install Maven, checkout your project, and build it using Maven. The build output should look something like this:


Now that the project is built, we can navigate to the detail page for our Maven module (project page -> Modules link on the left). For each module, Jenkins displays:

  1. A link to the module’s workspace
  2. The module’s artifacts
  3. A clickable test result trend
  4. Recent changes (just for that module!)
What’s unique about the Maven job type is that these links and reports are automatically broken down by module for you. You can even customize notifications for this module by clicking the “Configure” link on the left.

Finally, if we had configured Jenkins to build any downstream dependencies of this project, they would automatically start building after this build completed.

When running multiple builds simultaneously on the same slave, they will share the same local Maven repository. This can cause problems if two builds are trying to update the same artifact simultaneously, since local Maven repositories are not designed for concurrent access. One long-standing solution to this is to use the “Use private Maven repository” option in the “Advanced” section of the Maven build. This will create an isolated local Maven repository for the job (in $WORKSPACE/.repository) which prevents these problems. Jenkins releases since 1.448 let you specify a Maven repository per executor, which is a more efficient way to solve the same problem.

If you are using the Maven plugin, you should also investigate the M2 Release Plugin for automating releases with one click. The M2 Extra Steps plugin will let you run arbitrary build steps before and after your build. Finally, the new Config File Provider plugin lets you maintain different settings.xml which can be referenced by your Maven builds.

Relevant Documentation

Maven is a trademark of the Apache Software Foundation.

Ryan Campbell, Developer
Blog Categories: 


since version '1.433', the functionality of the 'M2 Extra Steps plugin' is available in the core 'maven-plugin', therefore the additional plugin is not needed anymore.

Thanks buddy!

Thanks for this info: "Upgrade to Jenkins 1.448 to specify Maven repo per executor"... saved us lots of time and heartache when running multiple builds simultaneously on the same slave - we had to use private Maven repos to avoid problems when two builds were trying to update the same artifact simultaneously.

Very, very old topic, but..... What about Sonar? with the newest versions of SonarQube its recommended to run sonar as maven step, but with maven-style jobs we can not run two different mavens :S

Is there a suggesion on pulling the newest version of a dependency during a Jenkins build? I've used ranges but that seems to be unstable, not pulling the same dependency build consistently or the latest. I need it to pull the latest. Thanks

Add new comment