Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I speed up my maven2 build?

Tags:

maven-2

People also ask

How long should a maven build take?

Maven takes almost around 40 secs to build it. I have tried maven with multi threaded builds too by specifying -T and -C args for no of threads and cores to be used. But I haven't seen any significant improvement in wall time of my builds. I am using maven 3.2.

What does clean install mean in maven?

The short answer 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 . class files and resources (like .

What is maven build command?

To build a Maven project via the command line, you use the mvn command from the command line. The command must be executed in the directory which contains the relevant pom file. You pass the build life cycle, phase or goal as parameter to this command.


There are some possibilities to optimize some of the build tasks. For example the 'clean' task can be optimized from minutes to just milliseconds using simple trick - rename 'target' folder instead of delete.

To get details how to do it refer to Speed up Maven build.


I don't know what version of Maven you are using, I assume 2, but I will give what I use for Maven 1.x to speed up and make things build a tiny bit quicker.

These will fork the junit tests into a new process (also helps when you use environment variables in tests etc and gives the tests a little more memory.

-Dmaven.junit.fork=true
-Dmaven.junit.jvmargs=-Xmx512m

This forks the compilation which might speed things up for you

-Dmaven.compile.fork=true

I hope this can help a little, try it out.

Also refer to get more speed with your maven2 build.


If you are using Maven3 ($ mvn -version), you can also follow this guide. In my case, the results are:

Normal execution:

$ mvn clean install 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:05 min
[INFO] Finished at: 2015-07-15T11:47:02+02:00
[INFO] Final Memory: 88M/384M

With Parallel Processing (4 threads):

$ mvn -T 4 clean install
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:22 min (Wall Clock)
[INFO] Finished at: 2015-07-15T11:50:57+02:00
[INFO] Final Memory: 80M/533M

Parallel Processing (2 threads per core)

$ mvn -T 2C clean install

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:12 min (Wall Clock)
[INFO] Finished at: 2015-07-15T12:00:29+02:00
[INFO] Final Memory: 87M/519M
[INFO] ------------------------------------------------------------------------

As we can see, the difference it's almost a minute, near 20-30% of speed improvement.


  1. Adjust memory configurations to optimum for eg: add this line to mvn.bat set MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=256m

  2. Clean phase of mvn normally deletes target folder. Instead if we are renaming target folder the cleaning phase will be much faster.<quickClean>

  3. -Dmaven.test.skip=true will skip the test execution.

  4. Add -Denforcer.skip=true to mvn command line argument (This is enforcing versions of maven, jdk etc ,we can skip it after initial runs)

  5. Disable non-critical operations during build phase: Analysis, javadoc generation, source packaging. This will save huge time.

  6. Spawnig new process also helps in time improvement -Dmaven.junit.fork=true (fork the junit tests into a new process) -Dmaven.compile.fork=true (forks the compilation)

    Hope it helps.


You can use -DskipTests=true to skip unit tests. which would speed up builds


I've found that parsing reactor projects is significantly slower than single-pom projects. If your build is reactor (multi-module) and your developers are not working on all modules at the same time, you can remove the parent POM and build them separately, resolving the dependencies using the local repo. The disadvantage is that you need to install or deploy a module in order for its dependents to see the changes.

Also, you might want to look at the new Maven 2.1 M1 which contains some significant speed improvements.

If none of these helps, post more details about your project configuration (modules structure and plugins), command line parameters and hardware config (memory and disk). Running Maven with -X might also show where is it taking its time.