JavaTop 10 New Features in Maven 3

Top 10 New Features in Maven 3

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Maven 3.0 was just released and the Java build tool has come a long way since version 2 was released almost six years back. Maven 2 had reached a stage where it was difficult to extend and its code was difficult to understand. In version 3.0 many of the Maven internals have been revamped to overcome all the issues associated with Maven 2. In this article, I run down the top 10 features in Maven 3.

1. Backward Compatibility

Maven founder and Sonatype CTO Jason van Zyl says that “Maven 3 is built for the user,” which is evident from the fact that Maven 3.0 is a drop-in replacement for Maven 2.x. The Maven 3 developers have worked hard to make sure that the transition from Maven 2.x to Maven 3.0 is smooth. Maven 3.0 will issue warnings if a duplicate dependency or plugin declarations or plugin version numbers are not specified. Maven 3.0 highly recommends that you fix these problems in your POM as future versions of Maven 3 might not support building malformed projects. I tried porting my multiple-module project to Maven 3 and it worked like a charm. I also got the aforementioned warnings but I didn’t face any problems or get any build errors. Maven 3.0 is a viable option for Maven 2.x.

2. Performance Improvement

The most important feature for which I would migrate to Maven 3 is its speed. Through better disk I/O, network I/O, CPU utilization, and memory consumption, Maven 3 is tuned to perform 50-400% faster than Maven 2.x. Faster Maven builds lead to higher developer productivity. I ran my project against Maven 3 and it took less than 2 minutes when previously it had taken close to 3 minutes. This was without making any changes such as running parallel builds (which I will discuss shortly). I just downloaded Maven 3 and built my project against it. Maven 3’s speed alone made migrating from Maven 2.x worth it for me.

3. Automatic Parent Versioning

In Maven 2, you have to specify the parent version in the sub-modules, which is a maintenance problem when you start development on a new version. Maven 3.1 will eliminate the need to specify the parent version in sub modules.

4. Parallel Builds

Maven 3 has also introduced a new feature called parallel builds, which analyzes your project dependency graph and enables you to build schedule modules in parallel. To run a project with parallel build, you use this command:

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

The first command says build this Maven project with two threads, and the second command says build this project with two threads per core. With parallel builds, you can achieve performance improvements of 20-50%. However, parallel builds are an experimental feature that can change in the future, so it is not wise to use them in production.

5. Better Error and Integrity Reporting

Maven 3 has improved error reporting, and it provides you with a link to the Maven wiki page where you will get a full description of the error and the possible causes (see Figure 1). For example, I tried running one of my sub modules without a parent version and it gave me an error. Note that you will not be required to write parent POM versions in sub modules in Maven 3.1.

 

 

6. Plugin Extension Points

In Maven 3, you can hook up different extension points to modify plugin behavior instead of extending the plugin as done with Maven 2. This functionality is something that the Maven team borrowed from Eclipse. For example, you can define an extension point to alter the way the web.xml is processed by the WAR plug-in.

7. Mixins

Mixins allow you to compose your POM with parameterized POM fragments instead of using inheritance. Mixins will be available in version 3.1 of Maven. These Mixins will be deployed to a repository and be referenced with a standard coordinate. For instance, if you want to use the release process of my project , with Maven 2 you have to inherit the Maven POM of my project. Because a Mixin will be a POM consisting of plugins and configurations that can be externally parameterized, you can compose your POM with them.

8. Maven Shell

Maven Shell Features

9. Polyglot Builds

Polyglot Maven is an interesting feature of Maven 3. It allows you to write the POM file in a non-XML format. The Maven core provides an underlying DSL to access the Maven internals and write POM files in the language of your choice. The currently supported formats are Groovy, Ruby, Scala, Clojure, YAML, etc. Polyglot Maven is an attempt to empower Maven users who are not so fond of XML.

Polyglot Maven does not come bundled with Maven 3, so you need to download it separately. The distribution will contain a tool called translator, which converts your XML POM into a Groovy, Scala or YAML format POM. To convert an XML POM to a Scala POM, for example, I fired this command:

translate pom.xml pom.scala

So, this simple pom.xml:


<project xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi_schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.shekhar</groupId>
<artifactId>polygot-maven-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>polygot-maven-test</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

…is represented in Scala as this:

project { m =>
m.artifactId = "polygot-maven-test"
m.dependency { d =>
d.artifactId = "junit"
d.groupId = "junit"
d.optional = false
d.scope = "test"
d._type = "jar"
d.version = "3.8.1"
}
m.groupId = "com.shekhar"
m.modelVersion = "4.0.0"
m.name = "polygot-maven-test"
m.packaging = "jar"
m.properties += ("project.build.sourceEncoding" -> "UTF-8")
m.url = "http://maven.apache.org"
m.version = "1.0-SNAPSHOT"
}

10. M2Eclipse

M2Eclipse provides Maven integration with Eclipse. M2Eclipse 0.10.0 is the latest release and it has been completely rewritten using the Maven 3 embedder API. The performance of M2Eclipse has drastically improved (in range of 200-500%, according to its website). The high performance is because M2Eclipse will provide some extra XML metadata in the Maven POM, which will be recognized only by M2Eclipse.

About the Author

Shekhar Gulati is a Java consultant with over 5 years of experience. He is currently working with Xebia India, an Agile Software Development company. The postings on this site and on his blog are his own and do not necessarily represent the opinion of his employer. You can follow him on Twitter.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories