Basic principles and new features of Maven

Source: Internet
Author: User
Tags checkstyle

Use Maven for project management
In the Java World, many developers choose ant to build a project. XML can complete compilation, testing, packaging, deployment, and many other tasks, but we also encounter many problems, such as chaotic JAR file management, various project structures and build. XML is quite different. The emergence of Maven provides more support for the project.
1 Overview
Maven, as an open-source project of Apache, aims to provide more support for project management, with the home address as a http://maven.apache.org. It was originally intended to provide unified development, testing, packaging and deployment for several projects organized by Apache, allowing developers to conveniently switch between multiple projects. The starting point of many great undertakings is very simple. eBay is a good example. Maven once again verifies this point, and more projects begin to use Maven.
Basic Principles
The basic principle of Maven is very simple. It uses remote warehouse and Local warehouse and a similar build. XML pom. XML, the Pom. the jar files defined in XML are downloaded from the remote repository to the local repository. Each application uses the jar files of the same local repository. The jar files of the same version only need to be downloaded once, in addition, avoid copying jar files for each application. 1. At the same time, it adopts the current popular plug-in system architecture, with only the smallest core retained, and other functions are provided in the form of plug-ins, so Maven download is very small (1.1 MB). When executing Maven tasks, to automatically download the required plug-in.
This basic principle is very similar to pear-the principles of the PHP extension and application repository are very similar. They all have an official repository, all of which are microkernels, download the required files to the local device through the network, and manage the corresponding class libraries in a unified manner through the official repository. PEAR has become a de facto standard for PHP development, and Maven's position in the Java World is gradually becoming a standard just around the corner.
In addition, due to the ease of Development of plug-ins, there are a lot of plug-ins available for selection, such as plug-ins combined with eclipse and plug-ins related to test, later we will introduce Maven in detail in the project.

Figure 1


2. Project Management

Maven officially defines Maven as a project management tool. Let's take a look at what kind of support Maven can provide to our project.
2.1 Project Standardization
An important feature of Maven is to define the standard template of the project. The official saying is best practice. Run the command MVN archetype: Create-dgroupid = com. mycompany. app-dartifactid = My-app to create a single Maven project. The created Project has a specific project structure. This project structure is based on the best practices in the industry and lays the foundation for the subsequent use of unified Maven commands, such as MVN test testing and MVN package packaging. You do not need to write a script line, you can easily implement multiple functions. Maven also provides other types of templates, which can be created by adding-darchetypeartifactid = {type}, such as-darchetypeartifactid = Maven-Archetype-site to generate site-type projects.
At the same time, some good open-source software uses Maven and can also be used as a good project template, such as equinox (simplified version of appfuse). Its pom defines hiberante, spring... You can use ant New-dapp. Name = Name to quickly generate a project prototype, and then cut the POM file to meet the needs of your project.
With this standard, projects can be easily communicated, and you can easily understand the structure of other Maven projects. For enterprises, the introduction of Maven naturally introduces specifications, which is much more effective than time-consuming and labor-consuming document writing.
2.2 documents and reports
MVN site can be used to quickly generate a project site. Many open-source Apache project sites use Maven to generate the built by Maven icon.
The plug-ins provided by Maven for such as JUnit, checkstyle, and PMD can conveniently perform tests and checks and generate reports directly. It is easy to use. For example, MVN surefire-Report: report can automatically perform the JUnit test and generate the JUnit report. mvn pmd: PMD generates the PMD report. For more information, see http://maven.apache.org/plugins/index.html.
2.3 Class Library Management
A very important feature of Maven is class library management. By defining the jar package version and dependencies in POM. XML, you can easily manage jar files. The following is a simple jar definition snippet:
<Dependency>
<Groupid> org. hibernate </groupid>
<Artifactid> hibernate </artifactid>
<Version> 3.1 </version>
<Type> jar </type>
<Scope> runtime </scope>
</Dependency>

This piece tells us that the dependent jar package groupid is org. hibernate, artifactid is hibernate, version is 3.1, scope is runtime. In the actual project, m2_repo (Maven local repository address)/org/hibernate/3.1/hibernate-3.1.jar is put into classpath.
Maven also manages the dependencies between jar packages through Pom. xml. For example, the above hibernate-3.1.jar directory at the same level will certainly have a hibernate-3.1.pom, In this pom file specified this jar on some other jar dependencies. The pom file is provided by a remote repository and does not need to be modified. When you execute Maven-related commands, the jar package is automatically downloaded based on relevant dependencies. In this way, you only need to define the dependency on Hibernate without worrying about the relevant jar, which makes it much easier to build a project.
Because the POM file manages the jar, it also produces a very attractive feature: the project file is very small. In the past, a web project, Jar files should be put under the WEB-INF/lib, and put into CVS (SVN), it is easy to reach dozens of M. With Maven, only one Pom. xml file is required. When MVN eclipse is executed: eclipse, the file is downloaded from the remote repository. The project file usually takes several hundred kb.
2.4 release management
Maven can be used for convenient project posting management. You can use MVN package to package a project at a certain stage of development. It will automatically run MVN test first and run all testcase. Only when all the packages pass can they be correctly packaged. The generated war package (if the project packaging is war) is in the target directory. This war package is generated using the ant script, but no script is required. This is one of the advantages of Maven over ant. Use MVN install to publish compiled and packaged files to the remote repository specified by distributionmanager. MVN deploy can be used to automatically manage project versions.
 
3. Use Maven
Maven is easy to use, has rich plug-ins and has passed a lot of Project tests, so there is little risk of introduction to the project. The author uses Maven in the project, and new employees can successfully build the project using Maven within half an hour, and quickly master several common commands. Maven does not need any special facilities. If all the projects use open-source software and do not require Maven to manage the project version, you only need an SCM Environment (CVS or SVN) to share the development source code. Set the repository in POM. XML as the official address. The configuration is as follows:
<Repository>
<ID> central </ID>
<URL> http://www.ibiblio.org/maven2 </URL>
</Repository>

This official remote repository has a lot of open-source software and should be able to meet the needs of most projects. If a project requires software that is not yet available on it, or you want to use internal software, you need to configure a repository on your own.
3.1 self-built private servers
Everyone should have heard of the legend private server. An Internet cafe creates a private server and a group of people will kill it for free. The official team has made a lot of money, so they always want to fight. Maven officially supports self-built private servers, and we can finally get started with private servers (springside's boss, Jiangnan white clothing, is a good place to create private servers ). Let's introduce the fastest way to build a private server.
First, you need an HTTP server. Find a server and install Apache on it. Put an empty Maven directory under htdocs. Assume that the server IP address is 192.168.0.1 and you can use http: // 192.168.0.1/Maven to access the directory. Then install maven2 on your machine and initialize the project. put the required jar definition in XML, and run the command such as MVN Eclipse: eclipse to automatically download the jar from the official website to the local directory. If no configuration is made, the local repository is $ {user by default. home }/. m2 /. For Windows XP, generally under C:/Documents and Settings/% username %/. m2, % username % indicates the operating system logon username. At this time, you can see that there is an additional repository directory under $ {user. Home}/. m2/, which contains a lot of project-related jar files. The directory is sorted by groupid/artifactid/version. Copy the entire repository directory to the maven directory of the Apache server. If you need a jar or internal jar that is missing from the official website, copy the JAR file to the maven directory. Private server created!
To use a private server, you only need to modify Pom. xml and add the following after the repository Configuration:
<Repository>
<ID> companyName </ID>
<URL> http: // $ {IP}/Maven </URL>
</Repository>

When downloading the JAR file, it will first go to the local repository to view it. If it has not been downloaded, it will go to the official search. If it has not been found, it will go to the subsequent repository search.
3.2 install and configure
Maven installation is simple:
1. Download the maven installation package from the Apache website, which is actually a compressed file. Decompress it to the directory where you want to store Maven. Here let's assume that you decompress Maven to C:/program files/Apache Software Foundation/maven-2.0.4.
2. Set C:/program files/Apache Software Foundation/maven-2.0.4/bin to your system environment variable % PATH %.
3. Make sure that you have set your JDK installation directory to the java_home system variable.
4. Open a command line window and run MVN-version to verify whether the installation is successful.
5. if you use eclipse, after running MVN Eclipse: eclipse to generate the Eclipse project file, you need to add m2_repo to classpath variables and point to the local repository, for example: C:/Documents and Settings/owner /. m2/Repository.
3.3 basic procedure
In the project process, no configuration is required. In the Pom. xml directory at the same level, CMD uses a simple Maven command to complete the entire process of initialization-> development-> test-> release. The basic usage process of Maven is as follows:

 
 

When using the command MVN eclipse for the first time: Eclipse, many plug-ins and jar will be downloaded, which may take a long time. We recommend that you have a cup of coffee and blow your finger with your colleagues. If it fails, it may be a network problem. You can run MVN Eclipse: Eclipse again. The downloaded plug-ins and jar files will not be downloaded. Run commands such as MVN test and MVN package to store the output in the target directory. If there is any problem in use, you can also contact me, mail: pesome@gmail.com, let's try to solve.
 
Summary:
Maven, as a new project management tool, is easy to use and can provide strong support for various stages of the project. At the same time, due to its scalable plug-in architecture, more and more plug-ins have emerged, it also makes its functions more powerful. In a short article, it is really difficult to describe its features one by one. MAVEN also has many useful functions and plug-ins waiting for us to use and develop. The best way to learn new technologies is practice. If you are interested, download the latest version as soon as possible to start your Maven journey!
 
 
Reference:
1. http://maven.apache.org official website documentation
2. http://www-128.ibm.com/developerworks/cn/opensource/os-maven2/ maven2 New Features
It has been a long time since Maven appeared, and the feeling of getting familiar with it is still clearly printed in my mind. Now, the idea of empathy with Maven from ant is actually very simple, because Maven can present project-related information in the form of a website, such as a list of developers and various reports. This method brings great convenience to the construction of the project, especially the report. Imagine how annoying each time you switch directories without a unified portal for the JUnit-report, javadoc, checkstyle, and PMD reports generated!

Maven is undoubtedly quite successful, as we can see from the increasing number of open-source projects that use Maven. The reason for Maven's success is simple: while simplifying the build script, the function has not shrunk, but has been enhanced; tools that collect project information are provided and presented in a friendly manner; multiple plug-ins simplify the work. The emergence of such powerful tools is naturally a competition for use.

New Features

Now maven2 has been launched. MAVEN official website says that maven2 is a great change from maven1 to maven1, and even sacrifices compatibility to achieve this goal. (For the sake of maven1 users, maven1 continues his mission .) What is the result of such a big change?

1. faster and simpler

Compared with maven1, maven2 has made a qualitative leap in speed, and even is inferior to ant (of course, download is not counted ). In addition, "Simplifying work and using industry-recognized best practices" is another major topic of maven2. other new features are everywhere in maven2's efforts to simplify work.

2. Fewer configuration files

Comparison between maven1 and maven2 configuration files:

Maven1: Project. XML, Maven. XML, project. properties, and build. properties.
Maven2: 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 also been upgraded from 3 to 4. For projects, only Pom. XML is required.

Maven. XML is not required or recommended in maven2 for the following reasons:

The ease of use of plugin is enhanced.
The content distributed in maven. XML is difficult to share among different projects and is not conducive to maintenance. In maven2, we recommend that you use a custom plugin to encapsulate the content.
If you still want to use features similar to maven. XML, such as <pregoal>, see 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 login users. Generally in $ home/. m2/, for Windows users, the directory is: C:/Documents and Settings/user name/. m2/settings. xml.
Global level: Generally, in % m2_home %/CONF/settings. XML, m2_home is the name of the environment variable in the root directory of maven2.
You can configure settings. XML, such as local repository and proxy. The settings. XML structure can be obtained from the official Maven website.

3. Change the plugin Language

In maven2, the plug-in language was changed from jelly to Java and beanshell. Java is faster, and developers are more familiar with it. For other popular scripts such as groovy and Maven official websites, consider it again when it becomes more mature.

.
4. Provide predefined directory templates

A good directory structure makes it easier for developers to understand projects and lay a good foundation for future maintenance work. Maven2 provides developers with a default standard directory template based on the industry-recognized best directory structure. The standard directory structure of maven2 is as follows:

 

You can use directory templates to make Pom. XML more concise. Because maven2 has predefined related actions based on the default directory, without manual intervention. Take the resources directory as an example:

Src/main/resources, responsible for managing the resources of the project owner. After using maven2 to execute compile, all files and subdirectories in this directory will be copied to the target/classes Directory, which provides convenience for future packaging.
Src/test/resources, responsible for managing the resources for project testing. After using maven2 to execute test-compile, all files and subdirectories in this directory will be copied to the target/test-classes directory to prepare for subsequent tests.
In maven1, these actions must be completed using <pregoal> or <postgoal> In Maven. xml. Currently, it can be completed automatically without being specified in POM. xml. Resources are used in both SRC and test to facilitate building and testing. This method is already previous experience. By using maven2, this experience has been popularized in the development team.

To create a standard directory template, run the following command:

 

MVN archetype: Create-dgroupid = com. codeline. commons-dartifactid = codelinecommons
 

The meanings of groupid and artifactid are the same as those in maven1. The value of the artifactid parameter is used as the name of the project root directory. In addition to creating the corresponding directory, maven2 will also create the default Pom. xml.

Maven2 also considers that different types of projects require different directory structures. To create a web project, run the following command:

 

MVN archetype: Create-dgroupid = com. mycompany. app
-Dartifactid = My-webapp
-Darchetypeartifactid = Maven-Archetype-webapp
 

5. Introduction of lifecycle

Maven2 has a clear concept of life cycle and provides corresponding commands, making the project build clearer and clearer. Main lifecycle phases:

Verify that the project is correct and all required resources are available.
Compile the source code of the project.
Test-compile: compile the project test code.
Test: Use the compiled test code to test the compiled source code.
Package: A published format, such as jar, which packages compiled source code.
Integration-test: process and release packages in an environment where the integration test can run.
Verify, run any check to verify whether the package is valid and meets quality standards.
Install the package in the local repository, which can be used by other projects as dependencies.
Deploy is executed in the integrated or released environment. The final version package is copied to a remote repository, so that other developers or projects can be shared.
Generate-sources to generate any additional source code required by the application, such as XDoclet.
If you want to compile the project, enter MVN Compile directly. For other stages, you can do so. There is a dependency between stages, for example, test depends on test-compile. When executing MVN test, the MVN test-compile command is run first, and then the MVN test command is run.

6. added the dependency scope.

In pom 4, <dependency> also introduces <scope>, which mainly manages the deployment of dependencies. Currently, <scope> can use five values:

Compile, default value, applicable to all stages and will be released along with the project.
Provided, similar to compile, expects JDK, container, or user to provide this dependency. For example, Servlet. jar.
Runtime, used only at runtime, such as the JDBC driver, applicable to the running and testing stages.
Test, which is only used for testing and is used to compile and run the test code. Will not be released along with the project.
System, similar to provided, must explicitly provide jar containing dependencies, MAVEN will not find it in repository.
<Scope> example:

 

<Dependency>
<Groupid> hibernate </groupid>
<Artifactid> hibernate </artifactid>
<Version> 3.0.3 </version>
<Scope> test </scope>
</Dependency>
 

7. Transfer dependencies to simplify dependency management

In maven1, you must also list the packages required for dependency. This is too complicated and inconvenient for users like hibernate. In maven2, dependencies are passed. For the packages that hibernate depends on, maven2 will automatically download and developers only need to care about hibernate.

Note: This feature can be obtained only when the dependency supported by Maven is usually displayed in the form of plugin. In addition, some old plug-ins may not support passing dependencies due to the time relationship. For example, 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 depends on.

 

 

 

Introduction

The steps for installing maven2 are very simple: first download the corresponding software package from the official Maven website, which is currently Maven 2.0.1; then decompress the package and set the environment variable m2_home = maven2 to unzip the installation directory; finally, add % m2_home %/bin to path to facilitate Maven to run in any directory.

The running command of maven2 is MVN, and MVN-h can be used to obtain related help information. Common scenarios:

Create a Maven project: MVN archetype: Create
Compile source code: MVN compile
Compile the test code: MVN test-compile
Run the test: MVN Test
Site: MVN site
Package: MVN package
Install jar: MVN install in local repository
Clear the generated project: MVN clean
Perhaps because of the launch, maven2 is still somewhat unsatisfactory. In particular, some plug-ins in the report section are not available yet, such as JUnit-report. Some are some inexplicable problems, such as checktyle and PMD, which cannot work normally in the local locale. For example, the following exception is thrown when a message 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, maven2 has received much attention as soon as it appears, and such problems should be solved quickly.

 

Conclusion

Based on maven1's advantages, maven2 has made another huge step forward. These new features greatly reduce the workload in development management, allowing developers to focus on actual business issues. These new features also play a positive role in simplifying usage and popularizing best practices.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/it_man/archive/2006/10/23/1346604.aspx

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.