Maven Tips to optimize build timings


Few Maven Tips and tricks to make Maven builds fast and faster and efficiently.
This can help to reduce the build timings drastically and optimize your build timings

  • Run compilation parallel

mvn -T 2 clean compile
#Above will trigger 2 threads

mvn -T 1C clean install
#Above will trigger 1 thread per CPU, if you have 2 CPU core then 2 threads will be spawned.
#You can even give number in fraction (1.5, 2.5 etc).



Note - > Maven takes care of dependency and calculate if 2 modules are dependent and will compile the first one before other. Still parallel builds feature is going through lot of extensive testing and subjected to use.
I have faced dependency problem in some cases and had to revert back to normal build.  This will not work with maven-release-plugin.
But it works 90% of time.
  • Skip the unit test
This is the easiest way to make the build fast. If you know that you can skip the test and taking care of running unit tests in some other phase, this will work for you.

-DskipTests
-Dmaven.test.skip=true
-Darguments="-DskipTests"

The first one will skip the test, latter one will not even compile the tests which will save the time and make maven build fast.
Last one will be use when you use maven release plugin and you want to skip the test during release prepare and perform.
  • Running the tests in separate thread with extra memory
You can even run the unit compilation tests and compilation in new thread
 -Dmaven.junit.fork=true 
and
-Dmaven.compile.fork=true 
and
-Dmaven.testng.fork=true 

Give extra memory to thread
-Dmaven.junit.jvmargs=-Xmx512m
  • Skip the JavaDoc when you are running daily regular build.
-Dmaven.javadoc.skip=true
  • Don't look remote Maven 
mvn -o clean install

-o Option will make your build offline, This means your build will no longer look for any artifacts from remote maven server and will use from local .m2.

Comments

Popular posts from this blog

Colour formatting - Jenkins Console

Manage Docker images on local disk

How to migrate Parent pom from maven to gradle