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 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:
A link to the module's workspace
The module's artifacts
A clickable test result trend
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 agent, 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.
Maven is a trademark of the Apache Software Foundation.
Ryan Campbell, Developer
Want to learn the latest in Jenkins? Subscribe to the Jenkins newsletter, Continuous Information. This monthly newsletter contains all of the latest interesting and useful happenings in the Jenkins community, sent directly to your inbox.
Stay up to date
We'll never share your email address and you can opt out at any time, we promise.