Maven practice (10) -- Maven 3, it's time to upgrade

Source: Internet
Author: User

Apache Maven released the official version last October in 3.0. On the 22nd of last month, the eclipse Foundation announced the release of eclipse 3.7 (indigo, one of the biggest new features of this version of eclipse is the integration of Maven. Users who download the Eclipse IDE for Java developers version will find that eclipse can automatically identify Maven projects. The built-in Maven version in Indigo is 3.0.2, which indicates that Maven 3 is very stable to some extent. However, I believe that there are still many Maven 2 users who are hesitant to upgrade. This article will introduce some of the most important features of Maven 3, aiming to help readers eliminate their doubts and enjoy all the conveniences that Maven 3 can bring as soon as possible.

Ensure compatibility

When upgrading the software, compatibility is obviously the first consideration. If the project can be successfully built under Maven 2, it will immediately fail under Maven 3, and it is difficult to fix it, which is obviously unacceptable. Fortunately, Maven users can dispel this concern. Since its design, Maven 3 has always considered its compatibility with Maven 2. This not only refers to its compatibility with the core of Maven 2, but also involves a large number of Org. apache. maven. plugins and org. codehaus. mojo plugin. Although some plug-in code is not fully compatible with 100% due to its special nature, there are basically no problems.

If you are still worried, read the official Maven 3.x compatibility notes and Maven 3.x plug-in compatibility list carefully. The compatibility issues of these two documents mainly involve some features that should be discarded, and all of them provide solutions under Maven 3.

Improved performance

Compared with Maven 2, Maven 3 has greatly improved its performance. This is reflected in two aspects: reduced memory usage and reduced build time. In particular, the parallel building feature introduced by Maven 3 can analyze the dependencies between project modules, and then build modules with no dependencies between them in parallel, so as to make full use of today's popular multi-core CPU resources.

The following two commands are constructed by four threads, and each core is allocated with one thread based on the number of CPU cores:

$ mvn -T 4 clean install $ mvn -T 1C clean install

Anders Hammar, one of Maven contributors, introduced a simple experiment in the ten reasons why he migrated his article to Maven 3, using Maven 2.2.1 and Maven 3.0.2 (single thread) respectively ), build the same Maven source code as Maven 3.0.2 (4 thread) that contains 32 modules. The following results are obtained:

Table 1. Build Maven SCM trunk with "MVN package" (32 modules)
  Time/memory
Maven 2.2.1 3: 20/53 m
Maven 3.0.2 (single thread) /27 m
Maven 3.0.2 (4 threads) 2: 26/28 m

We can see that the memory usage in Maven 3 is reduced by nearly half! However, after parallel building is enabled, the time saved is considerable. The larger the project, the more obvious the performance improvement. If your development environment does not have enough memory, and your project is very large, you can immediately switch to Maven 3 by saving the memory.

Improve inter-module dependency analysis

One of the headaches in Maven 2 is that when you build a multi-module project, in order to make the previous module take effect in the classpath module, you must install it in the local repository before Maven can parse and use it. Almost all Maven 2 users encountered this confusion either early or late. "Why can't I still see the update of module A when I have MVN clean Package Module A and can build Module B?" This problem is solved in Maven 3. when building a multi-module project, Maven 3 will parse the dependencies between modules from the reactor, that is, as long as module A executes the package, then Module B can find and parse the JAR file generated by a according to the relative path.

Promote best practices

Users who have just switched from Maven 2 to Maven 3 may find that the command line prints the following warnings when executing the Build:

[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-javadoc-plugin is missing.  [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.

Most of the following warnings are caused by the fact that users do not specify a version When configuring plug-ins or dependencies, which does not guarantee the reproducibility of the build and introduces potential risks to the build. This warning is a balance between compatibility and best practices. Similar improvements include discarding the profile. xml feature, and clearly separating project dependencies and plug-in dependencies.

Improved LOG output

This is a minor improvement, but it highlights the care of Maven developers for Maven users. I personally like this improvement very much. In short, Maven 3's build log is easier to read. Plug-in outputs are separated by blank lines. The version, target, and artifactid of each running plug-in are clearly displayed. When a build error occurs, such output helps you locate the problem more quickly.

Site (note !)

The site feature is the core of Maven 2, but in Maven 3, this feature is completely moved to Maven-site-plugin, which leads to the need to transfer related configurations. The site-related configuration in Maven 2 is under the POM reporting element, for example:

<project>   ...   <reporting>     <plugins>       <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-javadoc-plugin</artifactId>         <version>2.7</version>       </plugin>     </plugins>   </reporting> </project>

In Maven 3, all site-related configurations should appear under Maven-site-plugin:

<project>  ...  <build>    ...    <plugins>      ...      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-site-plugin</artifactId>        <version>3.0-beta-3</version>        <configuration>          <reportPlugins>            <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-javadoc-plugin</artifactId>              <version>2.7</version>            </plugin>          </reportPlugins>        </configuration>      </plugin>    </plugins>  </build> </project>
Summary and thanks

This article introduces Maven 3 in terms of compatibility, new features, performance improvement, and important details. Maven 3 is a sign of Maven's maturity. If you haven't upgraded yet, I strongly recommend that you try it at least. MAVEN installation is very simple, download a zip package, decompress the package, and set simple environment variables. Download the package now!

Due to the limited capacity and energy, it is difficult for me to write more Maven articles that are neither repeated nor fit with the taste of many readers. Therefore, I plan to release this column for the moment. I sincerely thank Zhang Kaifeng for his planning and editing, readers for his support, and my family, especially my three-year-old daughter, who should have contributed to the draft. Finally, I will continue to care about the development of Maven and share more experiences and experiences with me if I have the opportunity.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.