English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Maven has the following three standard lifecycles:
clean: Project cleanup processing
default( or build): Project deployment processing
site: Project site document creation processing
Each lifecycle contains a series of phases (phases). These phases are equivalent to the unified interfaces provided by Maven, and the implementation of these phases is completed by Maven plugins.
When we enter the mvn command, for example mvn clean, clean corresponds to the clean phase of the Clean lifecycle. However, the specific operation of clean is by maven-clean-plugin to implement.
Therefore, the specific implementation of each stage of the Maven lifecycle is actually implemented by Maven plugins.
Maven is actually a framework that executes dependency plugins, and each task is actually completed by plugins. Maven plugins are usually used for:
Create jar file
Create war file
Compile code files
Code unit test
Create project documentation
Create project report
Plugins usually provide a set of goals and can be executed using the following syntax:
<code>mvn [plugin-name]:[goal-name]/code>
For example, a Java project can use maven-compiler-compile goal of the plugin-goal Compile, use the following command:
<code>mvn compiler:compile</code>
Maven provides the following two types of plugins:
Type | Description |
---|---|
Build plugins | Executed during the build process and configured in the <elements> element of pom.xml. |
Reporting plugins | Executed during the website generation process and configured in the <elements> element of pom.xml. |
Below is a list of some commonly used plugins:
Plugin | Description |
---|---|
clean | Clean up target files after building. Delete the target directory. |
compiler | Compile Java source files. |
surefile | Run JUnit unit tests. Create test reports. |
jar | Build a JAR file from the current project. |
war | Build a WAR file from the current project. |
javadoc | Generate Javadoc for the project. |
antrun | to run a set of ant tasks from any stage of the build process. |
We have used it extensively in our example maven-antrun-plugin to output data to the console. Please see Maven - Build configuration file Let's understand this section in a better way. Create a pom.xml file in the C:\MVN\project directory.
<project xmlns="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>/groupId> <artifactId>project/artifactId> <version>1.0/version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId>/groupId> <artifactId>maven-antrun-plugin/artifactId> <version>1.1</version> <executions> <execution> <id>id.clean</id>/id> <phase>clean</phase>/phase> <goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>clean phase</echo>/echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Next, open the command terminal and navigate to the directory where pom.xml is located, and execute the following mvn command.
mvn clean
Maven will start processing and display the clean phase of the clean lifecycle.
[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-}] [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 demonstrates the following key concepts:
Plugins are defined using the plugins element in pom.xml.
Each plugin can have multiple goals.
You can define phases, and the plugin will start processing using its phase element. We have used clean phase.
You can configure the task to be executed by binding it to the target of the plugin. We have bound echo task to maven-antrun-of the plugin run Target.
That's it, Maven will handle the rest. It will download plugins that are not available in the local repository and start processing.