「Maven」- 概念术语:Lifecycle,Phase,Goal

Lifecycle

Maven 的生命周期就是为了对所有的构建过程进行抽象和统一。Maven 从大量项目和构 建工具中学习和反思,然后总结了一套高度完善的、易扩展的生命周期。这个生命周期包 含了项目的清理、初始化、编译、测试、打包、集成测试、验证、部署和站点生成等几乎 所有构建步骤。也就是说,几乎所有项目的构建,都能映射到这样一个生命周期上。

Maven 的生命周期是抽象的,这意味着生命周期本身不做任何实际的工作,在 Maven 的设计中,实际的任务 〈如编译源代码) 都交由插件来完成。这种思想与设计模式中的模 板方法 〈Template Method) 非常相似。模板方法模式在父类中定义算法的整体结构,子类 可以通过实现或者重写父类的方法来控制实际的行为,这样既保证了算法有足够的可扩展 性,又能够严格控制算法的整体结构。如下的模板方法抽象类能够很好地体现 Maven 生命 周期的概念。

Maven 拥有三套相互独 立的生命周期,它们分别为:
1)clean 生命周期的目的是清理项目,
2)default 生命周期的目的是构建项目,
3)而 site 生命周期的目的是建立项目站点。

Phase

每个生命周期包含一些阶段 (phase) ,这些阶段是有顺序的,并且后面的阶段依赖于 前面的阶段,用户和 Maven 最直接的交互方式就是调用这些生命周期阶段。

较之于生命周期阶段的前后依赖关系,三套生命周期本身是相互独立的,用户可以仅 仅调用 clean 生命周期的某个阶段,或者仅仅调用 default 生命周期的某个阶段,而不会对其 他生命周期产生任何影响,例如,当用户调用 clean 生命周期的 clean 阶段的时候,不会抽 发 default 生命周期的任何阶段,反之亦然,当用户调用 default 生命周期的 compile 阶段的时 翁,也不会触发 clean 生命周期的任何阶段

clean

以 clean 生命周 期为例,它包含的阶段有 pre-clean 、clean、post-clean。
当用户调用 pre-clean 的时候,只有 pre-clean 阶段得以执行;
当用户调用 clean 的时候,pre-clean 和 clean 阶段会得以顺序执行;
当用户调用 post-cjlean 的时候,pre-clean 、clean 和 post-clean 会得以顺序执行。

site

site 生命周期的目的是建立和发布项目站点,Maven 能够基于 POM 所包含的信息,自 动生成一个友好的站点,方便团队交流和发布项目信息。

该生命周期包含如下阶段:
pre-site 执行一些在生成项目站点之前需要完成的工作。
site 生成项目站点文档。
post-site 执行一些在生成项目站点之后需要完成的工作-。
site-deploy 将生成的项目站点发布到服务器上。

default

1)validate: validate the project is correct and all necessary information is available
2)compile: compile the source code of the project
3)test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
4)package: take the compiled code and package it in its distributable format, such as a JAR.
5)integration-test: process and deploy the package if necessary into an environment where integration tests can be run
6)verify: run any checks to verify the package is valid and meets quality criteria
7)install: install the package into the local repository, for use as a dependency in other projects locally
8)deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Plugin Goal

不管是 Lifecycle 还是 Phase,都属于高层定义,两者并不执行具体任务。任务是通过插件(Plugin)来提供的,不同的插件执行不同的动作,每个插件又包含多个目标(Goal),负责执行具体的任务。

例如:maven-dependency-plugin 包含十多个 Goal,比如 dependency:analyze、dependency:tree 等等;

插件仓库

插件也是从仓库中下载的,但是要使用 pluginRepository 来配置;

插件绑定

Phase 要和 Goal 进行绑定,然后 Maven 才能执行特定插件中的某个具体 Goal;

默认绑定,即某些绑定关系是 Maven 自带的,开箱即用:
1)site:sitemaven-site-plugin:site 绑定;
2)site:site-deploymaven-site-plugin:depoly 绑定;
3)更多默认绑定,参考 Introduction to the Build Lifecycle 文档;

修改绑定,即用户能够更具需要,来将不同插件的 Goal 绑定到不同的 Phase 中:

...
 <plugin>
   <groupId>org.codehaus.modello</groupId>
   <artifactId>modello-maven-plugin</artifactId>
   <version>1.8.1</version>
   <executions>
     <execution>
       <id>foo</id>
       <goals>
         <goal>java</goal>
       </goals>
       <phase>compile</phase>
       <configuration>
         <models>
           <model>src/main/mdo/maven.mdo</model>
         </models>
         <version>4.0.0</version>
       </configuration>
     </execution>
   </executions>
 </plugin>
...

// 该配置将插件的 java Goal 绑定到 compile 阶段。当后续执行 compile 阶段时,将执行 java Goal;

// 当然,即使不指定 phase 也能够成功执行,因为插件通常携带默认绑定关系:
// # mvn help:describe -Dplugin=org.codehaus.modello:modello-maven-plugin -Ddetail
// 输出的 Bound to phase 显示当前插件的各个 Goal 所绑定的 Phase;

// configuration,是对插件的参数配置,部分插件还能进一步配置所执行的命令

获取插件信息

插件文档;

通过命令:
1)mvn help:describe -Dplugin=org.codehaus.modello:modello-maven-plugin -Ddetail
2)mvn help:describe -Dplugin=org.codehaus.modello:modello-maven-plugin -Dgoal=compile

命令行调用插件

mvn org.apache.maven.plugins:maven-help-plugin:2.1:describe -Dgoal=compiler
mvn help:describe -Dgoal=compiler

helporg.apache.maven.plugins:maven-help-plugin:2.1 的目标前缀,用以简化命令输入,而无需输入完整坐标;

解析过程:WIP