|
|
|
|
send this page as an e-mail message |
|
|
|
Level: Beginner
Key Hu (jianhgreat@hotmail.com), Master of Jiaotong University
Huweih@cn.ibm.com, engineer, IBM Regional Partner Support Center, IBM
February 23, 2006 This article focuses on the new features of Maven2, which can greatly shorten the workload in development management, and enable developers to focus on real business issues.
Maven has been there for a long time now, and it's still clear to me how it feels. Now, the idea of empathy for Maven from Ant is simple, because Maven can display information about the project in the form of a Web site, such as a list of developers and various report types. This approach has brought great convenience to the construction of the project, especially the report. Imagine the resulting junit-report, JavaDoc, CheckStyle, PMD and other reports, if there is not a unified portal, each time to switch directories is how boring things.
Maven is undoubtedly quite successful, as you can see from more and more open source projects using MAVEN. The reason for Maven's success is simple: while simplifying the build script, the functionality does not shrink, it increases, it provides tools to assemble the project information, and presents it in a fairly friendly way; a rich plugin simplifies the job. Such powerful tools appear, naturally, to be used in a scramble.
New features
Now that Maven2 has been launched, Maven's official website says MAVEN2 is a pretty big shift from MAVEN1 to the end, even at the expense of compatibility. (For the sake of Maven1 's users, Maven1 is still continuing his mission.) The result of such a big change.
1. Faster and easier
Compared to the Maven1 of the speed of the slow speed, MAVEN2 has a qualitative leap in velocity, even compared with Ant is no less than (of course, download does not count). In addition, "simplifying work, using industry-accepted best practices" is another major theme of Maven2, and other new features are ubiquitous in Maven2 efforts to streamline work.
2. Fewer configuration Files
Comparison of Maven1 and Maven2 main profiles: Maven1:project.xml, Maven.xml, Project.Properties and Build.properties. Maven2:pom.xml and Settings.xml.
POM is the core object model of MAVEN, where Pom has been transferred from Project.xml to Pom.xml and the version has been upgraded from 3 to 4, Maven2. For the project, generally only need to pom.xml on the line.
The use of maven.xml is not required and is not advocated in Maven2 for the following reasons: Plugin usability enhancements. The content scattered in the maven.xml is difficult to share among different projects and is not conducive to maintenance. In Maven2, it is recommended that you use custom plugin to encapsulate the content.
If you still expect to be able to use features like Maven.xml, such as <PREGOAL>, refer to inserting non-standard build steps using Pregoals and Postgoals.
In Maven2, the configuration uses settings.xml, which replaces the original project.properties and Build.properties. There are two levels of configuration in Maven2: User-level, for operating system logged-on users. Generally in $home/.m2/, for Windows users, it is the directory: C:/Documents and settings/user name/.m2/settings.xml. Global level: Typically the%m2_home%/conf/settings.xml,m2_home is the root environment variable name of the Maven2.
Can be configured in settings.xml, such as local repository, proxy, etc., about the structure of settings.xml can be obtained from MAVEN's official website.
3. Plugin language Exchange
In Maven2, the language of writing plugin is changed from Jelly to Java and BeanShell. Java has an advantage in speed and is more familiar to developers. For other popular scripts, such as Groovy,maven's official website, the opinion is that wait for it to be more mature before considering.
4. Provide a predefined catalog template
A good directory structure makes it easier for developers to understand the project, and also lays a good foundation for future maintenance work. MAVEN2 provides developers with a default standard catalog template based on the industry's best-known directory structure. The standard directory structure for MAVEN2 is as follows:
You can make pom.xml more concise by using the catalog template. Because the MAVEN2 has predefined related actions based on the default directory, no manual intervention is required. Take the resources directory as an example: src/main/resources, responsible for managing the resource of the project subject. After executing compile with Maven2, all files and subdirectories in this directory are copied to the Target/classes directory, which provides convenience for future packaging. Src/test/resources, responsible for managing the resources of the project test. After executing test-compile with Maven2, all files and subdirectories in this directory are copied to the Target/test-classes directory, ready for subsequent tests.
These actions are in Maven1 and need to be done using <preGoal> or <postGoal> in Maven.xml. Today, you can do this automatically without having to specify it in Pom.xml. The use of resources in both SRC and test, which is easy to build and test, has been the experience of previous generations. By using MAVEN2, this experience has been popularized in the development team.
To create a standard catalog template, you can use the following command:
MVN archetype:create-dgroupid=com.codeline.commons-dartifactid=codelinecommons
|
The meaning of GroupID and Artifactid is the same as in Maven1, where the value of the parameter Artifactid is the name of the project's root directory. In addition to creating the appropriate directory, MAVEN2 also creates a default pom.xml.
Maven2 also considers that different types of projects need to have different directory structures. If you create a Web project, you can use the command:
MVN archetype:create-dgroupid=com.mycompany.app
-dartifactid=my-webapp
-darchetypeartifactid= Maven-archetype-webapp
|
5. The introduction of life cycle
There is a clear life cycle concept in Maven2, and the corresponding commands are provided to make project construction clearer. Primary life cycle phase: Validate, verifying that the project is correct, and that all required resources are available. Compile, compile the source code for the project. Test-compile, compile the project test code. Test, testing the compiled source code with the compiled test code. Package, a published format, such as a jar, packages the compiled source code. Integration-test, process and publish packages in an environment where integration testing can run. Verify, run any checks to verify that the package is valid and meets the quality standards. Install, the package installed in the local repository, can be used as a dependency of other projects to use the deploy, in the consolidation or release environment, the final version of the package copy to the remote repository, so that other developers or projects can be shared. Generate-sources, generate any additional source code that the application requires, such as Xdoclet.
If you want to perform a project compilation, then direct input: mvn compile, for the other stages can be analogous. There is a dependency relationship (dependency) between the phases, such as test-dependent test-compile. When you execute MVN test, the MVN Test-compile is run first and then MVN test.
6. New Dependency Scope
<scope> is also introduced in the,<dependency> in Pom 4, which primarily manages dependent deployments. Currently <scope> can use 5 values: Compile, default, applies to all stages and will be published along with the project. provided, similar to compile, expects the JDK, container or user to provide this dependency. such as Servlet.jar. runtime, used only at runtime, such as the JDBC driver, for the run and test phases. Test, used only during testing, to compile and run the test code. Will not be published with the project. System, similar to provided, needs to explicitly provide an jar,maven that contains dependencies and does not look for it in repository.
Examples of <scope> use:
<dependency>
<groupId>hibernate</groupId>
<artifactid>hibernate</artifactid >
<version>3.0.3</version>
<scope>test</scope>
</dependency>
|
7. Transitive dependencies, simplifying dependency management
In Maven1, you need to list the packages that are needed for dependencies. This is too much to do with users like Hibernate, and it's inconvenient. Delivery dependencies are implemented in Maven2, so for the packages that hibernate relies on, MAVEN2 is automatically downloaded, and developers only need to care about hibernate.
Note: This feature can only be obtained if the dependency is supported by Maven, usually in the form of plugin. And for some old plugin, it is possible that the relationship of time does not support transitive dependency. If at least in Maven 2.0.1, for Hibernate 2.1.2, you still need to explicitly list the packages that hibernate 2.1.2 relies on.
Introduction to use
The steps to install Maven2 are simple: first download the appropriate package from the MAVEN official website, currently maven 2.0.1; Then unzip and set the environment variable m2_home= Maven2 The decompression installation directory, and finally add%m2_home%/bin to the path, Easy maven to run in any directory.
The Run command for Maven2 is MVN, and the mvn-h can be used to obtain relevant help information. Common scenario: Creating a Maven project: MVN archetype:create compiling source code: MVN Compile compiling Test code: MVN test-compile Run test: MVN test generates SITE:MVN site package: mvn packag e Installing JAR:MVN Install in local repository The resulting project: MVN clean
Perhaps due to the recent launch, Maven2 there are still some unsatisfactory places. Especially the report part of the plugin, some because there is not, such as Junit-report. Some are puzzling problems, such as Checktyle and PMD, that do not work properly under local locale. With PMD as an example, the following exception is thrown when a PMD report is generated:
Java.util.MissingResourceException:Can ' t find bundle for base name Pmd-report,
locale ZH_CN
at Java.util.ResourceBundle.throwMissingResourceException (ResourceBundle
. java:839)
at Java.util.ResourceBundle.getBundleImpl (resourcebundle.java:808) at
Java.util.ResourceBundle.getBundle ( resourcebundle.java:702)
...
|
Fortunately, it takes a long time for the Maven2 to appear, and that sort of problem should soon be resolved.
Conclusion
Maven2 a big step forward on the basis of MAVEN1 's superiority. The new features it provides greatly reduce the amount of effort in development management, allowing developers to focus on real business issues. And these new features have also played a positive role in simplifying use and popularizing best practices.
Reference Inserting non-standard build steps using Pregoals and Postgoals http://docs.codehaus.org/display/MAVEN/Maven2+Goal +decoration
MAVEN official website http://maven.apache.org/
MAVEN-Supported dependency list Http://www.ibiblio.org/maven2
Using Maven in Eclipse
Project management: Maven makes things easier
About the author
|
|
|
Hu Yu, Master of Jiaotong University, has been engaged in software development since graduating in 2000. Since 2002, Java has been used, and opensource tools such as Ant, Maven, Hibernate, struts, etc. are often used in the development of projects, and the specification and technology of information integration are currently being researched. You can get in touch with him through jianhgreat@hotmail.com, or visit your personal blog:http://blog.donews.com/foxgem/. |
|
|
|
Hu Weihong, Master of Xi ' an Jiaotong university, currently works for IBM Regional Partner Support Center. Primarily responsible for WebSphere product support. Have a lot of interest in open source projects. You can contact her by huweih@cn.ibm.com. |