Maven 生命周期

1. Maven 构建生命周期

Maven 构建生命周期就是 Maven 将一个整体任务划分为一个个的阶段,类似于流程图,按顺序依次执行。也可以指定该任务执行到中间的某个阶段结束。
Maven 的内部有三个构建生命周期,分别是 clean, default, site。其中 default 生命周期的核心阶段如下所示:

default lifecycle

2. 如何使用构建生命周期来完成构建工作

  • 可以指定某个生命周期的阶段

执行 mvn install 命令,将完成 validate, compile, test, package, verify, install 阶段,并将 package 生成的包发布到本地仓库中。其中某些带有连字符的阶段不能通过 shell 命令单独指定。例如:(pre-, post-, or process-*)

mvn install
  • 可以指定多个不同构建生命周期的阶段

执行 mvn clean deploy 命令,首先完成的 clean lifecycle,将以前构建的文件清理,然后再执行 default lifecycle 的 validate, compile, test, package, verify, insstall, deploy 阶段,将 package 阶段创建的包发布到远程仓库中。

mvn clean deploy

3. 阶段与插件的关系

如上所述,Maven 将构建过程定义为 default lifecycle,并将 default lifecycle 划分为一个个的阶段 phase,这一系列 phase 仅仅是规定执行顺序,至于每个阶段做什么工作?由谁来做?答案就在 插件(plugins) 中。
Maven 对工程的所有操作实实在在的都是由 插件 来完成的。一个插件可以支持多种功能,称之为目标(goal),例如:compiler 插件有两个目标:compile 和 testCompile,分别实现编译源代码 和 编译测试代码。
如何将插件与 Maven 的构建生命周期绑定在一起呢?通过将插件的目标(goal)与 build lifecycle 中 phase 绑定到一起,这样,当要执行某个 phase 时,就调用插件来完成绑定的目标。
如下图所示:从图中可以看出,每一个阶段可以绑定0 个 或 多个目标,每个插件可以提供 1 个或多个目标。


build lifecycle & plugin goal

4. 如何为自己的工程创建构建生命周期

  • 设置不同的 packaging 类型

在 pom.xml 文件中,packaging 类型支持 jar, war, ear, pom 等多种类型,不同的 packaging 类型会使得不同的 phase 绑定不同的 plugin goal。下面是 packaging 类型为 jar 时,phase 与 plugin goal 的映射关系。

阶段 目标
process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
install install:install
deploy deploy:deploy
  • 配置 plugin

在 pom.xml 文件中, <build> <plugins> 元素下可以添加 <plugin>,通过指定 goal 和 phase 来进行绑定。
例如:将插件 modello-maven-plugin 的 java 目标绑定到 generate-sources 阶段。

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

5. 我没有在 pom.xml 指定任何 plugin,但是也能正常构建工程

你可以能会疑问,默认的 pom.xml 文件并没有配置各种 plugin,但是也能正常构建工程?答案是 Maven 自己默认指定了 plugin。
下面是一个没有配置任何 plugin 的 pom.xml,执行 mvn install 的输出日志,从日志中可以看到 一系列的 插件(plugin):版本号:目标(phase),例如 maven-resources-plugin:2.6:resources (default-resources),maven-compiler-plugin:3.1:compile (default-compile) ,maven-resources-plugin:2.6:testResources (default-testResources),maven-compiler-plugin:3.1:testCompile (default-testCompile),maven-surefire-plugin:2.12.4:test (default-test),maven-jar-plugin:2.4:jar (default-jar) ,maven-install-plugin:2.4:install (default-install),

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/zhangguanghui/git/my-app/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ my-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/zhangguanghui/git/my-app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/zhangguanghui/git/my-app/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ my-app ---
[INFO] Surefire report directory: /Users/zhangguanghui/git/my-app/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ my-app ---
[INFO] Building jar: /Users/zhangguanghui/git/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ my-app ---
[INFO] Installing /Users/zhangguanghui/git/my-app/target/my-app-1.0-SNAPSHOT.jar to /Users/zhangguanghui/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar
[INFO] Installing /Users/zhangguanghui/git/my-app/pom.xml to /Users/zhangguanghui/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.726 s
[INFO] Finished at: 2016-11-20T00:41:11+08:00
[INFO] Final Memory: 15M/310M
[INFO] ------------------------------------------------------------------------

5. 完整的 clean, default, site build lifecycle

  • clean lifecycle
phase function
pre-clean execute execute processes needed prior to the actual project cleaning
clean remove all files generated by the previous build
post-clean execute processes needed to finalize the project cleaning
  • default lifecycle
phase function
validate validate the project is correct and all necessary information is available.
initialize initialize build state, e.g. set properties or create directories.
generate-sources generate any source code for inclusion in compilation.
process-sources process the source code, for example to filter any values.
generate-resources generate resources for inclusion in the package.
process-resources copy and process the resources into the destination directory, ready for packaging.
compile compile the source code of the project.
process-classes post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sources generate any test source code for inclusion in compilation.
process-test-sources process the test source code, for example to filter any values.
generate-test-resources create resources for testing.
process-test-resources copy and process the resources into the test destination directory.
test-compile compile the test source code into the test destination directory
process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package take the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify run any checks to verify the package is valid and meets quality criteria.
install install the package into the local repository, for use as a dependency in other projects locally.
deploy done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
  • site lifecycle
phase function
pre-site execute processes needed prior to the actual project site generation
site generate the project's site documentation
post-site execute processes needed to finalize the site generation, and to prepare for site deployment
site-deploy deploy the generated site documentation to the specified web server

6. 参考文档

参考

maven 入门指南
maven 生命周期
Maven 默认插件以及功能
maven 依赖管理
maven-shade-plugin 入门指南
maven-assembly-plugin 入门指南
Introduction to the Build Lifecycle

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 所有项目的构建都是有生命周期的,这个生命周期包括:项目清理、初始化、编译、测试、打包、集成测试、验证、部署、站点生...
    zlcook阅读 2,691评论 0 21
  • 1.生命周期 maven有三个独立的有序的生命周期,分别是clean,default和site。其中clean(用...
    酱油和醋阅读 1,065评论 0 0
  • 【补充中】通常一个Java Web项目构建过程包括清理、编译、测试、打包、集成测试、验证、部署等步骤,Maven从...
    泡泡大脚阅读 327评论 0 0
  • 杀人者恒杀之,所以他已死! 死得蹊跷,死得诡异,死得其所! 事实上,这却是讽刺,纵横天下的他竟是自杀,天...
    小狗汪汪汪阅读 328评论 0 0