Maven: how to do parallel builds?

Maven 2Build ProcessBuild Automation

Maven 2 Problem Overview


When you build with maven on a multicore / multi-CPU machine it would often be possible to build different subprojects in parallel. Is there a way to do this with maven? Is there a plugin for this / whatever?

Maven 2 Solutions


Solution 1 - Maven 2

Maven 3 (as of beta 1) now supports parallel builds as an experimental feature.

For example,

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

Full documentation can be found on the Maven wiki: Parallel builds in Maven 3 - Apache Maven - Apache Software Foundation.

Solution 2 - Maven 2

The suggested solutions are great, but I wanted to add something to the answers here regarding the test stability during parallel builds.

So, when Maven parallel build is used:

mvn -T 4 clean install # Builds with 4 threads
mvn -T 1C clean install # 1 thread per cpu core
mvn -T 1.5C clean install # 1.5 thread per cpu core

Some issues with tests can appear. Note any behavior in tests which is different between serial and parallel test execution. Most of the times it happens do to improper test isolation resource-wise.

For example, test1 manipulate db entry with key 12345, which is hard-coded and test2 uses the same entry! It can't be good…

It's a situation that should be considered in the first place, but sometime it's forgotten and could lead to different problems once the switch to parallel maven build is made.

In case that happens, and you still want to use the parallel execution at least in some of the occasions, you can (of course, besides trying to fix the test and make them properly isolated) to disable Maven test runs using -DskipTests argument:

mvn clean install -T 4 -DskipTests

Solution 3 - Maven 2

Some of the CI build applications (e.g. hudson) can build multiple maven projects at the same time (and even on multiple machines).

Support for this in maven 'standalone' would also be nice, a quick look through the maven issue tracker gave me: http://jira.codehaus.org/browse/MNG-3004

Solution 4 - Maven 2

If you came to this question looking to sort out your build server and you're not using one that deals with maven natively the magic flag you are looking for is this:

-Dmaven.repo.local=someNoneGlobalDir

do that for each one of your builds and you can let them run all at the same time rather than having everything that uses maven in a queue!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHans-Peter StörrView Question on Stackoverflow
Solution 1 - Maven 2hallidaveView Answer on Stackoverflow
Solution 2 - Maven 2JohnnyView Answer on Stackoverflow
Solution 3 - Maven 2jorView Answer on Stackoverflow
Solution 4 - Maven 2JonnyRaaView Answer on Stackoverflow