Sunday, July 10, 2011

Upgrading to Maven 3

In my previous post, I discussed about what is Maven 3, reasons to migrate to Maven 3 and some of it's features. I tried upgrading to Maven 3 in some of our projects and for those projects, these are the following changes that I had to do to migrate to Maven 3.

  • Reporting Section - In Maven 2, all report configuration was under top level reporting section. But, in Maven 3, these configurations have to be moved under reportPlugins in maven site plugin configuration.
  • Report configuration does not apply to build - In Maven 2, we used to have PMD/Checkstyle/Cobertura plugin configurations in report configuration and it also took effect during the build. But, this is not the case anymore in Maven 3. So, I had to move these configurations to build section for them to take effect.
  • Maven 3 needs "project." prefix for groupId, artifactId etc., In Maven 2, the prefix was not required, but now in Maven 3, because of stricter POM validation, it is necessary to add this prefix.
  • Maven versions plugin needs reportSets configured - With Maven 2, it produced default reports, but with Maven 3, I had to add reportSets section and needed to add the necessary reports under that.
  • Needed to upgrade maven site plugin from 2.2 to 3.0-beta-2 to make it work with Maven 3.
  • Needed to upgrade maven findbugs plugin from 2.3.1 to 2.3.2-SNAPSHOT to make it work with Maven 3.

After doing these changes, I could do "mvn clean install" for our project, which ran PMD/Checkstyle/Findbugs/Cobertura analysis fine and also ran tests properly. Also, did "mvn clean site" and could confirm that the site was generated properly like before.

In your project, based on the plugins that you use, there could be other changes involved. Look at the plugin compatibility notes and plugin compatibility matrix for more information. But, the above changes should work for most users.

These are the changes needed to be done in the POM. Once the POM is changed, upgrading to Maven 3 for other developers in the rest of the team is very easy. All they have to do is download Maven 3, unzip it and change M2_HOME to point the new Maven 3 install directory. That's it!

Saturday, July 9, 2011

Maven 3

Introduction:

Maven 3 is complete rewrite of maven internal architecture and is almost backward compatible. Maven 3 repository format is same as that of Maven 2, unlike the switch from Maven 1 to Maven 2. Reporting is not part of core Maven and is now part of maven site plugin.

Reasons to migrate to Maven3:
  • Latest and Greatest!
  • New development only in Maven 3 - 6 week release cycle
  • Maven 2 - Release cycle is unpredictable
  • Backward compatible (almost)
  • Faster
  • Parallel Builds
  • Eclipse/OSGi friendly
  • Tycho - Helps building Eclipse/OSGi bundles with Maven
  • Strict Validation of POM
  • Improved error reporting - Link to maven page with description, cause and possible fix.
  • Improved Logging

Speed:


Maven documentation says that it could be anywhere from 50% - 400% faster, but it depends on the project though. In one of our simple projects, I could see 25% improvement for both clean install and clean site (no downloading of dependencies involved).

Parallel Builds:

One of the other advantage of Maven3 is parallel builds. Most of us have dual or quad cores these days and so it would be good to take advantage of them. Below are some examples:
  • mvn -T 2 clean install (Build project with 2 threads)
  • mvn -T 2C clean install (Build project with 2 threads per core)
Maven analyzes the projects and executes the plugins accordingly. Also, individual plugins need to declare themselves thread safe (@threadSafe). In one of our small project that I tried with, I could not see big difference with 2 threads, but with big projects, there could be a difference.

Maven Shell:

Maven Shell (mvnsh) seemed to be very promising. The idea is to load JVM, maven and plugins and keep it in memory, thus avoiding start up cost every time maven command is run. But, when I tried it, I kept getting out of memory error (even after increasing my MAVEN_OPTS). At least, this was the cause when I tried it few months back. It could be fixed by now.

Polyglot Maven:

Polyglot maven allows to write POM in other JVM languages - Groovy, Scala, Clojure etc., It is useful for those who don't like the verbose XML POM's. Polyglot maven needs to be downloaded separately though. It comes with translator to convert your current XML POM's. Below are some examples:
  • translate pom.xml pom.scala
  • translate pom.xml pom.groovy

Issues:

  • profiles.xml is no longer supported. It is advised to move that to inside settings.xml.
  • Reporting now goes under reportPlugins in maven-site plugin configuration.
  • mvn dependency:tree may not be correct, since this still uses legacy dependency resolution code.
  • Because of stricter POM validation, you will see more errors. These have to be fixed before proceeding further.
For the comprehensive list, look at the compatibility notes available here.

Conclusion:

I tried Maven 3 few months back (in March 2011) but just got chance to do a post on this. So, some issues that I saw might be fixed by now. This post is a compilation of what I learned in the brief time that I invested in reading/trying out maven 3. If you want to learn more, you can look at these other references, that are in my del.icio.us bookmarks.