Compared with the slow running speed of Maven 1, Maven2 has made a qualitative leap in speed, even compared with Ant (of course, downloading is not counted). In addition, "Simplifying work and using industry-recognized best practices" is another theme of Maven2, and other new features reflect Maven2' s efforts to simplify work anywhere.
2. Less configuration files
Maven 1 is compared with the main configuration files of Maven2;
Maven 1: project.xml, maven.xml, project.properties and build.properties
Maven 2: pom.xml and settings.xml
POM is the core object model of Maven. In Maven2, POM has been transferred from project.xml to pom.xml, and the version has been upgraded from 3 to 4. For projects, generally only pom.xml is needed.
Maven.xml is not required or recommended in Maven2 for the following reasons:
Enhance the ease of use of plug-ins.
The scattered content in maven.xml is difficult to share between different projects and is not conducive to maintenance. In Maven2, it is recommended to use custom plug-ins to encapsulate these contents.
If you still want to be able to use features like maven.xml, such as
In Maven2, the configuration uses settings.xml, which replaces the original project.properties and build.properties
User level, used for operating system login users. Generally in $home/.m2/, for windows users, it is the directory: c: \ documents and settings \ user name \ .m2 \ settings.xml.
Global level: generally in %M2_HOME%/conf/settings.xml, where M2_HOME is the root directory environment variable name of Maven2.
Settings.xml can be configured, such as local repository, proxy, etc. The structure of settings.xml can be obtained from Maven official website.
3. Plug-in language replacement
In Maven2, the plug-in language was changed from jelly to Java and BeanShell. Java is faster and more familiar to developers. For other popular scripts, such as groovy and Maven's official website, the advice is to wait until it is more mature.
4. Provide predefined directory templates
A good directory structure can make it easier for developers to understand the project and lay a good foundation for future maintenance. Maven2 provides a default standard directory template for developers according to the best directory structure recognized by the industry.
Using directory template can make pom.xml more concise. Because Maven2 has predefined related actions according to the default directory, there is no need for human intervention. Take the resource directory as an example:
Src/main/resources, which is responsible for managing the resources of the project subject. After compiling with Maven2, all files and subdirectories in this directory will be copied to the target/classes directory, which provides convenience for future packaging.
Src/test/resources, which is responsible for managing the resources for project testing. After performing test compilation with Maven2, all files and subdirectories in this directory will be copied to the target/test-classes directory to prepare for subsequent tests.
In Maven 1, these actions need to be used in maven.xml
To create a standard directory template, you can use the following command:
Mvn prototype: create-d groupid = com.codeline.commons-dartififact id = codelinecommons.
The meanings of groupId and artifactId are the same as those in Maven 1, and the value of the parameter artifactId will be the name of the project root directory. In addition to establishing the corresponding directory, Maven2 will also create the default pom.xml.
Maven2 also believes that different types of projects need different directory structures. If you create a web project, you can use the command:
Mvn prototype: create-d groupid = com.mycompany.app.
-darti factd = my-web app
-DarchetypeArtifactId = maven-archetype-web app
5. Introduction to life cycle
There is a clear concept of life cycle in Maven2, and all of them provide corresponding commands to make the project construction more clear. Main life cycle stages:
Verify that the project is correct and that all necessary resources are available.
Compile, compile the source code of the project.
Test-compile, compile the project test code.
Test: test the compiled source code with the compiled test code.
Package, a publishing format, such as jar, packages the compiled source code.
Integration-Test, process, and publish packages in an environment where integration tests can be run.
Verify, run any checks to verify that the package is valid and meets quality standards.
Install, which installs the package in the local repository and can be used as a dependency by other projects.
Deploy executed in an integrated or publishing environment copies the final version of the package to a remote repository so that other developers or projects can use it.
Generate-sources, which generates any additional source code required by the application, such as xdoclet.
If you want to compile a project, just enter: mvncomplete directly, and so on in other stages. There are dependencies between phases, for example, testing depends on test-compilation. When performing the mvn test, the mvn test compilation will be run first, and then the mvn test will be run.
6. Add Dependency Range
In POM 4,
The default value of Compile applies to all phases and will be published with the project.
Similar to compile, Provided expects JDK, container or user to provide this dependency. Such as servlet.jar
Runtime, only used at runtime, such as JDBC driver, is suitable for runtime and test phase.
Test is only used during testing to compile and run test code. Will not be published with the project.
Similar to provided, the System needs to explicitly provide a jar containing dependencies, and Maven will not look it up in the repository.
& lt Scope & gt Examples used:
& lt dependency & gt
& ltgroupId & gt hibernate & lt/groupid >
& ltartifactId & gt hibernate & lt/artifactid >
& lt version & gt3.0.3 < /version & gt;
& lt/scope & gt; Test & lt/scope >
& lt/dependency & gt;
7. Transfer dependencies and simplify dependency management.
In Maven 1, you need to list the packages required by the dependencies. This is too much and inconvenient for users who use things like Hibernate. Transport dependency is implemented in Maven2, so that Maven2 will automatically download the packages that Hibernate depends on, and developers only need to care about Hibernate.
Note: Only the dependencies supported by Maven, usually in the form of plug-ins, can get this feature. In addition, for some old plug-ins, transfer dependency may not be supported due to time. For example, at least in Maven 2.0. 1, for Hibernate 2. 1.2, it is still necessary to clearly list the packages that Hibernate 2. 1.2 depends on.