📅  最后修改于: 2020-11-12 04:52:08             🧑  作者: Mango
构建生命周期是一个定义明确的阶段序列,这些阶段定义了目标执行的顺序。这里的阶段代表生命周期的一个阶段。例如,典型的Maven Build Lifecycle由以下阶段序列组成。
Phase | Handles | Description |
---|---|---|
prepare-resources | resource copying | Resource copying can be customized in this phase. |
validate | Validating the information | Validates if the project is correct and if all necessary information is available. |
compile | compilation | Source code compilation is done in this phase. |
Test | Testing | Tests the compiled source code suitable for testing framework. |
package | packaging | This phase creates the JAR/WAR package as mentioned in the packaging in POM.xml. |
install | installation | This phase installs the package in local/remote maven repository. |
Deploy | Deploying | Copies the final package to the remote repository. |
总有前阶段和后阶段来注册目标,这些阶段必须在特定阶段之前或之后运行。
当Maven开始构建项目时,它将逐步按照定义的阶段顺序执行目标,并在每个阶段中进行记录。
Maven具有以下三个标准生命周期-
目标代表一项特定的任务,有助于项目的建设和管理。它可能绑定到零个或多个构建阶段。可以通过直接调用在构建生命周期之外执行不受任何构建阶段约束的目标。
执行的顺序取决于调用目标和构建阶段的顺序。例如,考虑以下命令。 clean和package参数是构建阶段,而dependency:copy-dependencies是目标。
mvn clean dependency:copy-dependencies package
在这里,将首先执行清理阶段,然后执行dependency:copy-dependencies目标,最后执行包阶段。
当我们执行mvn post-clean命令时,Maven会调用clean生命周期,该生命周期包括以下阶段。
Maven清洁目标(clean:clean)绑定到清洁生命周期中的清洁阶段。其clean:cleangoal通过删除构建目录来删除构建的输出。因此,当执行mvn clean命令时,Maven会删除构建目录。
通过在清洁生命周期的上述任何阶段中提及目标,我们可以自定义此行为。
在以下示例中,我们将把maven-antrun-plugin:run目标附加到清洗前,清洗和清洗后阶段。这将使我们能够回显显示干净生命周期各个阶段的文本消息。
我们在C:\ MVN \ project文件夹中创建了pom.xml。
4.0.0
com.companyname.projectgroup
project
1.0
org.apache.maven.plugins
maven-antrun-plugin
1.1
id.pre-clean
pre-clean
run
pre-clean phase
id.clean
clean
run
clean phase
id.post-clean
post-clean
run
post-clean phase
现在打开命令控制台,转到包含pom.xml的文件夹并执行以下mvn命令。
C:\MVN\project>mvn post-clean
Maven将开始处理并显示清洁生命周期的所有阶段。
[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] ------------------------------------------------------------------
您可以尝试调整mvn clean命令,该命令将显示pre-clean和clean。后清洁阶段将不执行任何操作。
这是Maven的主要生命周期,用于构建应用程序。它分为以下21个阶段。
Sr.No. | Lifecycle Phase & Description |
---|---|
1 |
validate Validates whether project is correct and all necessary information is available to complete the build process. |
2 |
initialize Initializes build state, for example set properties. |
3 |
generate-sources Generate any source code to be included in compilation phase. |
4 |
process-sources Process the source code, for example, filter any value. |
5 |
generate-resources Generate resources to be included in the package. |
6 |
process-resources Copy and process the resources into the destination directory, ready for packaging phase. |
7 |
compile Compile the source code of the project. |
8 |
process-classes Post-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes. |
9 |
generate-test-sources Generate any test source code to be included in compilation phase. |
10 |
process-test-sources Process the test source code, for example, filter any values. |
11 |
test-compile Compile the test source code into the test destination directory. |
12 |
process-test-classes Process the generated files from test code file compilation. |
13 |
test Run tests using a suitable unit testing framework (Junit is one). |
14 |
prepare-package Perform any operations necessary to prepare a package before the actual packaging. |
15 |
package Take the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file. |
16 |
pre-integration-test Perform actions required before integration tests are executed. For example, setting up the required environment. |
17 |
integration-test Process and deploy the package if necessary into an environment where integration tests can be run. |
18 |
post-integration-test Perform actions required after integration tests have been executed. For example, cleaning up the environment. |
19 |
verify Run any check-ups to verify the package is valid and meets quality criteria. |
20 |
install Install the package into the local repository, which can be used as a dependency in other projects locally. |
21 |
deploy Copies the final package to the remote repository for sharing with other developers and projects. |
与Maven生命周期相关的重要概念很少,值得一提-
通过Maven命令调用某个阶段(例如mvn compile)时,仅执行该阶段之前(包括该阶段)的阶段。
根据包装的类型(JAR / WAR / EAR),不同的Maven目标将绑定到Maven生命周期的不同阶段。
在以下示例中,我们将把maven-antrun-plugin:run目标附加到Build生命周期的几个阶段。这将使我们能够回显显示生命周期各个阶段的文本消息。
我们已经更新了C:\ MVN \ project文件夹中的pom.xml。
4.0.0
com.companyname.projectgroup
project
1.0
org.apache.maven.plugins
maven-antrun-plugin
1.1
id.validate
validate
run
validate phase
id.compile
compile
run
compile phase
id.test
test
run
test phase
id.package
package
run
package phase
id.deploy
deploy
run
deploy phase
现在打开命令控制台,转到包含pom.xml的文件夹并执行以下mvn命令。
C:\MVN\project>mvn compile
Maven将开始处理并显示构建生命周期的各个阶段,直至编译阶段。
[INFO] Scanning for projects...
[INFO] -----------------------------------------------------------------
-
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO] task-segment: [compile]
[INFO] -----------------------------------------------------------------
-
[INFO] [antrun:run {execution: id.validate}]
[INFO] Executing tasks
[echo] validate phase
[INFO] Executed tasks
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered
resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory
C:\MVN\project\src\main\resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[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] -----------------------------------------------------------------
-
Maven网站插件通常用于创建新文档以创建报告,部署网站等。它具有以下阶段-
在以下示例中,我们将maven-antrun-plugin:run目标附加到站点生命周期的所有阶段。这将使我们能够回显显示生命周期各个阶段的文本消息。
我们已经更新了C:\ MVN \ project文件夹中的pom.xml。
4.0.0
com.companyname.projectgroup
project
1.0
org.apache.maven.plugins
maven-antrun-plugin
1.1
id.pre-site
pre-site
run
pre-site phase
id.site
site
run
site phase
id.post-site
post-site
run
post-site phase
id.site-deploy
site-deploy
run
site-deploy phase
现在打开命令控制台,转到包含pom.xml的文件夹并执行以下mvn命令。
C:\MVN\project>mvn site
Maven将开始处理并显示站点生命周期的各个阶段,直至站点阶段。
[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] ------------------------------------------------------------------