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.

Monday, March 1, 2010

Free Maven Repository Hosting for Open Source projects by Sonatype

I'm very excited to see Sonatype support maven repositories for Open Source projects that use maven. In all honesty, they didn't have to do this. Unfortunately, the java.net repo was harming the maven reputation. I've had direct experience using the java.net maven repo and can say it was an unpleasant experience. When we open sourced our 4 JBI (Java Business Integration) Components their home existed on java.net and we used the their maven repo. It was difficult to upload anything and it seemed to be constantly down.

Before this announcement, open source java projects using maven didn't really have an option as to where they could publish their artifacts. To my knowledge neither Google Code or Sourceforge offered this capability. Apache and Codehaus did and obviously you still have the Maven Central (http://repo1.maven.org/maven2/), but I never went through the process of what it took to use them. Now it doesn't matter where your project is hosted. Hopefully the next thing to come is free Continuous Integration services in the cloud using Hudson. I think this is the next step for project hosting sites like Google Code and github.

Providing free maven repos I think has a lot of benefits and not just for maven users. For example, this should benefit all dependency management tools that are built on top of maven repos. I'm not 100% sure tools like Ivy, Grape, Gradle, and Buildr use maven repo's, but my guess is they do and this will benefit those users. Another benefit is being able to standardize on maven repositories, hopefully preventing users from searching where they can find your artifacts. I've wasted a lot of time in the past trying to find valid repositories where I could find artifacts for a project I was wanting to use.

I'm also very impressed with the features Sonatype is providing. Not only will they support release artifacts but SNAPSHOT's as well which could consume a lot of space. You'll also be able to easily sync with Central. Finally, they will support a staging repo in order to test things out before officially releasing.

So go sign up and thanks Sonatype. Also, read this post to learn how to release your project using the maven release plugin.