maven的生命周期
maven的生命周期是抽象的,其实际行为都由插件来完成,引入maven 的 生命周期就是为了对所有的构建过程进行抽象和统一。
这种方式类似于模板方法,模板方法模式在父类中定义算法的整体结构,子类可以通过实现或重写父类方法来控制实际的行为,这样既保证了算法有足够的可扩展性,又能够严格控制算法的整体结构;
maven的声明周期定义了构建的各个steps,定义了他们的次序,具体由插件来实现这些step。
maven为大多数step编写和绑定了默认的插件。
三套生命周期
  • clean 清理项目
  • default 构建项目
  • site 建立项目站点

命令行与生命周期

  • 命令行用于调用maven的生命周期阶段,如mvn clean、mvn test、mvn clean install、mvn clean deploy site-deploy
插件目标
2)对于插件: 为了能够复用代码,它往往能够完成多个任务;(干货——一个插件完成多个任务)
3)因为任务背后有很多可以复用的代码,即不同任务的实现代码有一部分是相同的,可以复用的,因此,多个功能聚集在一个插件里,每个功能就是一个插件目标;(干货——一个插件有多个功能,有多个目标,一个功能 == 一个目标)
4)看个荔枝: maven-dependency-plugin有多个目标
4.1)intro: maven-dependency-plugin有多个目标,每个目标对应一个功能,上述提到的几个功能分别对应的插件目标为 dependency:analyze, dependency:tree 和 dependency:list;
4.2)这是一种通用的写法: 冒号前面是 插件前缀,后面是该插件的目标;(干货——引入了插件前缀)
【4】插件绑定
1)intro:具体而言,是生命周期的阶段与 插件的目标相互绑定,以完成某个具体的构建任务;如下图所示:
【4.1】内置绑定
1)intro:为了让用户几乎不用任何配置就构建maven 项目,maven 在核心为一些主要的生命周期绑定了很多插件的目标;
2)clean 和 site 生命周期 阶段与插件目标的绑定关系如下图所示:
3)default 生命周期 阶段与插件目标的绑定关系如下图所示:
对上表的分析(Analysis)
A1)default 与插件目标的绑定关系是由 项目打包类型所决定的,打包类型是通过 POM 中的 packaging 元素定义的;
A2)除了默认的 jar 打包类型外,常见的打包类型还有 war,pom,maven-plugin,ear等;
4)看个荔枝: mvn clean install, maven 会输出包含了生命周期阶段与 插件的绑定关系
[cpp] view plaincopy
  1. D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean install
  2. [INFO] Scanning for projects...
  3. [WARNING]
  4. [WARNING] Some problems were encountered while building the effective model for com.maven.chapter3:service:jar:1.0-SNAPSHOT
  5. [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 15
  6. [WARNING]
  7. [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
  8. [WARNING]
  9. [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
  10. [WARNING]
  11. [INFO]
  12. [INFO] ------------------------------------------------------------------------
  13. [INFO] Building service says hello maven. 1.0-SNAPSHOT
  14. [INFO] ------------------------------------------------------------------------
  15. [INFO]
  16. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service ---
  17. [INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target
  18. [INFO]
  19. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service ---
  20. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  21. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources
  22. [INFO]
  23. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service ---
  24. [INFO] Changes detected - recompiling the module!
  25. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
  26. [INFO] Compiling 1 source file to D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes
  27. [INFO]
  28. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service ---
  29. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  30. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources
  31. [INFO]
  32. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service ---
  33. [INFO] Changes detected - recompiling the module!
  34. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
  35. [INFO] Compiling 1 source file to D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes
  36. [INFO]
  37. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service ---
  38. [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports
  39. -------------------------------------------------------
  40. T E S T S
  41. -------------------------------------------------------
  42. Running com.maven.chapter3.service.HelloTest
  43. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 sec
  44. Results :
  45. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  46. [INFO]
  47. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---
  48. [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar
  49. [INFO]
  50. [INFO] --- maven-shade-plugin:1.2.1:shade (default) @ service ---
  51. [INFO] Replacing original artifact with shaded artifact.
  52. [INFO] Replacing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar with D:\classical_books\java_set\maven_in
  53. _action\mycode\chapter3\target\service-1.0-SNAPSHOT-shaded.jar
  54. [INFO]
  55. [INFO] --- maven-install-plugin:2.4:install (default-install) @ service ---
  56. [INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar to D:\classical_books\java_set\maven_in_
  57. action\local_repo\com\maven\chapter3\service\1.0-SNAPSHOT\service-1.0-SNAPSHOT.jar
  58. [INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\pom.xml to D:\classical_books\java_set\maven_in_action\local_repo\com\ma
  59. ven\chapter3\service\1.0-SNAPSHOT\service-1.0-SNAPSHOT.pom
  60. [INFO] ------------------------------------------------------------------------
  61. [INFO] BUILD SUCCESS
  62. [INFO] ------------------------------------------------------------------------
  63. [INFO] Total time: 8.305 s
  64. [INFO] Finished at: 2016-06-22T18:43:41+08:00
  65. [INFO] Final Memory: 17M/142M
  66. [INFO] ------------------------------------------------------------------------
【4.2】自定义绑定
1)intro: 除了内置绑定外,用户可以自己选择将某个 目标绑定到 生命周期的某个阶段上;
2)看个荔枝:创建项目的源码jar 包,内置的插件绑定关系中并没有内置这一项任务,因此需要用户自行配置。 maven-source-plugin 可以帮助我们完成这项任务;它的 jar-no-fork 目标能够将项目的主代码打包成jar 文件,可以将其绑定到 default 生命周期的verify 阶段上;
[html] view plaincopy
  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-source-plugin</artifactId>
  4. <version>2.1.1</version>
  5. <executions>
  6. <execution>
  7. <id>attach-sources</id>
  8. <phase>verify</phase>
  9. <goals>
  10. <goal>jar-no-fork</goal>
  11. </goals>
  12. </execution>
  13. </executions>
  14. </plugin>
对以上代码的分析(Analysis):
A1)executions 下每个execution 子元素可以用来配置执行一个任务;
A2)该例中配置了一个id 为 attach-sources 的 任务,通过 phrase 配置,将其绑定到 verify 生命周期阶段上,再通过 goal 配置指定要执行的插件 目标,至此,自定义插件配置完成了;(干货——总结了自定义插件配置的steps)
3)运行 mvn verify 看如下输出:
[cpp] view plaincopy
  1. D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn verify
  2. [INFO] Scanning for projects...
  3. [WARNING]
  4. [WARNING] Some problems were encountered while building the effective model for com.maven.chapter3:service:jar:1.0-SNAPSHOT
  5. [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 15
  6. [WARNING]
  7. [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
  8. [WARNING]
  9. [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
  10. [WARNING]
  11. [INFO]
  12. [INFO] ------------------------------------------------------------------------
  13. [INFO] Building service says hello maven. 1.0-SNAPSHOT
  14. [INFO] ------------------------------------------------------------------------
  15. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-source-plugin/2.1.1/maven-source-plugin-2.1.1.pom
  16. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-source-plugin/2.1.1/maven-source-plugin-2.1.1.pom (5 KB at 2.6 KB/sec)
  17. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/14/maven-plugins-14.pom
  18. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/14/maven-plugins-14.pom (13 KB at 32.4 KB/sec)
  19. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-source-plugin/2.1.1/maven-source-plugin-2.1.1.jar
  20. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-source-plugin/2.1.1/maven-source-plugin-2.1.1.jar (24 KB at 59.4 KB/se
  21. c)
  22. [INFO]
  23. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service ---
  24. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  25. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources
  26. [INFO]
  27. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service ---
  28. [INFO] Nothing to compile - all classes are up to date
  29. [INFO]
  30. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service ---
  31. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  32. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources
  33. [INFO]
  34. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service ---
  35. [INFO] Nothing to compile - all classes are up to date
  36. [INFO]
  37. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service ---
  38. [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports
  39. -------------------------------------------------------
  40. T E S T S
  41. -------------------------------------------------------
  42. Running com.maven.chapter3.service.HelloTest
  43. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.056 sec
  44. Results :
  45. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  46. [INFO]
  47. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---
  48. [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar
  49. [INFO]
  50. [INFO] --- maven-shade-plugin:1.2.1:shade (default) @ service ---
  51. [INFO] Replacing original artifact with shaded artifact.
  52. [INFO] Replacing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar with D:\classical_books\java_set\maven_in
  53. _action\mycode\chapter3\target\service-1.0-SNAPSHOT-shaded.jar
  54. [INFO]
  55. [INFO] --- maven-source-plugin:2.1.1:jar-no-fork (attach-sources) @ service ---  // maven-source-plugin:jar-no-fork 会得以执行.
  56. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4/maven-archiver-2.4.pom
  57. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4/maven-archiver-2.4.pom (4 KB at 9.1 KB/sec)
  58. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/10/maven-shared-components-10.pom
  59. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/10/maven-shared-components-10.pom (9 KB at 19.0 KB/se
  60. c)
  61. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.pom
  62. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.pom (723 B at 1.8 KB/sec)
  63. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom
  64. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom (9 KB at 21.5 KB/sec)
  65. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0/maven-model-2.0.pom
  66. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0/maven-model-2.0.pom (3 KB at 6.2 KB/sec)
  67. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0/maven-project-2.0.pom
  68. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0/maven-project-2.0.pom (2 KB at 4.1 KB/sec)
  69. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0/maven-profile-2.0.pom
  70. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0/maven-profile-2.0.pom (2 KB at 3.6 KB/sec)
  71. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0/maven-artifact-manager-2.0.pom
  72. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0/maven-artifact-manager-2.0.pom (2 KB at 3.6 KB/sec)
  73. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0/maven-repository-metadata-2.0.pom
  74. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0/maven-repository-metadata-2.0.pom (2 KB at 3.1 KB/sec)
  75. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-11/plexus-archiver-1.0-alpha-11.pom
  76. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-11/plexus-archiver-1.0-alpha-11.pom (2 KB at 4.5 KB/sec
  77. )
  78. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.9/plexus-components-1.1.9.pom
  79. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.9/plexus-components-1.1.9.pom (3 KB at 6.3 KB/sec)
  80. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.10/plexus-1.0.10.pom
  81. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.10/plexus-1.0.10.pom (9 KB at 21.0 KB/sec)
  82. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-15/plexus-container-default-1.0-alpha-15.pom
  83. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-15/plexus-container-default-1.0-alpha-15.pom (
  84. 2 KB at 4.1 KB/sec)
  85. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-containers/1.0-alpha-15/plexus-containers-1.0-alpha-15.pom
  86. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-containers/1.0-alpha-15/plexus-containers-1.0-alpha-15.pom (2 KB at 4.9 KB
  87. /sec)
  88. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.9/plexus-1.0.9.pom
  89. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.9/plexus-1.0.9.pom (8 KB at 19.9 KB/sec)
  90. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-component-api/1.0-alpha-15/plexus-component-api-1.0-alpha-15.pom
  91. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-component-api/1.0-alpha-15/plexus-component-api-1.0-alpha-15.pom (948 B at
  92. 2.4 KB/sec)
  93. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/1.2-alpha-6/plexus-classworlds-1.2-alpha-6.pom
  94. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/1.2-alpha-6/plexus-classworlds-1.2-alpha-6.pom (3 KB at 6.1 KB
  95. /sec)
  96. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.3/plexus-utils-1.3.pom
  97. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.3/plexus-utils-1.3.pom (2 KB at 2.6 KB/sec)
  98. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-3/plexus-io-1.0-alpha-3.pom
  99. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-3/plexus-io-1.0-alpha-3.pom (2 KB at 3.7 KB/sec)
  100. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-component-api/1.0-alpha-16/plexus-component-api-1.0-alpha-16.pom
  101. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-component-api/1.0-alpha-16/plexus-component-api-1.0-alpha-16.pom (3 KB at
  102. 5.7 KB/sec)
  103. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-containers/1.0-alpha-16/plexus-containers-1.0-alpha-16.pom
  104. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-containers/1.0-alpha-16/plexus-containers-1.0-alpha-16.pom (2 KB at 4.8 KB
  105. /sec)
  106. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/1.2-alpha-7/plexus-classworlds-1.2-alpha-7.pom
  107. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/1.2-alpha-7/plexus-classworlds-1.2-alpha-7.pom (3 KB at 6.1 KB
  108. /sec)
  109. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.4.9/plexus-utils-1.4.9.pom
  110. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.4.9/plexus-utils-1.4.9.pom (3 KB at 5.8 KB/sec)
  111. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.6/plexus-interpolation-1.6.pom
  112. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.6/plexus-interpolation-1.6.pom (3 KB at 7.1 KB/sec)
  113. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-9/plexus-archiver-1.0-alpha-9.pom
  114. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-9/plexus-archiver-1.0-alpha-9.pom (2 KB at 4.7 KB/sec)
  115. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-1/plexus-io-1.0-alpha-1.pom
  116. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-1/plexus-io-1.0-alpha-1.pom (2 KB at 3.3 KB/sec)
  117. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4/maven-archiver-2.4.jar
  118. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar
  119. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-9/plexus-archiver-1.0-alpha-9.jar
  120. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-1/plexus-io-1.0-alpha-1.jar
  121. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.6/plexus-interpolation-1.6.jar
  122. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4/maven-archiver-2.4.jar (20 KB at 46.2 KB/sec)
  123. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-1/plexus-io-1.0-alpha-1.jar (12 KB at 15.5 KB/sec)
  124. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.6/plexus-interpolation-1.6.jar (50 KB at 43.3 KB/sec)
  125. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-9/plexus-archiver-1.0-alpha-9.jar (154 KB at 116.1 KB/s
  126. ec)
  127. Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar (262 KB at 95.3 KB/sec)
  128. [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT-sources.jar // 生成了源码文件.
  129. [INFO] ------------------------------------------------------------------------
  130. [INFO] BUILD SUCCESS
  131. [INFO] ------------------------------------------------------------------------
  132. [INFO] Total time: 18.822 s
  133. [INFO] Finished at: 2016-06-22T19:00:32+08:00
  134. [INFO] Final Memory: 13M/171M
  135. [INFO] ------------------------------------------------------------------------
对以上console info的分析(Analysis):可以看到, 当执行 verify 生命周期阶段的时候,maven-source-plugin:jar-no-fork 会得以执行,它会创建一个以 -sources.jar 结尾的源码文件包;
4)problem+solutions:
4.1)problem:当插件目标被绑定到不同的生命周期的时候,其执行顺序会由生命周期阶段的先后顺序决定;当多个目标被绑定到同一阶段时,他们的执行顺序会是怎样?
4.2)solutions:当多个插件目标绑定到同一个阶段的时候,这些插件声明的先后顺序决定了目标的执行顺序;
【5】插件配置
1)intro:几乎所有maven 插件的目标都有一些可配置的参数,用户可以通过 命令行 和 POM 配置等方式来配置这些参数;
【5.1】 命令行插件配置
1)problem+solutions:
1.1)problem:如何从命令行配置插件?
1.2)solutions:用户可以在maven 命令中使用 -D 参数,并伴随一个参数键=参数值的形式,来配置插件目标的参数;参数-D 是java 自带的,其功能是通过命令行设置一个 java 系统属性;(干货——参数-D 是java 自带的,其功能是通过命令行设置一个 java 系统属性)
2)看个荔枝: mvn install-Dmaven.test.skip=true : 跳过测试;
【5.2】POM 中插件全局配置
1)intro:在pom 文件中一次性配置显然要比在命令行中输入参数要方便;
2)用户可以在声明插件的时候,对此插件进行一个全局配置。也就是说,所有基于该插件目标的任务,都会使用这些配置;
3)看个荔枝:告诉maven 基于 jdk1.8 编译该文件.
[java] view plaincopy
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-compiler-plugin</artifactId>
  6. <configuration>
  7. <source>1.8</source>
  8. <target>1.8</target>
  9. </configuration>
  10. </plugin>
【5.3】 POM中插件任务配置
1)intro:用户可以为某个插件任务配置特定的参数;
2)看个荔枝:
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/maven-v4_0_0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.maven.chapter3</groupId>
  8. <artifactId>service</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <name>service says hello maven.</name>
  11. <dependencies>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.7</version>
  16. <scope>test</scope>
  17. </dependency>
  18. </dependencies>
  19. <build>
  20. <plugins>
  21. <plugin>
  22. <groupId>org.apache.maven.plugins</groupId>
  23. <artifactId>maven-compiler-plugin</artifactId>
  24. <configuration>
  25. <source>1.8</source>
  26. <target>1.8</target>
  27. </configuration>
  28. </plugin>
  29. <plugin>
  30. <groupId>org.apache.maven.plugins</groupId>
  31. <artifactId>maven-shade-plugin</artifactId>
  32. <version>1.2.1</version>
  33. <executions>
  34. <execution>
  35. <phase>package</phase>
  36. <goals>
  37. <goal>shade</goal>
  38. </goals>
  39. <configuration>
  40. <transformers>
  41. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  42. <mainClass>com.maven.chapter3.service.Hello</mainClass>
  43. </transformer>
  44. </transformers>
  45. </configuration>
  46. </execution>
  47. </executions>
  48. </plugin>
  49. <plugin>
  50. <groupId>org.apache.maven.plugins</groupId>
  51. <artifactId>maven-source-plugin</artifactId>
  52. <version>2.1.1</version>
  53. <executions>
  54. <execution>
  55. <id>attach-sources</id>
  56. <phase>verify</phase>
  57. <goals>
  58. <goal>jar-no-fork</goal>
  59. </goals>
  60. </execution>
  61. </executions>
  62. </plugin>
  63. <plugin>
  64. <groupId>org.apache.maven.plugins</groupId>
  65. <artifactId>maven-antrun-plugin</artifactId>
  66. <version>1.3</version>
  67. <executions>
  68. <execution>
  69. <id>ant-validate</id>
  70. <phase>validate</phase>
  71. <goals>
  72. <goal>run</goal>
  73. </goals>
  74. <configuration>
  75. <tasks>
  76. <echo>i am bound to validate phase.</echo>
  77. </tasks>
  78. </configuration>
  79. </execution>
  80. <execution>
  81. <id>ant-verify</id>
  82. <phase>verify</phase>
  83. <goals>
  84. <goal>run</goal>
  85. </goals>
  86. <configuration>
  87. <tasks>
  88. <echo>i am bound to verify phase.</echo>
  89. </tasks>
  90. </configuration>
  91. </execution>
  92. </executions>
  93. </plugin>
  94. </plugins>
  95. </build>
  96. </project>
对以上代码的分析(Analysis):
A1)maven-antrun-plugin:run 与 validate 阶段绑定,从而构成了一个id为 ant-validate 的任务;
A2)插件 全局配置中的 configuration 元素位于 plugin 元素下面,而这里的configuration元素则位于 execution 元素下,表示这是 特定任务的配置,而非插件整体的配置;
A3)ant-validate任务配置了一个 echo 任务,而id为 ant-verify 任务也配置了一个 echo 任务;
【6】获取插件信息
1)intro:当遇到一个构件任务的时候,用户还需要知道到哪里去寻找合适的插件,以帮助完成任务;
【6.1】在线插件信息
1)intro:基本上所有的插件都来自 apache 和 codehaus;
2)插件详细的列表在这个地址得到:
http://maven.apache.org/plugins/index.html
http://repo1.maven.org/maven2/org/apache/maven/plugins/
3)看个荔枝:以 maven-surefire-plugin 为例,访问 https://maven.apache.org/surefire/maven-surefire-plugin/, 可以看到该插件的简要intro, 包含的目标,使用介绍;
Attention)并不是所有插件目标参数都有表达式,也就是说,一些插件目标参数只能在 POM 中配置;
【6.2】使用 maven-help-plugin 描述插件
1)intro:运行以下命令来获取  maven-compiler-plugin2.1  版本的信息:
2)值得一提的是 目标前缀:其作用是方便在命令行直接运行插件;(干货——引入了目标前缀)
2.1)看个荔枝: maven-compiler-plugin 的目标前缀是 compiler。
2.2)在描述插件的时候,还可以省去版本信息,让 maven 自动获取最新版本来进行表述, 如: mvn help:describe-Dplugin=org.apache.maven.plugins:maven-compiler-plugin
2.3)进一步简化:可以使用插件目标前缀来替换坐标: mvn help:describe-Dplugin=compiler
2.4)想仅仅描述某个插件目标的信息,可以加上 goal 参数:
[cpp] view plaincopy
  1. mvn help:describe-Dplugin=compiler-Dgoal=compile
2.5)想让  maven-help-plugin 输出更详细的信息,可以加上 detail参数:
[cpp] view plaincopy
  1. mvn help:describe-Dplugin=compiler-Ddetail
【7】 从命令行调用插件
1)intro:运行mvn -h 来显示 mvn 命令帮助:
[cpp] view plaincopy
  1. D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn -h
  2. usage: mvn [options] [<goal(s)>] [<phase(s)>]
2)我们可以通过mvn 命令激活生命周期阶段,从而执行那些绑定在生命周期阶段上的插件目标;
3)maven 还支持:直接从命令行调用插件目标,因为这些任何不适合绑定在生命周期上,这些插件目标应该通过如下方式使用:
[cpp] view plaincopy
  1. mvn help:describe-Dplugin=compiler
  2. mvn dependency:tree

4)problem+solutions:

4.1)problem:为什么不是 mavnen-dependency-plugin:tree 而是 dependency:tree ?
4.2)solutions:可以尝试如下命令:
[cpp] view plaincopy
  1. mvn org.apache.maven.plugins:maven-help-plugin:2.1:describe-Dplugin=compiler
  2. mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:tree
4.2.1)上面的命令一对比,显然 前面的目录更加简洁,更容易使用;
4.2.2)这也是 maven 引入 目标前缀的原因: help 是 maven-help-plugin 的目标前缀, 而 dependency 是 maven-dependency-plugin 的前缀,有了插件前缀,maven 就能够找到对应的 artifactId;不过 除了 artifactId ,maven 还需要得到 groupId 和 version 才能确定到 某个插件;
【8】插件解析机制
【8.1】插件仓库
1)intro:与依赖构建一样,插件构件同样基于坐标存储在 maven 仓库中:在需要的时候,maven 会从本地仓库中寻找插件,如果不存在,则从远程插件仓库查找。找到插件后,再下载到 本地仓库使用;
2)maven 会 区别对待依赖的远程仓库与插件的远程仓库:当maven 需要的依赖在本地仓库不存在时,它会去所配置的远程仓库中查找,可是当 maven 需要的插件在本地仓库中不存在时,它就不会去 这些远程仓库中查找;
3)插件的远程仓库使用 pluginRepositories 和 pluginRepository 配置,maven 内置了如下的插件远程仓库配置;
【8.2】插件的默认 groupId
1)intro:在 pom 中配置插件的时候,如果该插件是 maven 的官方插件,就可以省略 groupId 配置,见如下代码:(Attention——原书作者不推荐这种做法,即望补全groupId)
【8.3】解析插件版本
1)intro:maven 在超级POM 中为所有核心插件设定了版本,超级 POM 是所有maven 项目的父 POM,所有项目都继承这个超级 POM 的配置,因此,即使用户不加任何配置,maven 使用核心插件的时候,他们的版本就已经确定了;
2) 如果用户使用某个插件的时候没有设定版本,而这个插件又不属于 核心插件的范畴, maven 就会去 检查所有 仓库中可用的版本,然后做出选择;
3)依赖maven 解析插件版本是不推荐的做法: 即使 maven3 将版本解析到最新的非快照版,其还是存在潜在的不稳定性;
【8.4】 解析插件前缀
1)intro:maven 如何 根据插件前缀解析得到插件的坐标;
2)插件前缀用户 groupId:artifactId 是一一对应的,这种匹配关系存储在仓库元数据中;
3)maven在解析插件仓库元数据的时候,会默认使用 org.apache.maven.plugins 和 org.codehaus.mojo 两个 groupId。也可以通过配置 settings.xml 让 maven 检查其他groupId 上的 插件仓库元数据:
[html] view plaincopy
  1. <settings>
  2. <pluginGroups>
  3. <pluginGroup>com.your.plugins</pluginGroup>
  4. </pluginGroups>
  5. </settings>

对以上代码的分析(Analysis): maven 不仅仅会检查 org/apache/maven/plugins/maven-metadata.xml 和  org/codehaus/mojo/maven-metadata.xml ,还会检查 com/your/plugins/maven-metadata.xml;

4)看看插件仓库元数据的内容,如下:
对以上代码的分析(Analysis): 
A1)上述内容是从 中央仓库的 org.apche.maven.plugins groupId 下插件仓库元数据中截取的一些片段,从这段数据中就能看到 maven-clean-plugin 的前缀为 clean,maven-compile-plugin 的前缀为 compiler, maven-dependency-plugin 的前缀为 dependency;
A2)当maven 解析到 dependency:tree 这样的命令后,他首先基于 默认的 groupId 归并所有插件仓库的元数据 org/apache/maven/plugins/maven-metadata.xml; 其次检查归并后的元数据, 找到对应的 artifactId 为 maven-dependency-plugin; 然后结合当前元数据的 groupId org.apache.maven.plugins;最后使用 章节8.3 的方法解析得到 version,从而得到完整的插件坐标;

maven入门(1-3)maven的生命周期相关推荐

  1. Maven入门指南⑦:Maven的生命周期和插件

    Maven入门指南⑦:Maven的生命周期和插件 一个完整的项目构建过程通常包括清理.编译.测试.打包.集成测试.验证.部署等步骤,Maven从中抽取了一套完善的.易扩展的生命周期.Maven的生命周 ...

  2. maven白小白(二)生命周期complie,package,install

    目录 一.Maven对项目进行构建的生命周期 二.例子: 一.Maven对项目进行构建的生命周期 (构建:生产一个可以运行项目的过程) clean清理:清理的是class字节码文件,为下一次编译做准备 ...

  3. android生命周期方法,Android零基础入门|Activity状态和生命周期方法

    原标题:Android零基础入门|Activity状态和生命周期方法 前面两期我们学习了Activity的创建和注册.以及启动和关闭,也学会了重写onCraete方法,这些知识在实际开发中远远不够,还 ...

  4. 【Maven入门教程】Maven的基本概念

    一.POM(Project Object Model)项目对象模型 Pom在Maven中是一个XML文件,位于项目的根目录下,其包含着项目构建所需要的必要信息,Pom还支持继承,当一个项目中拥有多个模 ...

  5. Servlet第一篇【介绍Servlet、HTTP协议、WEB目录结构、编写入门Servlet程序、Servlet生命周期】...

    tags: Servlet 什么是Serlvet?# Servlet其实就是一个遵循Servlet开发的java类.Serlvet是由服务器调用的,运行在服务器端. 为什么要用到Serlvet? 我们 ...

  6. Servlet第一篇【介绍Servlet、HTTP协议、WEB目录结构、编写入门Servlet程序、Servlet生命周期】

    什么是Servlet? Servlet其实就是一个遵循Servlet开发的java类.Servlet是由服务器调用的,运行在服务器端. 为什么要用到Servlet? 我们编写java程序想要在网上实现 ...

  7. spring入门第二讲 bean的生命周期 装配方式 Spring整合Junit

    bean的生命周期 实体类 //初始化 public void init(){System.out.println("--初始化--"); }//销毁 public void de ...

  8. Maven入门指南① :Maven 快速入门及简单使用

    原文链接:http://www.cnblogs.com/luotaoyeah/archive/2014/06/02/3764533.html 开发环境 MyEclipse 2014 JDK 1.8 M ...

  9. Maven入门指南② :Maven 常用命令,手动创建第一个 Maven 项目

    1.根据 Maven 的约定,我们在D盘根目录手动创建如下目录及文件结构: 2.打开pom.xml文件,添加如下内容: 1 <project xmlns="http://maven.a ...

  10. zeebe入门课程16-工作流生命周期3(Conditions)条件

    zeebe中条件表达式使用.条件可用于条件流以确定以下任务. 条件是一个布尔表达式,具有类似于javascript的语法.它允许将工作流实例的变量与其他变量或文本(如数字.字符串等)进行比较. 工作流 ...

最新文章

  1. 对IsUnderPostmaster变量初步学习
  2. 测试一下,你能小学毕业吗?
  3. 我国在计算机科学领域故事,我国巨型计算机领域的一代天骄
  4. 计算机视觉工具包Luminoth
  5. 用Java语言,写一个植物大战僵尸简易版!
  6. (JAVA)TreeSet
  7. WordPress前台后台页面打开慢的解决方法
  8. python服务器环境搭建(2)——安装相关软件
  9. 针对IE的CSS hack 全面 实用
  10. C#- Chart Controls (转)
  11. 推理集 —— 特殊的时间
  12. linux中awk命令_Linux / Unix中的AWK命令
  13. trigger 根据绑定到匹配元素的给定的事件类型执行所有的处理程序和行为。
  14. Listary与QTtabbar整合
  15. 2018今日头条内推笔试1
  16. 登录失败 12306服务器不稳定,为什么12306登陆失败
  17. 手机android app 无线控制led灯开关
  18. 环游世界,走遍读过的每一个国家和城镇
  19. js 数字金额的转换 (转)
  20. 关于程序化交易系统的交易体系

热门文章

  1. 理解 JavaScript 作用域和作用域链
  2. 自己整合优化的一个Android框架
  3. XunSearch中常用方法整合
  4. grep awk sed练习
  5. Windows 8测试版安装图组
  6. 使用repeater,遍历数据,不规则排序,不同的样式之间切换
  7. 标题: Debian 下 VirtualBox 的桥接、USB 设置 ── 迷你怎么做 [转自sir]
  8. 二层交换机的六条安全秘诀
  9. 5G 信令流程 — 5GC 的连接管理(CM,Connection Management)
  10. 药师帮完成1.33亿美元D轮融资,投资方为老虎环球基金、H Capital和DCM