Speed up your build with Maven 3


I have been trying to find ways to make my project’s maven build run fast. It is very frustrating and annoying when your project’s full build takes 12 minutes to complete. It does not happen too often in a day because you work at module level,  but 2-3 times a day you need to build the full project and if your build takes this long,  it really slows you down. These days builds take a lot of time because you embed all sorts of plugins in your build like checkstyle, findbugs, cobertura (It is evil because it run your test twice), etc.  Although,  It does give you time to have coffee but 12 minutes is way too much. So, I was really pissed off.

Few days back I was checking my tweets and found out that Maven is releasing its new version and it will have some performance improvements. Maven 3 is the latest version of maven which is expected to be released today.  So, I decided to play with it. I downloaded the beta version of  Maven 3, changed my windows path system variable to point to Maven 3 and ran a mvn clean install on my project. Without any build failures or other problems, the build just ran fine. I have heard that when Maven 2 was released people had hard time migrating from version 1 to 2 as it was not backward compatible. But Maven 3.0 is completely backward compatible and most of the plugins will also work fine. To my surprise, the build took 9 minutes. This is still a high number but it was way better than 12 minutes. Seeing this decrease in build time motivated me to read more about Maven 3.

After reading some blogs and watching Jason Van Zyl presentation,  I found out that there are lot of new features in Maven 3. One of the new features in Maven3 is Parallel Builds. Parallel build analyzes your project’s dependency graph and schedule modules that can be run in parallel. This is a very cool feature and it really sped up the build. To run a paralled build, you can type

mvn -T 4 clean install
mvn -T 2C clean install

The first command will run your build with 4 threads and second command will build your project with 2 threads per core. How fast your build will become, depends a lot on your project/build structure, how your unit tests are distributed in modules  so it may vary from project to project. For my project with 4 threads, the build time got reduced to under 6 minutes, which I think is  great.  You should try out some combination with number of threads or number of threads per core to find out the best possible combination for your project. I am very happy that build time of my project got reduced to almost half.

In his presentation, Jason Van Zyl talks a shell based tool called Maven Shell which can also further improve the performance. Maven Shell is maven embedded in a long lived shell process. It does not comes bundled with maven 3 so you have to download it from http://shell.sonatype.org/. I tried running my project build inside Maven Shell but it gives PermGen space error all the time. I tried changing the perm gen memory setting but it didn’t helped. Maven shell is expected to run much fast because it caches the parsed POMs.

Maven 3 is definitely worth spending time so have a look at it. I have just touched the tip of iceberg , there are lot of new features in Maven 3.  I will try and talk about others in my future blogs.

8 thoughts on “Speed up your build with Maven 3

  1. Pingback: Tweets that mention Speed up your build with Maven 3 « A daily technical diary -- Topsy.com

  2. Baptiste Wicht

    Thanks for this interesting post🙂

    In my side, I’ve tested Maven 3 on my project (a multimodule one) and it’s 10% slower than Maven 2…

    I tried also the parralel builds, but it doesn’t change anything. I’ve a lot of reports indicating that my modules are not marked thread safe, so I don’t know if the builds are made parralel or not.

    Di you’ve configured something to have this performance improvement ?

    Reply
    1. whyjava Post author

      Hello Baptiste,

      I didn’t configured anything for improving the performance. Its strange that you didn’t get any performance improvement. I tested this on my Windows Vista Intel Core2 duo 2.40 GHz 4GB RAM 64 Bit operating system.

      Reply
      1. Baptiste Wicht

        Thanks for reply.

        Yes, I found it strange too… I tested on my Ubuntu 10.04, Core 2 Duo 3.06 Ghz, 64 bit, 6GB RAM. Perhaps it was already a good time of build, I don’t know.

  3. Henke

    I’m wondering about this too… Do you have to structure your modules in some special way to get a good speed bump?

    Comparing the -T 2C flag to something in for example -j6 in make the difference is staggering.
    Adding -j6 to a make based progress can cut the build time down several times but for Maven based there seems to be little or now difference.

    Reply
  4. Knut Ivar

    Anyway you can “default” the -T parameter?
    … I want start mvn without specify the -T 1C all the time.

    btw; great article. It really works!🙂 excellent. Finally my Quad core comes in handy.

    Reply
  5. Pingback: 2010 in review « I'm Shekhar Gulati , this is my blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s