Maven 的生命周期就是对所有的构建过程进行抽象和统一。包含了项目的清理(clean)、初始化(initialization)、编译(compile)、测试(test)、打包(package)、集成测试、验证、部署(deploy)和站点生成等几乎所有的构建步骤。
Maven 的生命周期是抽象的,即生命周期不做任何实际的工作,实际任务由插件完成,类似于设计模式中的模板方法。了解 Maven 的生命周期对用好 maven 至关重要,如下图:
上图展示的是 Maven 的默认(default)生命周期:
1)validate:验证工程是否正确,所有需要的资源是否可用。
2)compile:编译项目的源代码。
3)test:使用合适的单元测试框架来测试已编译的源代码,这些测试不需要打包和布署。
4)package:把已编译的代码打包成可发布的格式,比如jar。
5)verify:运行所有检查,验证包是否有效且达到质量标准。
6)install:把包安装到 Maven 本地仓库,可以被其他工程作为依赖来使用。
7)deploy:在集成或者发布环境下执行,将最终版本的包拷贝到远程的仓库(repository),使得其他的开发者或者工程可以共享。
上面通过一些理论知识,初步认识了 Maven 的声明周期。下面将创建一个简单的 Maven 项目,然后运行 Maven 的不同生命周期,观察输出的日志(其中那些插件被执行了)。
项目结构如下图:
执行项目清理,将删除项目下面的 target 目录。将执行如下插件:
(1)maven-clean-plugin:用来清理 target 目录
日志如下:
[INFO] ------------------------< com.hxstrive.demo:demo >------------------------ [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ demo --- [INFO] Deleting C:\Users\Administrator\Desktop\demo\target [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
验证工程是否正确,所有需要的资源是否可用,日志如下:
[INFO] ------------------------< com.hxstrive.demo:demo >------------------------ [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.082 s [INFO] Finished at: 2020-08-26T16:57:12+08:00 [INFO] ------------------------------------------------------------------------
并没有执行任何插件。
编译项目的源代码。该生命周期将运行如下插件:
(1)maven-resources-plugin:负责处理项目资源文件并拷贝到输出目录。
(2)maven-compiler-plugin:用来编译Java代码,并且输出到 target\classes 目录。
Maven 区别对待 Java 代码文件和资源文件,maven-compiler-plugin 用来编译Java代码,maven-resources-plugin 则用来处理资源文件。
默认的主资源文件目录是 src/main/resources,很多用户会需要添加额外的资源文件目录,这个时候就可以通过配置 maven-resources-plugin 来实现。
日志如下:
[INFO] ------------------------< com.hxstrive.demo:demo >------------------------ [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.506 s [INFO] Finished at: 2020-08-26T16:55:51+08:00 [INFO] ------------------------------------------------------------------------
使用合适的单元测试框架来测试已编译的源代码,这些测试不需要打包和布署。该生命周期将运行如下插件:
(1)maven-resources-plugin:2.6:resources:负责处理项目资源文件并拷贝到输出目录。
(2)maven-compiler-plugin:3.1:compile:编译项目 Java 代码。
(3)maven-resources-plugin:2.6:testResources:负责处理项目测试资源文件并拷贝到输出目录。
(4)maven-compiler-plugin:3.1:testCompile:编译项目测试 Java 代码
(5)maven-surefire-plugin:2.12.4:test:负责执行项目的单元测试,源码位于 src/test/java,编译后位于 target/test-class 目录。
日志如下:
[INFO] ------------------------< com.hxstrive.demo:demo >------------------------ [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
把已编译的代码打包成可发布的格式,比如:jar、war。该生命周期将运行如下插件:
(1)maven-resources-plugin:2.6:resources:负责处理项目资源文件并拷贝到输出目录。
(2)maven-compiler-plugin:3.1:compile:编译项目 Java 代码。
(3)maven-resources-plugin:2.6:testResources:负责处理项目测试资源文件并拷贝到输出目录。
(4)maven-compiler-plugin:3.1:testCompile:编译项目测试 Java 代码
(5)maven-surefire-plugin:2.12.4:test:负责执行项目的单元测试,源码位于 src/test/java,编译后位于 target/test-class 目录。
(6)maven-jar-plugin:2.4:jar:负责将项目打包成 jar 包,不包含测试代码和资源。如果你需要将项目打包成 war 包,则需要使用 maven-war-plugin 插件。
日志如下:
[INFO] ------------------------< com.hxstrive.demo:demo >------------------------ [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo --- [INFO] Building jar: C:\Users\Administrator\Desktop\demo\target\demo-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
运行所有检查,验证包是否有效且达到质量标准。该生命周期将运行如下插件:
(1)maven-resources-plugin:2.6:resources:负责处理项目资源文件并拷贝到输出目录。
(2)maven-compiler-plugin:3.1:compile:编译项目 Java 代码。
(3)maven-resources-plugin:2.6:testResources:负责处理项目测试资源文件并拷贝到输出目录。
(4)maven-compiler-plugin:3.1:testCompile:编译项目测试 Java 代码
(5)maven-surefire-plugin:2.12.4:test:负责执行项目的单元测试,源码位于 src/test/java,编译后位于 target/test-class 目录。
(6)maven-jar-plugin:2.4:jar:负责将项目打包成 jar 包,不包含测试代码和资源。如果你需要将项目打包成 war 包,则需要使用 maven-war-plugin 插件。
细心的读者会发现,它和 package 生命周期执行的插件顺序和个数一样。日志如下:
[INFO] ------------------------< com.hxstrive.demo:demo >------------------------ [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo --- [INFO] Building jar: C:\Users\Administrator\Desktop\demo\target\demo-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
把包安装到 Maven 本地仓库,可以被其他工程作为依赖来使用。该生命周期将运行如下插件:
(1)maven-resources-plugin:2.6:resources:负责处理项目资源文件并拷贝到输出目录。
(2)maven-compiler-plugin:3.1:compile:编译项目 Java 代码。
(3)maven-resources-plugin:2.6:testResources:负责处理项目测试资源文件并拷贝到输出目录。
(4)maven-compiler-plugin:3.1:testCompile:编译项目测试 Java 代码
(5)maven-surefire-plugin:2.12.4:test:负责执行项目的单元测试,源码位于 src/test/java,编译后位于 target/test-class 目录。
(6)maven-jar-plugin:2.4:jar:负责将项目打包成 jar 包,不包含测试代码和资源。如果你需要将项目打包成 war 包,则需要使用 maven-war-plugin 插件。
(7)maven-install-plugin:2.4:install:将打包好的 jar 安装到本地 Maven 仓库,供其他项目引用。
日志如下:
[INFO] ------------------------< com.hxstrive.demo:demo >------------------------ [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo --- [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ demo --- [INFO] Installing C:\Users\Administrator\Desktop\demo\target\demo-1.0-SNAPSHOT.jar to D:\Repository\Maven\com\huangx\demo\demo\1.0-SNAPSHOT\demo-1.0-SNAPSHOT.jar [INFO] Installing C:\Users\Administrator\Desktop\demo\pom.xml to D:\Repository\Maven\com\huangx\demo\demo\1.0-SNAPSHOT\demo-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
在集成或者发布环境下执行,将最终版本的包拷贝到远程的仓库(repository),使得其他的开发者或者工程可以共享。该生命周期将运行如下插件:
(1)maven-resources-plugin:2.6:resources:负责处理项目资源文件并拷贝到输出目录。
(2)maven-compiler-plugin:3.1:compile:编译项目 Java 代码。
(3)maven-resources-plugin:2.6:testResources:负责处理项目测试资源文件并拷贝到输出目录。
(4)maven-compiler-plugin:3.1:testCompile:编译项目测试 Java 代码
(5)maven-surefire-plugin:2.12.4:test:负责执行项目的单元测试,源码位于 src/test/java,编译后位于 target/test-class 目录。
(6)maven-jar-plugin:2.4:jar:负责将项目打包成 jar 包,不包含测试代码和资源。如果你需要将项目打包成 war 包,则需要使用 maven-war-plugin 插件。
(7)maven-install-plugin:2.4:install:将打包好的 jar 安装到本地 Maven 仓库,供其他项目引用。
(8)maven-deploy-plugin:2.7:deploy:将打包好的 jar 安装到远程 Maven 仓库。
日志如下:
[INFO] ------------------------< com.hxstrive.demo:demo >------------------------ [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to C:\Users\Administrator\Desktop\demo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo --- [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo --- [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ demo --- [INFO] Installing C:\Users\Administrator\Desktop\demo\target\demo-1.0-SNAPSHOT.jar to D:\Repository\Maven\com\huangx\demo\demo\1.0-SNAPSHOT\demo-1.0-SNAPSHOT.jar [INFO] Installing C:\Users\Administrator\Desktop\demo\pom.xml to D:\Repository\Maven\com\huangx\demo\demo\1.0-SNAPSHOT\demo-1.0-SNAPSHOT.pom [INFO] [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ demo --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE
到这里 Maven 常用生命周期就介绍完了,通过上面的介绍我们就可以清晰的知道 Maven 每个生命周期具体执行了那些插件,以及他们的顺序。