So Maven 3 has finally been released, with a host of improvements and significant rework under the hood. Maven 3 is largely backward compatible with Maven 2, so migration from Maven 2 to Maven 3 is usually a very easy process. In this article, we will take a look at the one area where you may have some work to do in the migration process: site generation.
In many ways, this is a very discreet release, with few apparent major new features. However, under the hood it's a different story. The internal architecture of Maven 3 has pretty much been completely rewritten, with Guice as it's IOC framework, and with clean integration points to allow smooth interaction with third-party systems such as IDEs and CI servers. Given this almost-total rewrite of the Maven internals, the extremely high backward compatibility that Maven 3 shows is truly impressive.
Performance is a big feature of this new release, both in terms of general performance, and with the new ability to build modules in parallel, which can be very useful for large, multi-module builds. Maven 3 is more strict about Maven best practices, and will grumble if you don't define version numbers for your plugins or dependencies (as of course you should).
One more significant difference however is site generation. You might use the Maven site generation useful as a central point of documentation about your project, or you may use it simply to generate reports and code quality metrics for use in CI servers such as Hudson or TeamCity. In either case, the mvn site command can come in very handy.
If you have been using the reporting section of the pom.xml file to generate code quality metrics, javadoc reports, and so forth, you may have a little work to do to migrate this feature into Maven 3. Indeed, the reporting and reportSets sections have been deprecated (it won't cause an error with Maven 3, it will just be ignored), and have been replaced by a reportPlugins section in the configuration block of the maven-site-plugin itself. For example, suppose your Maven 2 pom.xml file looks like this:
<project>
...
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<formats>
<format>xml</format>
<format>html</format>
</formats>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.6</version>
</plugin>
</plugins>
</reporting>
</project>
This won't work in Maven 3. Instead, you will have to integrate your reports into the maven-site-plugin, as shown here:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.0-beta-2</version>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.1</version>
<configuration>
<aggregate>true</aggregate>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<formats>
<format>xml</format>
<format>html</format>
</formats>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.6</version>
</plugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
</project>
So you are basically just moving your old reporting plugins into the configuration section of the new maven-site-plugin. You probably should take the opportunity to upgrade your reporting plugins - I haven't come across any incompatibility problems, but you never know.
Maven 3 generally produces better console output than Maven 2, and reporting is no exception. At the end of the mvn site output, you get a nice summary of the reports generated:
[INFO] Generating "Source Xref" report --- maven-jxr-plugin:2.1
[INFO] Generating "Test Source Xref" report --- maven-jxr-plugin:2.1
[INFO] Generating "Checkstyle" report --- maven-checkstyle-plugin:2.6
[INFO]
[INFO] There are 93 checkstyle errors.
[INFO] Generating "Surefire Report" report --- maven-surefire-report-plugin:2.6
[INFO] Generating "Cobertura Test Coverage" report --- cobertura-maven-plugin:2.4
...
[INFO] Cobertura Report generation was successful.
[INFO] Generating "CPD Report" report --- maven-pmd-plugin:2.5
[INFO] Generating "PMD Report" report --- maven-pmd-plugin:2.5
[INFO] Generating "FindBugs Report" report --- findbugs-maven-plugin:2.3.1
[INFO] Locale is en
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 39.315s
[INFO] Finished at: Tue Oct 12 15:23:22 NZDT 2010
[INFO] Final Memory: 37M/98M
[INFO] ------------------------------------------------------------------------
All in all, a pretty straight-forward migration path.
One final comment on Maven site generation might be worth noting - when it first came out, the Maven-generated technical web site was revolutionary. However, nowadays there are other tools that build on Maven's powerful reporting features to take things even further - CI servers like Hudson and TeamCity can make use of the XML reports generated by Maven to provide code quality metrics of their own, and Sonar can even run code quality metrics reporting on any Maven project, without needing to configure the project at all. All these options are worth considering when you try to introduce code quality reporting into your project.