English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Maven build lifecycle defines the process of project construction and release.
A typical Maven build (build) lifecycle is composed of a sequence of the following stages:
Stage | Processing | Description |
---|---|---|
Validate validate | Validate the project | Validate that the project is correct and all required information is available |
Compile compile | Execute compilation | Source code compilation is completed in this stage |
Test Test | Test | Run tests using an appropriate unit testing framework (such as JUnit). |
Packaging package | Packaging | Create JAR/WAR package if mentioned in the pom.xml definition |
Check verify | Check | Check the results of integrated test to ensure the quality meets the standard |
Installation install | Installation | Install the packaged project into the local repository for use by other projects |
Deployment deploy | Deployment | Copy the final project package to the remote repository to share with other developers and projects |
To complete the default lifecycle, these stages (including other lifecycle stages not listed above) will be executed in order.
Maven has the following three standard lifecycles:
clean: Handling project cleanup
default (or build): Handling project deployment
site: Handling the creation of project site documentation
A plugin goal represents a specific task (more fine-grained than build stages), which helps in the construction and management of the project. These goals may be bound to multiple stages or not bound at all. Goals not bound to any build stage can be executed outside the build lifecycle by direct invocation. The execution order of these goals depends on the order of invocation and the build stage.
For example, consider the following command:
clean and pakage are build stages, dependency:copy-dependencies is the target
mvn clean dependency:copy-dependencies package
The clean stage here will be executed first, followed by dependency:copy-The target dependencies will be executed, and finally, the package stage will be executed.
When we execute mvn post-When the clean command is executed, Maven calls the clean lifecycle, which includes the following stages:
pre-clean: perform some work that needs to be completed before clean
clean: remove all files generated by the last build
post-clean: perform some work that needs to be completed immediately after clean
In mvn clean, clean refers to the above clean. In a lifecycle, when a stage is executed, all stages before it will also be executed, that is, if mvn clean is executed, the following two lifecycle stages will be run:
pre-clean, clean
If we run mvn post-If clean is executed, the following three lifecycle stages will be run:
pre-clean, clean, post-clean
We can modify the operation behavior of this part by defining a goal at any stage of the above clean lifecycle.
In the following example, we will use maven-antrun-plugin:run target added to pre-clean, clean, and post-in the clean phase. This allows us to display text information at each stage of the clean lifecycle.
We have created 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> <artifactId>project</artifactId> <version>1.0<//version> </build> </plugins> </plugin> <groupId>org.apache.maven.plugins</groupId/groupId> <artifactId>maven</artifactId-antrun-plugin>/artifactId> <version>1.1</version> <executions> <execution> <id>id.pre</id-clean</id> <phase>pre</phase-clean</phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>pre</echo-clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.clean</id> <phase>clean</phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post</id-clean</id> <phase>post</phase-clean</phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>post</echo-clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Now open the command console, navigate to the directory where pom.xml is located, and execute the following mvn command.
C:\MVN\project>mvn post-clean
Maven will start processing and display all stages 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] [antrun:run {execution: id.pre-clean}] [INFO] Executing tasks [echo] pre-clean phase [INFO] Executed tasks [INFO] [clean:clean {execution: default}-clean}] [INFO] [antrun:run {execution: id.clean}] [INFO] Executing tasks [echo] clean phase [INFO] Executed tasks [INFO] [antrun:run {execution: id.post}-clean}] [INFO] Executing tasks [echo] post-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] ------------------------------------------------------------------
You can try modifying the mvn clean command to display pre-clean and clean, and in post-No operation is performed in the clean phase.
This is Maven's main lifecycle, used to build applications, including the following. 23 Phase:
Lifecycle phase | Description |
---|---|
validate (validate) | Validate whether the project is correct and all necessary information can complete the project build process. |
initialize (initialize) | Initialize the build status, such as setting property values. |
generate-sources (generate source code) | Generate any source code included in the compilation phase. |
process-sources (processing source code) | Process the source code, for example, filter any values. |
generate-resources (generate resource files) | Generate resource files that will be included in the project package. |
process-resources (processing resource files) | Copy and process resources into the target directory, preparing for the packaging phase. |
compile (compile) | Compile the source code of the project. |
process-classes (processing class files) | Process the files generated by compilation, for example, perform bytecode enhancement optimization on Java class files. |
generate-test-sources (generate test source code) | Generate any test source code included in the compilation phase. |
process-test-sources (processing test source code) | Process the test source code, for example, filter any values. |
generate-test-resources (generate test resource files) | Create resource files for testing. |
process-test-resources (processing test resource files) | Copy and process test resources into the target directory. |
test-compile (compile test source code) | Compile the test source code into the test target directory. |
process-test-classes (processing test class files) | Process the files generated by the test source code compilation. |
test (testing) | Run tests using a suitable unit test framework (JUnit is one of them). |
prepare-package (preparation for packaging) | Execute any necessary operations to prepare for packaging before the actual packaging. |
package (packaging) | Package the compiled code into a distributable file format, such as JAR, WAR, or EAR files. |
pre-integration-test (before integration test) | Perform necessary actions before the integration test is executed. For example, set up the required environment. |
integration-test (integration test) | Process and deploy the project to an environment where integration tests can be run. |
post-integration-test (after integration test) | Perform necessary actions after the integration test is completed. For example, clean up the integration test environment. |
verify (verification) | Run any checks to verify that the project package is valid and meets quality standards. |
install (installation) | Install the project package to the local repository so that the package can be used as a dependency for other local projects. |
deploy (deployment) | Copy the final project package to the remote repository to share it with other developers and the project. |
有一些与 Maven 生命周期相关的重要概念需要说明:
当通过 Maven 命令调用一个阶段时,例如 mvn compile,只有该阶段之前以及包括该阶段在内的所有阶段会被执行。
不同的 Maven 目标将根据打包的类型(JAR / WAR / EAR),被绑定到不同的 Maven 生命周期阶段。
In the following example, we will use maven-antrun-插件:运行 目标被添加到构建生命周期的一部分阶段中。这样我们可以显示生命周期的文本信息。
We have updated the pom.xml file under 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> <artifactId>project</artifactId> <version>1.0<//version> </build> </plugins> </plugin> <groupId>org.apache.maven.plugins</groupId/groupId> <artifactId>maven</artifactId-antrun-plugin>/artifactId> <version>1.1</version> <executions> <execution> <id>id.validate</id> <阶段>验证</phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>验证阶段</echo> </tasks> </configuration> </execution> <execution> <id>id.compile</id> <阶段>编译</phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>编译阶段</echo> </tasks> </configuration> </execution> <execution> <id>id.test</id> <阶段>测试</phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>测试阶段</echo> </tasks> </configuration> </execution> <execution> <id>id.package</id> <阶段>打包</phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>打包阶段</echo> </tasks> </configuration> </execution> <execution> <id>id.deploy</id> <阶段>部署</phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>部署阶段</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Now open the command console, navigate to the directory where pom.xml is located, and execute the following mvn command.
C:\MVN\project>mvn compile
Maven 将开始处理并显示构建生命周期的各个阶段,直到编译阶段。
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0 [INFO] task-段: [编译] [INFO] ------------------------------------------------------------------ [INFO] [antrun:运行 {执行:id.validate}] [INFO] Executing tasks [echo] 验证阶段 [INFO] Executed tasks [INFO] [资源:资源 {执行:默认}-resources}] [WARNING] 使用平台编码 (Cp1252 实际上)要复制过滤后的资源, 即构建是平台依赖的! [INFO] 跳过不存在的资源目录 C:\MVN\project\src\main\resources [INFO] [编译器:编译 {执行:默认}-compile}] [INFO] 没有需要编译的内容 - 所有类都是最新的 [INFO] [antrun:run {execution: id.compile}] [INFO] Executing tasks [echo] compile phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------ [INFO] Total time: 2 seconds [INFO] Finished at: Sat Jul 07 20:18:25 IST 2012 [INFO] Final Memory: 7M/64M [INFO] ------------------------------------------------------------------
In the development environment, use the following command to build and install the project into the local repository
mvn install
This command executes the stages of the default lifecycle (validate, compile, package, etc.) in order before executing the install phase, and we only need to call the last stage, such as install here.
In the build environment, use the following call to cleanly build and deploy the project to the shared repository
mvn clean deploy
This command can also be used in the case of multi-module projects, that is, projects that contain multiple sub-projects. Maven will execute the clean command for each sub-project, and then execute the deploy command.
The Maven Site plugin is generally used to create new report documents, deploy sites, etc.
pre-site: Execute some work that needs to be completed before generating the site documents
site: Generate the project's site documents
post-site: Execute some work that needs to be completed after generating the site documents, and prepare for deployment
site-deploy: Deploy the generated site documents to a specific server
The site phase and site-deploy phase to generate and publish the Maven site, which is a very powerful feature of Maven, favored by the Manager. Documents and statistics are automatically generated, looking very nice. In the following example, we will use maven-antrun-The plugin:run target is added to all stages of the Site lifecycle. This allows us to display all text information of the lifecycle.
We have updated the pom.xml file under 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> <artifactId>project</artifactId> <version>1.0<//version> </build> </plugins> </plugin> <groupId>org.apache.maven.plugins</groupId/groupId> <artifactId>maven</artifactId-antrun-plugin>/artifactId> <version>1.1</version> <executions> <execution> <id>id.pre</id-site</phase/id> <phase>pre</phase-site</phase/phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>pre</echo-site phase</execution/echo> </tasks> </configuration> </execution> <execution> <id>id.site</id/id> <phase>site</phase/phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>site phase</echo/echo> </tasks> </configuration> </execution> <execution> <id>id.post</id-site</phase/id> <phase>post</phase-site</phase/phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>post</echo-site phase</execution/echo> </tasks> </configuration> </execution> <execution> <id>id.site</id-deploy</phase/id> <phase>site</phase-deploy</phase/phase> </goals> <goal>run</goal>/goal> </goals> <configuration> <tasks> <echo>site-deploy phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Now open the command console, navigate to the directory where pom.xml is located, and execute the following mvn command.
C:\MVN\project>mvn site
Maven will start processing and display each stage of the site lifecycle until the site phase.
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------ [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0 [INFO] task-segment: [site] [INFO] ------------------------------------------------------------------ [INFO] [antrun:run {execution: id.pre-site}] [INFO] Executing tasks [echo] pre-site phase [INFO] Executed tasks [INFO] [site:site {execution: default-site}] [INFO] Generating "About" report. [INFO] Generating "Issue Tracking" report. [INFO] Generating "Project Team" report. [INFO] Generating "Dependencies" report. [INFO] Generating "Project Plugins" report. [INFO] Generating "Continuous Integration" report. [INFO] Generating "Source Repository" report. [INFO] Generating "Project License" report. [INFO] Generating "Mailing Lists" report. [INFO] Generating "Plugin Management" report. [INFO] Generating "Project Summary" report. [INFO] [antrun:run {execution: id.site}] [INFO] Executing tasks [echo] site phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------ [INFO] Total time: 3 seconds [INFO] Finished at: Sat Jul 07 15:25:10 IST 2012 [INFO] Final Memory: 24M/149M [INFO] ------------------------------------------------------------------```