生命周期是包含在一个项目构建中的一系列有序的阶段。Maven可以支持许多不同的生命周期,但是最常用的是mavan默认的生命周期,这个生命周期中一开始的一个阶段吗是验证项目的基本完整性,最后的一个阶段是把一个项目发布成产品。
生命周期的阶段被特地留得含糊,单独的定义为验证(validation),测试(testing),或发布(deployment),而他们对不同项目来说以为着不同的事情。例如,打包(packe)这个阶段在一个项目里生成一个jar,它意味着“将一个项目打成一个jar”,而在另外一个项目里,打包这个阶段可能生成一个war文件。
以下图为例,“一个生命周期是一些阶段的序列”展示了默认Maven生命周期的简单样子。
Default 生命周期定义了真正构建时所需要执行的所有步骤,它是所有Maven生命周期中最核心的部分,被用于构建应用。
使用maven完成项目的构建,项目构建包括:清理、编译、测试、安装、部署等过程,maven将这些过程规范为一个生命周期,如下所示是生命周期的各个阶段。
这里只对重要的阶段进行解释:
生命周期阶段 | 描述 |
---|---|
validate | 检查工程配置是否正确,完成构建过程的所有必要信息是否能够获取到。 |
initialize | 初始化构建状态,例如设置属性。 |
generate-sources | 生成编译阶段需要包含的任何源码文件。 |
process-sources | 处理源代码,例如,过滤任何值(filter any value)。 |
generate-resources | 生成工程包中需要包含的资源文件。 |
process-resources | 拷贝和处理资源文件到目的目录中,为打包阶段做准备。 |
compile | 编译工程源码。 |
process-classes | 处理编译生成的文件,例如 Java Class 字节码的加强和优化。 |
generate-test-sources | 生成编译阶段需要包含的任何测试源代码。 |
process-test-sources | 处理测试源代码,例如,过滤任何值(filter any values)。 |
test-compile | 编译测试源代码到测试目的目录。 |
process-test-classes | 处理测试代码文件编译后生成的文件。 |
test | 使用适当的单元测试框架(例如JUnit)运行测试。 |
prepare-package | 在真正打包之前,为准备打包执行任何必要的操作。 |
package | 获取编译后的代码,并按照可发布的格式进行打包,例如 JAR、WAR 或者 EAR 文件。 |
pre-integration-test | 在集成测试执行之前,执行所需的操作。例如,设置所需的环境变量。 |
integration-test | 处理和部署必须的工程包到集成测试能够运行的环境中。 |
post-integration-test | 在集成测试被执行后执行必要的操作。例如,清理环境。 |
verify | 运行检查操作来验证工程包是有效的,并满足质量要求。 |
install | 安装工程包到本地仓库中,该仓库可以作为本地其他工程的依赖。 |
deploy | 拷贝最终的工程包到远程仓库中,以共享给其他开发人员和工程。 |
有一些与 Maven 生命周期相关的重要概念需要说明:
当一个阶段通过 Maven 命令调用时,例如 mvn compile,只有该阶段之前以及包括该阶段在内的所有阶段会被执行。
不同的 maven 目标将根据打包的类型(JAR / WAR / EAR),被绑定到不同的 Maven 生命周期阶段。
在下面的例子中,我们将 maven-antrun-plugin:run 目标添加到 Build 生命周期的一部分阶段中。这样我们可以显示生命周期的文本信息。
更新测试工程根目录下的pom.xml 文件。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.8.RELEASE
com.hl.magic.demo
springboot-maven
0.0.1-SNAPSHOT
springboot-maven
Demo project for Spring Boot
1.8
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
执行 mvn compile命令。
E:\HL\IdeaProjects\SpringBoot-Item\springboot-maven>mvn compile
Maven 将会开始处理并显示直到编译阶段的构建生命周期的各个阶段。
E:\HL\IdeaProjects\SpringBoot-Item\springboot-maven>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.hl.magic.demo:springboot-maven >-----------------
[INFO] Building springboot-maven 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.validate) @ springboot-maven ---
[INFO] Executing tasks
[echo] validate phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ springboot-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ springboot-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.compile) @ springboot-maven ---
[INFO] Executing tasks
[echo] compile phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.027 s
[INFO] Finished at: 2020-04-23T21:12:13+08:00
[INFO] ------------------------------------------------------------------------
执行 mvn package命令
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.hl.magic:Java-Maven >-----------------------
[INFO] Building Java-Maven 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Java-Maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\HL\IdeaProjects\Think-In-Java\Java-Maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Java-Maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Java-Maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\HL\IdeaProjects\Think-In-Java\Java-Maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Java-Maven ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ Java-Maven ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ Java-Maven ---
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (make-assemble) @ Java-Maven ---
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] org/ already added, skipping
[INFO] org/apache/ already added, skipping
[INFO] org/apache/commons/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/NOTICE.txt already added, skipping
[INFO] META-INF/LICENSE.txt already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/LICENSE.txt already added, skipping
[INFO] META-INF/NOTICE.txt already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] org/ already added, skipping
[INFO] org/apache/ already added, skipping
[INFO] org/apache/commons/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/LICENSE.txt already added, skipping
[INFO] META-INF/NOTICE.txt already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] Building jar: E:\HL\IdeaProjects\Think-In-Java\Java-Maven\target\Java-Maven-1.0.0-jar-with-dependencies.jar
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] org/ already added, skipping
[INFO] org/apache/ already added, skipping
[INFO] org/apache/commons/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/NOTICE.txt already added, skipping
[INFO] META-INF/LICENSE.txt already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/LICENSE.txt already added, skipping
[INFO] META-INF/NOTICE.txt already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] org/ already added, skipping
[INFO] org/apache/ already added, skipping
[INFO] org/apache/commons/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/LICENSE.txt already added, skipping
[INFO] META-INF/NOTICE.txt already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.742 s
[INFO] Finished at: 2021-06-29T20:33:38+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
我们知道打包阶段会将项目输出为一个jar包,然而在它之前的目标做什么呢?像compiler:compiler和surefire:test?在Maven经过它生命周期中package之前的阶段时,这些目标被允许了;
Maven执行一个阶段的时候,它首先会有序的执行前面的所有阶段,直到命令行指定的那个阶段为止,所以这个栗子棒轻了然一组标准插件的目标到默认的生命周期。当maven经过以package为结尾的默认生命周期的时候,下面的目标按顺序被执行:
Resources插件的 resources 目标绑定到了 resources 阶段。这个目标复制 src/main/resources 下的所有资源和其它任何配置的资源目录,到输出目录target。
Compiler插件的 compile 目标绑定到了 compile 阶段。这个目标编译 src/main/java 下的所有源代码和其他任何配置的资源目录,到输出目录target。
Resources插件的 testResources 目标绑定到了 test-resources 阶段。这个目标复制src/test/resources 下的所有资源和其它任何的配置的测试资源目录,到测试输出目录target。
Compiler插件的 testCompile 目标绑定到了 test-compile 阶段。这个目标编译 src/test/java 下的测试用例和其它任何的配置的测试资源目录,到测试输出目录target。
Surefire插件的 test 目标绑定到了 test 阶段。这个目标运行所有的测试并且创建那些捕捉详细测试结果的输出文件。默认情况下,如果有测试失败,这个目标会终止。
Jar插件的 jar 目标绑定到了 package 阶段。这个目标把输出目录打包成JAR文件。