在 Maven 出现之前,项目构建的生命周期就已经存在,开发人员每天都在对项目进行清理,编译,测试及部署,但由于没有统一的规范,不同公司甚至不同项目之间的构建的方式都不尽相同。

Maven 从大量项目和构建工具中学习和反思,最后总结了一套高度完美的,易扩展的生命周期。这个生命周期将项目的清理、初始化、编译、测试、打包、集成测试、验证、部署和站点生成等几乎所有构建过程进行了抽象和统一。

 

1. Maven 三套标准的生命周期

    1) clean 生命周期

        clean 生命周期用于清理项目,包括以下 3 个阶段。

            (1) pre-clean(清理前);
            (2) clean(清理);
            (3) post-clean(清理后);

    2) default (或称 build) 生命周期

        用于构建项目 default 生命周期定义了项目真正构建时所需要的所有步骤,它是所有生命周期中最核心,最重要的部分。

        default 生命周期包含非常多的阶段,如下表。

阶段 描述
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 拷贝最终的包文件到远程仓库中,以共享给其他开发人员和项目。

    3) site 生命周期

        sit 生命周期的目的是建立和部署项目站点,Maven 能够根据 POM 包含的信息,自动生成一个友好的站点,该站点包含一些与该项目相关的文档。

        site 生命周期包含以下 4 个阶段:

            (1) pre-site
            (2) site
            (3) post-site
            (4) site-deploy

2. 生命周期与插件的关系

    Maven 生命周期是抽象的,其本身不能做任何实际工作,这些实际工作(如源代码编译)都通过调用 Maven 插件 中的插件目标(plugin goal)完成的。

    为了更好的理解 Maven 生命周期、插件以及插件目标三者的关系,我们可以将 Maven 生命周期理解成一个抽象父类,将插件理解成其子类,将插件目标理解成子类中重写的方法,其基本结构与下面的示例代码相似。

复制代码

 1         package com.example; 2  3         public abstract class LifeCycle { 4             // 定义构建过程 5             public void build() { 6                 // 模拟生命周期各个阶段,即调用插件中目标 7                 clean(); 8                 init(); 9                 compile();10                 package();11                 install();12             }13             // 定义清理的过程14             public abstract void clean();15             // 定义初始化的过程16             public abstract void init();17             // 定义编译的过程18             public abstract void compile();19             // 定义打包的过程20             public abstract void package();21             // 定义安装的过程22             public abstract void install();23         }

复制代码

 

    模拟 clean 插件的子类,代码如下。

复制代码

1         package com.example;2 3         public abstract class CleanPlugin extends LifeCycle {4 5             @Override6             public void clean() {7                 System.out.println("clean");8             }9         }

复制代码

       
    以上示例中,父类 LifeCycle 模拟的是 Maven 生命周期,子类 CleanPlugin 模拟的是 Maven 插件,而子类中重写的 clean() 模拟的是插件目标。

    虽然示例中的代码与 Maven 实际代码相去甚远,但它们的基本理念是方法相同的。生命周期抽象了构建的各个步骤,定义了它们的执行顺序,但却没有提供具体的实现。插件中完成了对构建过程的实现,想要完成某个构建步骤,调用插件中的一个或多个插件目标即可。

    生命周期中的每个构建过程都可以绑定一个或多个插件目标,且 Maven 为大多数的构建步骤都绑定了默认的插件。例如,针对源代码编译的插件是 maven-compiler-plugin、针对集成测试的插件是 maven-surefire-plugin 等。

3. 示例

    在 “Maven基础知识(1)- Maven 简介、Maven 安装配置、创建 Quickstart 项目” 里 MavenDemo01 项目基础上,使用 maven-antrun-plugin 插件的 run 目标绑定到 Maven 生命周期的各阶段,显示自定义文本信息。
    
    1) 修改 pom.xml

复制代码

  1         <project xmlns="http://maven.apache.org/POM/4.0.0"  2                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  3                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  4                                     http://maven.apache.org/maven-v4_0_0.xsd">  5   6             <modelVersion>4.0.0</modelVersion>  7             <groupId>com.example</groupId>  8             <artifactId>MavenDemo01</artifactId>  9             <packaging>jar</packaging> 10             <version>1.0-SNAPSHOT</version> 11             <name>MavenDemo01</name> 12             <url>http://maven.apache.org</url> 13             <dependencies> 14                 <dependency> 15                     <groupId>junit</groupId> 16                     <artifactId>junit</artifactId> 17                     <version>3.8.1</version> 18                     <scope>test</scope> 19                 </dependency> 20             </dependencies> 21  22             <build> 23                 <plugins> 24                     <!-- site 生命周期相关插件,其它生命周期不需要 --> 25                     <plugin> 26                         <groupId>org.apache.maven.plugins</groupId> 27                         <artifactId>maven-site-plugin</artifactId> 28                         <version>3.8.2</version> 29                     </plugin> 30  31                     <!-- 添加插件 maven-antrun-plugin --> 32                     <plugin> 33                         <groupId>org.apache.maven.plugins</groupId> 34                         <artifactId>maven-antrun-plugin</artifactId> 35                         <version>1.8</version> 36                         <executions> 37                             <!-- clean 生命周期 --> 38                             <execution> 39                                 <!-- pre-clean 自定义输出信息 --> 40                                 <id>com.example pre-clean</id> 41                                 <phase>pre-clean</phase> 42                                 <goals> 43                                     <goal>run</goal> 44                                 </goals> 45                                 <configuration> 46                                     <tasks> 47                                         <echo>Maven clean lifecycle -> pre-clean: 自定义输出信息</echo> 48                                     </tasks> 49                                 </configuration> 50                             </execution> 51                             <execution> 52                                 <!-- clean 自定义输出信息 --> 53                                 <id>com.example clean</id> 54                                 <phase>clean</phase> 55                                 <goals> 56                                     <goal>run</goal> 57                                 </goals> 58                                 <configuration> 59                                     <tasks> 60                                         <echo>Maven clean lifecycle -> clean: 自定义输出信息</echo> 61                                     </tasks> 62                                 </configuration> 63                             </execution> 64                             <execution> 65                                 <!-- post-clean 自定义输出信息 --> 66                                 <id>com.example post-clean</id> 67                                 <phase>post-clean</phase> 68                                 <goals> 69                                     <goal>run</goal> 70                                 </goals> 71                                 <configuration> 72                                     <tasks> 73                                         <echo>Maven clean lifecycle -> post-clean: 自定义输出信息</echo> 74                                     </tasks> 75                                 </configuration> 76                             </execution> 77  78                             <!-- default 生命周期 --> 79                             <execution> 80                                 <!-- validate 自定义输出信息 --> 81                                 <id>com.example validate</id> 82                                 <phase>validate</phase> 83                                 <goals> 84                                     <goal>run</goal> 85                                 </goals> 86                                 <configuration> 87                                     <tasks> 88                                         <echo>Maven default lifecycle -> validate: 自定义输出信息</echo> 89                                     </tasks> 90                                 </configuration> 91                             </execution> 92                             <execution> 93                                 <!-- compile 自定义输出信息 --> 94                                 <id>com.example compile</id> 95                                 <phase>compile</phase> 96                                 <goals> 97                                     <goal>run</goal> 98                                 </goals> 99                                 <configuration>100                                     <tasks>101                                         <echo>Maven default lifecycle -> compile: 自定义输出信息</echo>102                                     </tasks>103                                 </configuration>104                             </execution>105                             <execution>106                                 <!-- test 自定义输出信息 -->107                                 <id>com.example test</id>108                                 <phase>test</phase>109                                 <goals>110                                     <goal>run</goal>111                                 </goals>112                                 <configuration>113                                     <tasks>114                                         <echo>Maven default lifecycle -> test: 自定义输出信息</echo>115                                     </tasks>116                                 </configuration>117                             </execution>118                             <execution>119                                 <!-- package 自定义输出信息 -->120                                 <id>com.example package</id>121                                 <phase>package</phase>122                                 <goals>123                                     <goal>run</goal>124                                 </goals>125                                 <configuration>126                                     <tasks>127                                         <echo>Maven default lifecycle -> package: 自定义输出信息</echo>128                                     </tasks>129                                 </configuration>130                             </execution>131                             <execution>132                                 <!-- deploy 自定义输出信息 -->133                                 <id>com.example deploy</id>134                                 <phase>deploy</phase>135                                 <goals>136                                     <goal>run</goal>137                                 </goals>138                                 <configuration>139                                     <tasks>140                                         <echo>Maven default lifecycle -> deploy: 自定义输出信息</echo>141                                     </tasks>142                                 </configuration>143                             </execution>144 145                             <!-- site 生命周期 -->146                             <execution>147                                 <!-- pre-site 自定义输出信息 -->148                                 <id>com.example pre-site</id>149                                 <phase>pre-site</phase>150                                 <goals>151                                     <goal>run</goal>152                                 </goals>153                                 <configuration>154                                     <tasks>155                                         <echo>Maven site lifecycle -> pre-site: 自定义输出信息</echo>156                                     </tasks>157                                 </configuration>158                             </execution>159                             <execution>160                                 <!-- site 自定义输出信息 -->161                                 <id>com.example site</id>162                                 <phase>site</phase>163                                 <goals>164                                     <goal>run</goal>165                                 </goals>166                                 <configuration>167                                     <tasks>168                                         <echo>Maven site lifecycle -> site: 自定义输出信息</echo>169                                     </tasks>170                                 </configuration>171                             </execution>172                             <execution>173                                 <!-- post-site 自定义输出信息 -->174                                 <id>com.example post-site</id>175                                 <phase>post-site</phase>176                                 <goals>177                                     <goal>run</goal>178                                 </goals>179                                 <configuration>180                                     <tasks>181                                             <echo>Maven site lifecycle -> post-site: 自定义输出信息</echo>182                                     </tasks>183                                 </configuration>184                             </execution>185                             <execution>186                                 <!-- site-deploy 自定义输出信息 -->187                                 <id>com.example site-deploy</id>188                                 <phase>site-deploy</phase>189                                 <goals>190                                     <goal>run</goal>191                                 </goals>192                                 <configuration>193                                     <tasks>194                                             <echo>Maven site lifecycle -> site-deploy: 自定义输出信息</echo>195                                     </tasks>196                                 </configuration>197                             </execution>198 199                         </executions>200                     </plugin>201                 </plugins>202             </build>                203         </project>

复制代码

        打开 cmd 命令行窗口,进入 D:\Workshop\maven\MavenDemo01 目录。

    2) 测试 clean 生命周期

       执行如下命令:

            D:\Workshop\maven\MavenDemo01>mvn post-clean

        输出结果如下:

复制代码

            [INFO] Scanning for projects...
            [INFO]
            [INFO] ----------------------< com.example:MavenDemo01 >-----------------------
            [INFO] Building MavenDemo01 1.0-SNAPSHOT
            [INFO] --------------------------------[ jar ]---------------------------------
            [INFO]
            [INFO] --- maven-antrun-plugin:1.8:run (com.example pre-clean) @ MavenDemo01 ---
            [WARNING] Parameter tasks is deprecated, use target instead
            [INFO] Executing tasks

            main:
                [echo] Maven clean lifecycle -> pre-clean: 自定义输出信息
            [INFO] Executed tasks
            [INFO]
            [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo01 ---
            [INFO] Deleting D:\Workshop\maven\MavenDemo01\target
            [INFO]
            [INFO] --- maven-antrun-plugin:1.8:run (com.example clean) @ MavenDemo01 ---
            [WARNING] Parameter tasks is deprecated, use target instead
            [INFO] Executing tasks

            main:
                [echo] Maven clean lifecycle -> clean: 自定义输出信息
            [INFO] Executed tasks
            [INFO]
            [INFO] --- maven-antrun-plugin:1.8:run (com.example post-clean) @ MavenDemo01 ---
            [WARNING] Parameter tasks is deprecated, use target instead
            [INFO] Executing tasks

            main:
                [echo] Maven clean lifecycle -> post-clean: 自定义输出信息
            [INFO] Executed tasks
            [INFO] ------------------------------------------------------------------------
            [INFO] BUILD SUCCESS
            [INFO] ------------------------------------------------------------------------
            [INFO] Total time:  0.627 s
            [INFO] Finished at: 2022-06-05T20:17:36+08:00
            [INFO] ------------------------------------------------------------------------

复制代码

    3) 测试 default 生命周期

        执行如下命令:

            D:\Workshop\maven\MavenDemo01>mvn compile

        输出结果如下:

复制代码

            [INFO] Scanning for projects...
            [INFO]
            [INFO] ----------------------< com.example:MavenDemo01 >-----------------------
            [INFO] Building MavenDemo01 1.0-SNAPSHOT
            [INFO] --------------------------------[ jar ]---------------------------------
            [INFO]
            [INFO] --- maven-antrun-plugin:1.8:run (com.example validate) @ MavenDemo01 ---
            [WARNING] Parameter tasks is deprecated, use target instead
            [INFO] Executing tasks

            main:
                [echo] Maven default lifecycle -> validate: 自定义输出信息
            [INFO] Executed tasks
            [INFO]
            [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo01 ---
            [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
            [INFO] skip non existing resourceDirectory D:\Workshop\maven\MavenDemo01\src\main\resources
            [INFO]
            [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo01 ---
            [INFO] Changes detected - recompiling the module!
            [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
            [INFO] Compiling 2 source files to D:\Workshop\maven\MavenDemo01\target\classes
            [INFO]
            [INFO] --- maven-antrun-plugin:1.8:run (com.example compile) @ MavenDemo01 ---
            [WARNING] Parameter tasks is deprecated, use target instead
            [INFO] Executing tasks

            main:
                [echo] Maven default lifecycle -> compile: 自定义输出信息
            [INFO] Executed tasks
            [INFO] ------------------------------------------------------------------------
            [INFO] BUILD SUCCESS
            [INFO] ------------------------------------------------------------------------
            [INFO] Total time:  1.177 s
            [INFO] Finished at: 2022-06-05T20:18:49+08:00
            [INFO] ------------------------------------------------------------------------

复制代码

    4) 测试 site 生命周期

        执行如下命令:

            D:\Workshop\maven\MavenDemo01>mvn site

        输出结果如下:

复制代码

            [INFO] Scanning for projects...
            [INFO]
            [INFO] ----------------------< com.example:MavenDemo01 >-----------------------
            [INFO] Building MavenDemo01 1.0-SNAPSHOT
            [INFO] --------------------------------[ jar ]---------------------------------
            [INFO]
            [INFO] --- maven-antrun-plugin:1.8:run (com.example pre-site) @ MavenDemo01 ---
            [WARNING] Parameter tasks is deprecated, use target instead
            [INFO] Executing tasks

            main:
                [echo] Maven site lifecycle -> pre-site: 自定义输出信息
            [INFO] Executed tasks
            [INFO]
            [INFO] --- maven-site-plugin:3.8.2:site (default-site) @ MavenDemo01 ---
            [WARNING] Input file encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
            [WARNING] Report plugin org.apache.maven.plugins:maven-project-info-reports-plugin has an empty version.
            [WARNING]
            [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
            [WARNING]
            [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
            [INFO] configuring report plugin org.apache.maven.plugins:maven-project-info-reports-plugin:3.3.0
            [INFO] 15 reports detected for maven-project-info-reports-plugin:3.3.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)
            [INFO] Relativizing decoration links with respect to localized project URL: http://maven.apache.org
            [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.3.0:dependencies
            [INFO] Generating "Dependency Information" report --- maven-project-info-reports-plugin:3.3.0:dependency-info
            [INFO] Generating "About" report         --- maven-project-info-reports-plugin:3.3.0:index
            [INFO] Generating "Plugin Management" report --- maven-project-info-reports-plugin:3.3.0:plugin-management
            [INFO] Generating "Plugins" report       --- maven-project-info-reports-plugin:3.3.0:plugins
            [INFO] Generating "Summary" report       --- maven-project-info-reports-plugin:3.3.0:summary
            [INFO]
            [INFO] --- maven-antrun-plugin:1.8:run (com.example site) @ MavenDemo01 ---
            [WARNING] Parameter tasks is deprecated, use target instead
            [INFO] Executing tasks

            main:
                [echo] Maven site lifecycle -> site: 自定义输出信息
            [INFO] Executed tasks
            [INFO] ------------------------------------------------------------------------
            [INFO] BUILD SUCCESS
            [INFO] ------------------------------------------------------------------------
            [INFO] Total time:  5.081 s
            [INFO] Finished at: 2022-06-05T20:19:45+08:00
            [INFO] ------------------------------------------------------------------------

复制代码

        Maven 命令运行完毕后,可以在项目的 target\site 目录下找到 Maven 生成的站点文件 (项目文档),双击打开 index.html 查看。

转自:https://www.cnblogs.com/tkuang/p/16345651.html