A Build Lifecycle is a well defined sequence of phases which define the order in which the goals are to be executed. Here phase represents a stage in life cycle.
As an example, a typical Maven Build Lifecycle consists of following sequence of phases
Phase | Handles | Description |
---|---|---|
prepare-resources | resource copying | Resource copying can be customized in this phase. |
compile | compilation | Source code compilation is done in this phase. |
package | packaging | This phase creates the JAR / WAR package as mentioned in packaging in POM.xml. |
install | installation | This phase installs the package in local / remote maven repository. |
There are always pre and post phases which can be used to register goals which must run prior to or after a particular phase.
When Maven starts building a project, it steps through a defined sequence of phases and executes goals which are registered with each phase. Maven has following three standard lifecycles:
clean
default(or build)
site
A goal represents a specific task which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.
The order of execution depends on the order in which the goal(s) and the build phase(s) are invoked. For example, consider the command below. The clean and package arguments are build phases while the dependency:copy-dependencies is a goal.
mvn clean dependency:copy-dependencies package
Here the clean phase will be executed first, and then the dependency:copy-dependencies goal will be executed, and finally package phase will be executed.
When we execute mvn post-clean command, Maven invokes the clean lifecycle consisting of the following phases.
pre-clean
clean
post-clean
Maven的清洁目标(清洁:清洁)绑定到干净的清洁生命周期阶段。它的干净:干净的目标通过删除创建目录来删除创建的输出。因此,当mvn clean命令执行时,Maven将删除创建目录。
我们可以通过提及清洁生命周期的上述任何阶段中的目标来定制此行为。
在下面的例子中,我们将附加maven-antrun-plugin:运行目标到预清洁,清洁和清洁后阶段。这将允许我们回显显示清洁生命周期阶段的短信息。
我们在C: MVN project文件夹中创建了一个pom.xml。
<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> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-clean</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.clean</id> <phase>clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-clean</id> <phase>post-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
现在打开命令控制台,转到包含pom.xml的文件夹,然后执行以下mvn命令。
C:MVNproject>mvn post-clean
Maven将开始处理和显示清洁生命周期的所有阶段
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building project 1.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-antrun-plugin:1.1:run (id.pre-clean) @ project --- [INFO] Executing tasks [echo] pre-clean phase [INFO] Executed tasks [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project --- [INFO] [INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ project --- [INFO] Executing tasks [echo] clean phase [INFO] Executed tasks [INFO] [INFO] --- maven-antrun-plugin:1.1:run (id.post-clean) @ project --- [INFO] Executing tasks [echo] post-clean phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.078 s [INFO] Finished at: 2015-09-26T08:03:06+05:30 [INFO] Final Memory: 7M/247M [INFO] ------------------------------------------------------------------------
您可以尝试调整 mvn clean命令,这将显示预清洁和清洁,没有任何内容将执行后清洁阶段。
这是Maven的主要生命周期,用于创建应用程序。它有23个阶段。
生命周期阶段 | 描述 |
---|---|
验证 | 验证项目是否正确,并且所有必需的信息都可用于完成创建过程。 |
初始化 | 初始化创建状态,例如设置属性 |
生成源 | 生成要包含在编译阶段的任何源代码。 |
流程来源 | 处理源代码,例如,过滤任何值。 |
生成资源 | 生成包含在包中的资源。 |
流程资源 | 将资源复制并处理到目标目录,准备打包阶段。 |
编译 | 编译项目的源代码。 |
过程类 | 从编译中处理生成的文件,例如对Java类进行字节码增强/优化。 |
生成测试源 | 生成要包含在编译阶段的任何测试源代码。 |
过程测试源 | 处理测试源代码,例如过滤任何值。 |
测试编译 | 将测试源代码编译成测试目标目录。 |
过程测试类 | 从测试代码文件编译处理生成的文件。 |
测试 | 使用合适的单元测试框架运行测试(Junit是一个)。 |
准备包装 | 在实际包装之前,执行必要的操作来准备包装。 |
包 | 拿出编译的代码,并以其可分发的格式打包,例如JAR,WAR或EAR文件。 |
预集成测试 | 在执行集成测试之前执行所需的操作。例如,设置所需的环境。 |
集成测试 | 如果需要,可以将该包过程并部署到可以运行集成测试的环境中。 |
后整合测试 | 执行集成测试后执行所需的操作。例如,清理环境。 |
校验 | 运行任何检查以验证包装是否有效并符合质量标准。 |
安装 | 将软件包安装到本地存储库中,可以将其用作本地其他项目的依赖项。 |
部署 | 将最终软件包复制到远程存储库,以便与其他开发人员和项目共享。 |
有关Maven生命周期的几个重要概念值得一提:
当通过Maven命令调用一个阶段时,例如mvn编译,只有阶段才能执行。
根据包装类型(JAR / WAR / EAR),不同的maven目标将绑定到Maven生命周期的不同阶段。
在下面的示例中,我们将附加maven-antrun-plugin:运行目标到Build生命周期的几个阶段。这将允许我们回显显示生命周期阶段的短信。
我们已经在C: MVN project文件夹中更新了pom.xml。
<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> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.validate</id> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>validate phase</echo> </tasks> </configuration> </execution> <execution> <id>id.compile</id> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>compile phase</echo> </tasks> </configuration> </execution> <execution> <id>id.test</id> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>test phase</echo> </tasks> </configuration> </execution> <execution> <id>id.package</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>package phase</echo> </tasks> </configuration> </execution> <execution> <id>id.deploy</id> <phase>deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>deploy phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
现在打开命令控制台,去包含pom.xml的文件夹,然后执行下面的mvn命令。
C:MVNproject>mvn compile
Maven将开始处理和显示创建生命周期的阶段,直到编译阶段。
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building project 1.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-antrun-plugin:1.1:run (id.validate) @ project --- [INFO] Executing tasks [echo] validate phase [INFO] Executed tasks [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project -- - [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:MVNprojectsrcmain esources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project --- [INFO] No sources to compile [INFO] [INFO] --- maven-antrun-plugin:1.1:run (id.compile) @ project --- [INFO] Executing tasks [echo] compile phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.704 s [INFO] Finished at: 2015-09-26T08:22:05+05:30 [INFO] Final Memory: 10M/247M [INFO] ------------------------------------------------------------------------
Maven Site plugin通常用于创建新的文档来创建报告,部署站点等。
阶段
网站前
现场
后现场
站点部署
在以下示例中,我们将附加 maven-antrun-plugin:运行目标到站点生命周期的所有阶段。这将允许我们回显显示生命周期阶段的短信。
我们已经在C: MVN project文件夹中更新了pom.xml。
<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> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-site</id> <phase>pre-site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.site</id> <phase>site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-site</id> <phase>post-site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-site phase</echo> </tasks> </configuration> </execution> <execution> <id>id.site-deploy</id> <phase>site-deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>site-deploy phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
现在打开命令控制台,去包含pom.xml的文件夹,然后执行下面的mvn命令。
C:MVNproject>mvn site
Maven将开始处理和显示站点生命周期的阶段。
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building project 1.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-antrun-plugin:1.1:run (id.pre-site) @ project --- [INFO] Executing tasks [echo] pre-site phase [INFO] Executed tasks [INFO] [INFO] --- maven-site-plugin:3.3:site (default-site) @ project --- [WARNING] Report plugin org.apache.maven.plugins:maven-project-info-reports-plug in has an empty version. [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten t he stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support buildin g such malformed projects. [INFO] configuring report plugin org.apache.maven.plugins:maven-project-info-rep orts-plugin:2.8.1 [WARNING] No project URL defined - decoration links will not be relativized! [INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 ski n. [INFO] Generating "Dependency Convergence" report --- maven-project-info-repo rts-plugin:2.8.1 [INFO] Generating "Dependency Information" report --- maven-project-info-repo rts-plugin:2.8.1 [INFO] Generating "About" report --- maven-project-info-reports-plugin:2.8.1 [INFO] Generating "Plugin Management" report --- maven-project-info-reports-p lugin:2.8.1 [INFO] Generating "Project Plugins" report --- maven-project-info-reports-plu gin:2.8.1 [INFO] Generating "Project Summary" report --- maven-project-info-reports-plu gin:2.8.1 [INFO] [INFO] --- maven-antrun-plugin:1.1:run (id.site) @ project --- [INFO] Executing tasks [echo] site phase [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 11.390 s [INFO] Finished at: 2015-09-26T08:43:45+05:30 [INFO] Final Memory: 18M/247M [INFO] ------------------------------------------------------------------------