Wednesday, March 17, 2010

How to Speed up Maven

How many times do you invoke maven in a day? How much time could you save if you shaved 15 seconds off each execution? That doesn't really sound like a lot but when executed a 100 times a day it adds up quickly; and 15 seconds is probably conservative. Now stop being selfish and think collectively as team how often maven is executed. 15 seconds for 50-100 builds a day across a team of 10-15 programmers adds up even quicker. This was my experience experimenting with the maven-cli-plugin with a simple sub-module running mvn clean install with unit tests.

I had never actually heard of maven providing this ability. I had seen and used it for other technologies like grails and scala, but I never considered if maven did as well. It wasn't until James Strachen recently tweeted about attempting to use a new feature provided by Sonatype called mvnsh: "a CLI Interface that enables you to speed up your builds because project information and Maven plugins are loaded into a single, always-ready JVM instance". From the FAQ, "The Maven engine is only started up (bootstrapped) one time and then held in a "ready" state waiting for user input of Maven or other shell command". Since I spend most of my day basically running maven, I was very intrigued.

Unfortunately, it appears mvnsh only supports maven 3 projects and our projects are still using maven 2. Even though I've read maven 3 is backwards compatible, I'm still waiting for a more official stable release (maven 3 is currently at 3.0-alpha-7). On the bright side, it appears mvnsh was set to improve on the maven-cli-plugin that supports maven 2 projects.

So I just wanted to share my experience getting the maven-cli-plugin set up and get the word out to fellow maven users to improve their productivity. Overall it seems to work as expected and does improve my local build times. I'm on Ubuntu 9.10, maven 2.0.10, and JDK 1.5. As I mentioned in the beginning, using cli:execute-phase decreased my build times approximately 15 seconds. The biggest disadvantage so far is I don't have access to my alias's, which I depend on heavily for just about everything I do with maven.

Getting started with the maven-cli-plugin
I basically followed the Common Setup instructions on the github wiki User Guide.

I added the pluginGroup to my settings.xml file:
<pluginGroups>
    <pluginGroup>org.twdata.maven</pluginGroup>
</pluginGroups>
I then added a new profile to my settings.xml file:
<profile>
    <id>cli</id>
    <pluginRepositories>
        <pluginRepository>
            <id>twdata-m2-repository</id>
            <name>twdata.org Maven 2 Repository</name>
            <url>http://twdata-m2-repository.googlecode.com/svn/</url>
        </pluginRepository>
    </pluginRepositories>
</profile>
And finally I enabled the new profile in settings.xml:
<activeProfiles>
    <activeProfile>cli</activeProfile>
</activeProfiles>
Using the maven-cli-plugin
The maven-cli-plugin basically supports 2 goals: execute and execute-phase. Use execute when you want to run goals and use execute-phase when you want to run phases. It's unfortunate the plugin forces you to pre-commit to one or the other; hopefully mvnsh has improved this. I typically run clean install which are phases so I ran: mvn cli:execute-phase. This puts you in an interactive shell where you can run clean install. Once finished you continue to stay in the shell allowing you to repeatedly execute mvn phases without having to bootstrap maven each time.

Summary
Since I spend a majority of my time executing maven and this simple experiment decreased my build times, I plan on using the cli plugin for every day use when I don't want to use my alias's. Also, this plugin should work on windows as well so don't think you are left out. Let me know how your mileage varies. Do you have any other tips that improve maven execution times?

On a side note, I'm sure the django/python crowd cringe at how much time java developers waste compiling and deploying. Execution times are something I believe they don't have to worry about.
blog comments powered by Disqus