Maven has three standard lifecycles:
clean : handling of project cleanup
default(或 build) Processing of project deployment
site Processing of project site document creation
Each lifecycle contains a series of phases (phase). These phase are equivalent to the unified interface provided by Maven, and then the implementation of these phase is done by Maven plug-ins.
When we enter the mvn command, such as mvn clean,clean, it corresponds to the clean phase in the Clean life cycle. But the specific operation of clean is made up of maven-clean-plugin To make it happen.
So the concrete implementation of each phase of the Maven life cycle is implemented by the Maven plug-in.
Maven is actually a framework that relies on plug-ins for execution, and each task is actually done by plug-ins. The Maven plug-in is usually used to:
Create a jar file
Create a war file
Compile code file
Code unit test
Create a project document
Create a project report
Plug-ins usually provide a collection of targets and can be executed using the following syntax:
<code>mvn [plugin-name]:[goal-name]</code>
For example, a Java project can be compiled using maven-compiler-plugin ‘s compile-goal, using the following command:
<code>mvn compiler:compile</code>
11.7.1. Plug-in type ¶
Maven provides the following two types of plug-ins:
Types | Description |
|---|---|
Build plugins | It is executed at build time and configured in the element of pom.xml. |
Reporting plugins | It is executed during the site generation process and configured in the element of pom.xml. |
Here is a list of some common plug-ins:
Plug-in | Description |
|---|---|
Clean | Clean up the target file after building. Delete the destination directory. |
Compiler | Compile the Java source file. |
Surefile | Run the JUnit unit test. Create a test report. |
Jar | Build the JAR file from the current project. |
War | Build the WAR file from the current project. |
Javadoc | Generate Javadoc for the project. |
Antrun | Run a collection of ant tasks from any stage of the build process. |
11.7.2. Example ¶
We’ve used it a lot in our example. maven-antrun-plugin To output the data to the console. Please check Maven - 构建配置文件 Chapter. Let’s understand this section in a better way and create a pom.xml file in the C:MVNproject directory.
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.companyname.projectgroup</groupId><artifactId>project</artifactId><version>1.0</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.1</version><executions><execution><id>id.clean</id><phase>clean</phase><goals><goal>run</goal></goals><configuration><tasks><echo>clean
phase</echo></tasks></configuration></execution></executions></plugin></plugins></build></project>
Next, open the command terminal and jump to the directory where pom.xml is located, and execute the following mvn command.
mvn clean
Maven will begin to process and display the clean phase of the clean life cycle.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO] task-segment: [post-clean]
[INFO] ------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] [antrun:run {execution: id.clean}]
[INFO] Executing tasks
[echo] clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Sat Jul 07 13:38:59 IST 2012
[INFO] Final Memory: 4M/44M
[INFO] ------------------------------------------------------------------
The above example shows the following key concepts:
Plug-ins are defined using the plugins element in pom.xml.
Each plug-in can have multiple goals.
You can define the phase, and the plug-in will use its phase element to start processing. We’ve used it. clean Stage.
You can configure the tasks to be performed by binding to the target of the plug-in. We’re already bound. echo Task to maven-antrun-plugin run target.
That’s it. Maven will take care of the rest. It will download plug-ins that are not available in the local repository and start processing.