http://trinea.iteye.com/blog/1290898
This article is mainly about the use of Maven , core Concepts (Pom, repositories, Artifact, Build Lifecycle, Goal) Introduction, usage ( Archetype meaning and creation of various projects),maven common parameters and commands , and simple troubleshooting , maven extensions (Eclipse, Cobertura, findbugs, plug-in development),maven configuration .
This article is longer, can be based on individual needs to have a selective view, such as first look at the usage and then look back to the core concept
1. Use of Maven
MAVEN is a project building and management tool that provides help in managing builds , documentation , reporting , dependencies ,scms , publish , and distribute the method. It is easy to compile code, manage dependencies, manage binaries, and more.
The benefit of MAVEN is the ability to standardize, automate, and efficiently integrate project processes with strong scalability
With Maven itself and its plug-ins, you can also get code review reports, unit test coverage, continuous integration, and more.
2, the core concept of MAVEN Introduction
2.1 Pom
Pom refers to the project object Model. The POM is an XML, which is pom.xml in Maven2. Is the basis of MAVEN work, when executing a task or goal, MAVEN will go to the project root to read Pom.xml get the required configuration information
The pom file contains information about the project and the configuration information required by the MAVEN build project , typically with project information (such as version, member), Project dependencies, plugins and goal, build options, and so on
Pom can be inherited , usually for a large project or multiple module cases, the submodule Pom needs to specify the parent module of the POM
The node in the Pom file is
Java code
- Top-level elements of the project Pom file
- The object model version used by modelversion, which is mandatory to ensure stable use. No modifications are required unless MAVEN developer upgrades the template
- GroupId is a unique identifier for a project to create a community or organization, usually a domain name write-down, such as GroupId org.apache.maven.plugins is reserved for all Maven plugins
- Artifactid is the unique site name of the project artifact
- Packaging artifact packaging methods such as jar, war, ear, etc. The default is jar. This is not only the file that indicates what suffix the project ultimately produces, but also the <a target="_blank" href= "http://maven.apache.org/guides/" that the build process uses. Introduction/introduction-to-the-lifecycle.html#built-in_lifecycle_bindings ">lifecycle</a>.
- Versions of version artifact, which are usually seen for similar 0.0. 1-snapshot, where SNAPSHOT represents a development version of the project
- Name represents the name of the project and is used in the MAVEN-generated document
- The URL represents the address of the project and is used in the MAVEN-generated document
- Description represents a description of the project, which is used in a MAVEN-generated document
- Dependencies indicates dependencies, adding GroupID Artifactid and version that are dependent on the child node dependencies
- Build represents the build configuration
- Parent indicates that the Father Pom
One of the groupId:artifactId:version only identified a artifact
2.2 Artifact
This is a little bit difficult to explain, roughly speaking is a project will be produced by the file, can be jar files, source files, binaries, war files, even pom files. Each artifact is uniquely identified by an identifier consisting of groupId:artifactId:version . Artifact that need to be used (dependent) are placed in the warehouse (see repository)
2.3 Repositories
The repositories is used to store artifact . If the artifact of our project is a small tool, then repositories is a warehouse, which has our own tools to create, we can also store other built tools, we need to use a tool in the project, in the Pom declaration dependency, The code will be compiled according to dependency to download the tool (Artifact)for your own use.
You can put the project into the warehouse (repositories) with the MVN install command after you have completed your project
Warehouses are divided into local warehouses and remote warehouses, which are the warehouses used to store artifact on remote servers, local warehouses refer to the warehouses of native storage artifact, and for Windows machine local warehouse addresses for system users. M2/repository below.
For required dependencies, add dependency in the Pom and search in Maven's warehouse: http://mvnrepository.com/
2.4 Build Lifecycle
Refers to the process of a project build. MAVEN's build lifecycle is divided into three categories, default(the deployment of processing projects), clean (processing project cleanup), andsite(document generation for processing projects). They all contain different lifecycle.
Build lifecycle is made up of phases , which focuses on the default build lifecycle several important phase
XML code
- Validate verify that the project is correct and that the necessary information is available
- Compile compiling source code
- Test tests the compiled code, which executes the unit test code
- Package compiles the compiled code to generate the packages file under the target directory
- Integration-test Process the package so that it can be deployed to an integrated test environment when needed
- Verify test package is effective and meets quality standards
- Install the package to a local warehouse for use in other local projects
- Deploy deployment, copy the final package to the remote repository and develop this or project share for him, complete in the integration or release environment
The above phase is ordered (note that the actual two adjacent phase between the other phase is omitted, complete phase see Lifecycle), the following phase must be performed after the previous phase is completed
If a phase is a direct goal, the phase before it is executed, such as MVN install
Will first validate, compile, test, package, Integration-test, verify and then execute the install phase
2.5 Goal
Goal represents a specific task
XML code
- A goal represents a specific task (finer than a build phase) which contributes to the building and managing of a project.
Such as
The MVN package represents a packaged task, and we know from the above that the execution of this task is performed before the phase phase
MVN deploy represents a deployed task
Mven clean install indicates that the clean phase (containing the other sub-phase) is executed before the install phase.
3. MAVEN Usage
Mainly about the archetype and several common items of the creation
MAVEN creates the project based on the archetype (prototype). Let's introduce the following archetype
3.1 Archetype
prototype for the role of the project is equivalent to the role of the tool , we want to make a hammer, pour molten iron into the mold, a little modification can be.
Like we can create a project with different archetype depending on the type of project you want. With archetype we can create projects quickly and standard . The standard folder directory structure is available after the project is created with archetype
Since archetype equivalent to mold, then of course, you can rebuild the mold, ah, create archetype
The following describes creating a project with a centralized archetype that comes with Maven. Create a project with a goal of MVN archetype:generate, and specify Archetypeartifactid, where Archetypeartifactid see Maven's own Archetypeartifactid
3.2 Quick Start Project
Create a simple Quick Start project that specifies -darchetypeartifactid as Maven-archetype-quickstart, such as the following command
XML code
- MVN archetype:generate -dgroupid=com.trinea.maven.test -dartifactid=maven-quickstart - darchetypeartifactid=Maven-archetype-quickstart -dinteractivemode=false
Where dgroupid specifies groupid,dartifactid specifies artifactid,darchetypeartifactid to specify Archetypeid,
Dinteractivemode indicates whether to use interactive mode, the interaction mode will let the user fill in the version information and the like, non-interactive mode with default values
This allows us to build a simple MAVEN project with the following directory structure:
Now we can use 2.4 of the build lifecycle to do some operations, first command line to the project root directory
Compiling MVN Compile
Packaged MVN package, the Maven-quickstart-1.0-snapshot.jar file appears under the target directory, which is the package file
Package and install to the local warehouse mvn install, at this time the native warehouse will add Maven-quickstart-1.0-snapshot.jar files.
3.3 Web Engineering
Create a simple Web project, only need to repair -darchetypeartifactid for Maven-archetype-webapp , such as the following command
Java code
- MVN archetype:generate-dgroupid=com.trinea.maven.web.test-dartifactid=maven-web-darchetypeartifactid= Maven-archetype-webapp-dinteractivemode=false
Other:
src\main\resources folder is used to store resource files , MAVEN project does not have the resources folder by default, if we need to use a configuration file like log4j.properties, we need to src\ Create a new resources folder under the main folder and put the log4j.properties in it.
Test need to use resource file, similar to put under Src\test
For Apache log4j There is no log4j.properties file or directory error, the following exception will be reported
XML code
- Log4j:warn No Appenders could is found for logger (org.apache.commons.httpclient.HttpClient).
- Log4j:warn Initialize the log4j system properly.
4. Maven Common parameters and commands
Mainly describes maven common parameters and commands and simple troubleshooting
4.1 MVN Common Parameters
MVN-E Show Detailed errors
Mvn-u force update of snapshot type plug-ins or dependent libraries (otherwise MAVEN will only update snapshot dependencies once a day)
Mvn-o run offline mode, non-networked update dependent
Mvn-n execute commands only in the current project module, close reactor
MVN-PL module_name executing commands on the specified module
Mvn-ff quits directly when an error occurs during a recursive execution of a command
Mvn-dxxx=yyy specifying Java Global Properties
Mvn-pxxx Reference profile XXX
4.2 First 2.4 Build L commands that are described in Ifecycle
MVN test-compile compiling test code
Unit tests in the MVN test run program
MVN Compile compiling the project
MVN package, the Maven-quickstart-1.0-snapshot.jar file appears under the target directory, which is the package file
MVN install is packaged and installed to the local warehouse, the native repository will add maven-quickstart-1.0-snapshot.jar files.
Each phase can be used as a goal, or can be combined, as previously described in MVN clean install
4.3 maven daily Kick
MVN archetype:generate Creating a Maven project
MVN package, already covered.
MVN package-prelease Packaging, and generating packages for deployment, such as Deploy/*.tgz
MVN install packaged and installed to a local library
MVN eclipse:eclipse generate Eclipse project files
MVN Eclipse:clean purge Eclipse project files
MVN site to generate project-related information
4.4 Maven Plugin Common parameters
Mvn-dwtpversion=2.0 specifying the MAVEN version
Mvn-dmaven.test.skip=true If the command contains test phase, the unit test is ignored
Mvn-duserprop=filepath specifying a user-defined profile location
Mvn-ddownloadsources=true-declipse.addversiontoprojectname=true Eclipse:eclipse Build the Eclipse project file and try to download the source code from the repository, And the resulting project contains the module version (note that the above switch is turned on by default if using a public pom)
4.5 maven Simple Troubleshooting
Mvn-dsurefire.usefile=false If an error occurs in the execution of a unit test, the command can output failed unit tests and related information in the console
Set maven_opts=-xmx512m-xx:maxpermsize=256m large JVM memory and persistent generation, MAVEN/JVM out of the memories error
Mvn-x maven Log level is set to debug at run
Mvndebug run JPDA allows remote debug
MVN--help This will not say.
5. maven Extension
Maven common plug-in configuration and use
6. Maven Configuration
To modify the JDK version of Maven's creation of a project since default, see Maven configuration, refer to http://www.iteye.com/topic/41612
maven2.0 default use of jdk1.5 results in introspection, @override, etc. annotation not available. Two ways to modify JDK versions
The first kind: Modify the pom.xml of the project, affect the individual project, the symptom does not cure
Java code
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- <encoding>utf-8</encoding>
- </configuration>
- </plugin>
- </plugins>
- </build>
Add build configuration in Pom, specify Java version to 1.6
Second: Modify the MAVEN configuration to affect all MAVEN-built projects
To the MAVEN installation directory under the Conf folder, modify the Settings.xml file as follows:
Java code
- <profiles>
- <profile>
- <id>jdk-1.6</id>
- <activation>
- <activeByDefault>true</activebydefault>
- <jdk>1.6</jdk>
- </activation>
- <properties>
- <maven.compiler.source>1.6</maven.compiler.source>
- <maven.compiler.target>1.6</maven.compiler.target>
- <maven.compiler.compilerVersion>1.6</maven.compiler.compilerversion>
- </properties>
- </profile>
- </profiles>
This allows you to specify a JDK of 1.6 for all default MAVEN projects
Stop, take a break.
Resources:
MAVEN Official Document: http://maven.apache.org/guides/index.html
MAVEN's authoritative guide: http://www.sonatype.com/books/maven-book/reference_zh/public-book.html
Maven Install: http://maven.apache.org/download.html
Http://www.infoq.com/cn/search.action?queryString=maven%E5%AE%9E%E6%88%98&searchOrder=relevance&search =maven%e5%ae%9e%e6%88%98
MAVEN uses, core concepts, usage, common parameters and commands, extensions