Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: configure parallel build in pom.xml

Tags:

java

maven

Maven has a capability to perform parallel builds: https://cwiki.apache.org/confluence/display/MAVEN/Parallel+builds+in+Maven+3

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

Is it possible to specify this arguments in pom.xml or settings.xml? Repeating this options could be annoying.

like image 337
Adam Szecowka Avatar asked Sep 08 '14 08:09

Adam Szecowka


People also ask

Which of the following Maven commands is correct for parallel setup?

mvn -T 4 package This command tells maven to run parallel builds using the specified thread count. It's useful in multiple module projects where modules can be built in parallel.

Is Maven multi threaded?

By default, Maven uses a single thread. In the age of multicores, this is just waste. It's possible to run parallel builds using multiple threads by setting an absolute number or a number relative to the number of available cores. For more information, please check the relevant documentation.

What is Maven_opts?

MAVEN_OPTS is an environment variable used by the shell scripts that launch the Java Virtual Machine (JVM) that runs Maven. Using MAVEN_OPTS you can pass options to the JVM when it's launched.

What is Mvn clean install?

mvn clean install is the command to do just that. You are calling the mvn executable, which means you need Maven installed on your machine. (see How do you install Maven?) You are using the clean command, which will delete all previously compiled Java .


1 Answers

This solution is a bit of a hack but worked for me. It involves specifying a new environment variable, assigning the value -T3 to it and adding this variable to the Maven launch script.

For Windows (Linux in parens):

  1. Open the Environment Variables window: Computer -> Properties -> Advanced System settings -> Environment Variables
  2. Add the property MAVEN_CMD_LINE_OPTS with your desired value. In my case -T 3 as I want Maven to use 3 threads to build in parallel.
  3. Edit the mvn.cmd file (In Linux: the mvn file). Find the part where the Java command is actually executed, the line starting with %MAVEN_JAVA_EXE% (In Linux: generally after the line defining the main class: org.codehaus.plexus.classworlds.launcher.Launcher)

  4. Add %MAVEN_CMD_LINE_OPTS% to the end of the line (In Linux: $MAVEN_CMD_LINE_OPTS)

When you run mvn compile on a Maven project you will now see the following line:

Using the MultiThreadedBuilder implementation with a thread count of 3

This has the advantage of the user being able to 'override' this value. So if the user executes mvn -T4 compile, then 4 threads are used instead of the default 3.

Note:

  1. I tried this on Maven 3.3.9 but the concept should work on any Maven version.
  2. Multi-threaded builds can suffer from issues where plugins especially custom plugins are not thread safe. So use with care and consider disabling this as a fix in case of issues.
like image 99
Deepak Avatar answered Sep 20 '22 13:09

Deepak