package net.biancheng.www; /** * 模拟 maven 生命周期 */ public abstract class LifeCycle { //定义构建过程 public void build() { //模拟生命周期各个阶段,即调用插件中目标 clean(); initialize(); compile(); packager(); install(); } //定义清理的过程 public abstract void clean(); //定义初始化的过程 public abstract void initialize(); //定义编译的过程 public abstract void compile(); //定义打包的过程 public abstract void packager(); //定义安装的过程 public abstract void install(); }
package net.biancheng.www; /** * 子类模拟clean 相关的插件 */ public abstract class CleanPlugin extends LifeCycle { //重写父类(生命周期)的清理工作 //模拟插件目标 @Override public void clean() { System.out.println("清理"); } }
注意:Maven 插件和插件目标了解即可,在本教程后面的章节 Maven 插件中会详细介绍。
mvn install
与构建阶段的前后依赖关系不同,三套生命周期本身是相互独立的,用户可以只调用 clean 生命周期的某个阶段,也可以只调用 default 生命周期的某个阶段,而不会对其他生命周期造成任何影响。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.biancheng.www</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>compile</scope> </dependency> <!-- log4j依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <plugins> <!-- 添加插件 maven-antrun-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <!--自定义阶段 id --> <id>www.lmcjl.com pre-clean</id> <!--阶段 --> <phase>pre-clean</phase> <!--目标 --> <goals> <goal>run</goal> </goals> <!--配置 --> <configuration> <!-- 执行的任务 --> <tasks> <!-- 输出自定义文本信息 --> <echo>预清理阶段,编程帮 欢迎您的到来,网址:www.lmcjl.com</echo> </tasks> </configuration> </execution> <execution> <!--自定义阶段 id --> <id>www.lmcjl.com clean</id> <!--阶段 --> <phase>clean</phase> <!--目标 --> <goals> <goal>run</goal> </goals> <!--配置 --> <configuration> <!-- 执行的任务 --> <tasks> <!--自定义文本信息 --> <echo>清理阶段,编程帮 欢迎您的到来,网址:www.lmcjl.com</echo> </tasks> </configuration> </execution> <execution> <!--自定义阶段 id --> <id>www.lmcjl.com post-clean</id> <!--阶段 --> <phase>post-clean</phase> <!-- 目标 --> <goals> <goal>run</goal> </goals> <!--配置 --> <configuration> <!-- 执行的任务 --> <tasks> <!-- 执行的任务 --> <echo>后清理阶段,编程帮 欢迎您的到来,网址:www.lmcjl.com</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
mvn post-clean
,结果如下。
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.www:maven >----------------------- [INFO] Building maven 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-antrun-plugin:1.1:run (www.lmcjl.com pre-clean) @ maven --- [INFO] Executing tasks [echo] 预清理阶段,编程帮 欢迎您的到来,网址:www.lmcjl.com [INFO] Executed tasks [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven --- [INFO] [INFO] --- maven-antrun-plugin:1.1:run (www.lmcjl.com clean) @ maven --- [INFO] Executing tasks [echo] 清理阶段,编程帮 欢迎您的到来,网址:www.lmcjl.com [INFO] Executed tasks [INFO] [INFO] --- maven-antrun-plugin:1.1:run (www.lmcjl.com post-clean) @ maven --- [INFO] Executing tasks [echo] 后清理阶段,编程帮 欢迎您的到来,网址:www.lmcjl.com [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.365 s [INFO] Finished at: 2021-03-01T13:25:07+08:00 [INFO] ------------------------------------------------------------------------
阶段 | 描述 |
---|---|
validate | 验证项目是否正确以及所有必要信息是否可用。 |
initialize | 初始化构建状态。 |
generate-sources | 生成编译阶段需要的所有源码文件。 |
process-sources | 处理源码文件,例如过滤某些值。 |
generate-resources | 生成项目打包阶段需要的资源文件。 |
process-resources | 处理资源文件,并复制到输出目录,为打包阶段做准备。 |
compile | 编译源代码,并移动到输出目录。 |
process-classes | 处理编译生成的字节码文件 |
generate-test-sources | 生成编译阶段需要的测试源代码。 |
process-test-sources | 处理测试资源,并复制到测试输出目录。 |
test-compile | 编译测试源代码并移动到测试输出目录中。 |
test | 使用适当的单元测试框架(例如 JUnit)运行测试。 |
prepare-package | 在真正打包之前,执行一些必要的操作。 |
package | 获取编译后的代码,并按照可发布的格式进行打包,例如 JAR、WAR 或者 EAR 文件。 |
pre-integration-test | 在集成测试执行之前,执行所需的操作,例如设置环境变量。 |
integration-test | 处理和部署所需的包到集成测试能够运行的环境中。 |
post-integration-test | 在集成测试被执行后执行必要的操作,例如清理环境。 |
verify | 对集成测试的结果进行检查,以保证质量达标。 |
install | 安装打包的项目到本地仓库,以供其他项目使用。 |
deploy | 拷贝最终的包文件到远程仓库中,以共享给其他开发人员和项目。 |
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.biancheng.www</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>compile</scope> </dependency> <!-- log4j依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <plugins> <!-- 添加插件 maven-antrun-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>www.lmcjl.com validate</id> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com 验证阶段……</echo> </tasks> </configuration> </execution> <execution> <id>www.lmcjl.com compile</id> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com 编译阶段……</echo> </tasks> </configuration> </execution> <execution> <id>www.lmcjl.com test</id> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com 测试阶段……</echo> </tasks> </configuration> </execution> <execution> <id>www.lmcjl.com package</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com 打包阶段……</echo> </tasks> </configuration> </execution> <execution> <id>www.lmcjl.com deploy</id> <phase>deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com 部署阶段……</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
D:\maven>mvn compile
,结果如下。[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.www:maven >----------------------- [INFO] Building maven 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-antrun-plugin:1.1:run (www.lmcjl.com validate) @ maven --- [INFO] Executing tasks [echo] 编程帮 欢迎您的到来,网址:www.lmcjl.com 验证阶段…… [INFO] Executed tasks [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\maven\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven --- [INFO] No sources to compile [INFO] [INFO] --- maven-antrun-plugin:1.1:run (www.lmcjl.com compile) @ maven --- [INFO] Executing tasks [echo] 编程帮 欢迎您的到来,网址:www.lmcjl.com 编译阶段…… [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.652 s [INFO] Finished at: 2021-03-01T15:07:50+08:00 [INFO] ------------------------------------------------------------------------
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.biancheng.www</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>compile</scope> </dependency> <!-- log4j依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- 添加插件 maven-antrun-plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> <executions> <execution> <id>www.bianchengbang.net pre-site</id> <phase>pre-site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com pre-site 阶段……</echo> </tasks> </configuration> </execution> <execution> <id>www.bianchengbang.net site</id> <phase>site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com site 阶段……</echo> </tasks> </configuration> </execution> <execution> <id>www.bianchengbang.net post-site</id> <phase>post-site</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com post-site 阶段……</echo> </tasks> </configuration> </execution> <execution> <id>www.bianchengbang.net site-deploy</id> <phase>site-deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>编程帮 欢迎您的到来,网址:www.lmcjl.com site-deploy 阶段……</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
mvn post-site
,结果如下。
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.www:maven >----------------------- [INFO] Building maven 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-antrun-plugin:1.3:run (www.bianchengbang.net pre-site) @ maven --- [INFO] Executing tasks [echo] 编程帮 欢迎您的到来,网址:www.lmcjl.com pre-site 阶段…… [INFO] Executed tasks [INFO] [INFO] --- maven-site-plugin:3.7.1:site (default-site) @ maven --- [WARNING] Input file encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] configuring report plugin org.apache.maven.plugins:maven-project-info-reports-plugin:3.0.0 [INFO] 15 reports detected for maven-project-info-reports-plugin:3.0.0: ci-management, dependencies, dependency-info, dependency-management, distribution-management, index, issue-management, licenses, mailing-lists, modules, plugin-management, plugins, scm, summary, team [INFO] Rendering site with default locale English (en) [WARNING] No project URL defined - decoration links will not be relativized! [INFO] Rendering content with org.apache.maven.skins:maven-default-skin:jar:1.2 skin. [INFO] Generating "Dependencies" report --- maven-project-info-reports-plugin:3.0.0:dependencies [INFO] Generating "Dependency Information" report --- maven-project-info-reports-plugin:3.0.0:dependency-info [INFO] Generating "About" report --- maven-project-info-reports-plugin:3.0.0:index [INFO] Generating "Plugin Management" report --- maven-project-info-reports-plugin:3.0.0:plugin-management [INFO] Generating "Plugins" report --- maven-project-info-reports-plugin:3.0.0:plugins [INFO] Generating "Summary" report --- maven-project-info-reports-plugin:3.0.0:summary [INFO] [INFO] --- maven-antrun-plugin:1.3:run (www.bianchengbang.net site) @ maven --- [INFO] Executing tasks [echo] 编程帮 欢迎您的到来,网址:www.lmcjl.com site 阶段…… [INFO] Executed tasks [INFO] [INFO] --- maven-antrun-plugin:1.3:run (www.bianchengbang.net post-site) @ maven --- [INFO] Executing tasks [echo] 编程帮 欢迎您的到来,网址:www.lmcjl.com post-site 阶段…… [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.824 s [INFO] Finished at: 2021-03-01T16:02:44+08:00 [INFO] ------------------------------------------------------------------------
本文链接:http://task.lmcjl.com/news/13030.html