maven入門(1-3)maven的生命週期

 
maven的生命週期
maven的生命週期是抽象的,其實際行爲都由插件來完成,引入maven 的 生命週期就是爲了對全部的構建過程進行抽象和統一。
這種方式相似於模板方法,模板方法模式在父類中定義算法的總體結構,子類能夠經過實現或重寫父類方法來控制實際的行爲,這樣既保證了算法有足夠的可擴展性,又可以嚴格控制算法的總體結構;
 
maven的聲明週期定義了構建的各個steps,定義了他們的次序,具體由插件來實現這些step。
maven爲大多數step編寫和綁定了默認的插件。
 
三套生命週期
  • clean 清理項目
  • default 構建項目
  • site 創建項目站點

命令行與生命週期html

  • 命令行用於調用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 plain  copy
 
  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 plain  copy
 
  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 plain  copy
 
  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.   
  18. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/14/maven-plugins-14.pom  
  19. 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)  
  20. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-source-plugin/2.1.1/maven-source-plugin-2.1.1.jar  
  21. 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  
  22. c)  
  23. [INFO]  
  24. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service ---  
  25. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  
  26. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources  
  27. [INFO]  
  28. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service ---  
  29. [INFO] Nothing to compile - all classes are up to date  
  30. [INFO]  
  31. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service ---  
  32. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  
  33. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources  
  34. [INFO]  
  35. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service ---  
  36. [INFO] Nothing to compile - all classes are up to date  
  37. [INFO]  
  38. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service ---  
  39. [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports  
  40.   
  41. -------------------------------------------------------  
  42.  T E S T S  
  43. -------------------------------------------------------  
  44. Running com.maven.chapter3.service.HelloTest  
  45. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.056 sec  
  46.   
  47. Results :  
  48.   
  49. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  
  50.   
  51. [INFO]  
  52. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---  
  53. [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar  
  54. [INFO]  
  55. [INFO] --- maven-shade-plugin:1.2.1:shade (default) @ service ---  
  56. [INFO] Replacing original artifact with shaded artifact.  
  57. [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  
  58. _action\mycode\chapter3\target\service-1.0-SNAPSHOT-shaded.jar  
  59. [INFO]  
  60. [INFO] --- maven-source-plugin:2.1.1:jar-no-fork (attach-sources) @ service ---  // maven-source-plugin:jar-no-fork 會得以執行.  
  61. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4/maven-archiver-2.4.pom  
  62. 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)  
  63. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/10/maven-shared-components-10.pom  
  64. 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  
  65. c)  
  66. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.pom  
  67. 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)  
  68. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom  
  69. Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom (9 KB at 21.5 KB/sec)  
  70. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-model/2.0/maven-model-2.0.pom  
  71. 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)  
  72. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-project/2.0/maven-project-2.0.pom  
  73. 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)  
  74. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-profile/2.0/maven-profile-2.0.pom  
  75. 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)  
  76. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact-manager/2.0/maven-artifact-manager-2.0.pom  
  77. 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)  
  78. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-repository-metadata/2.0/maven-repository-metadata-2.0.pom  
  79. 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)  
  80.   
  81. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-11/plexus-archiver-1.0-alpha-11.pom  
  82. 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  
  83. )  
  84. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.9/plexus-components-1.1.9.pom  
  85. 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)  
  86. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.10/plexus-1.0.10.pom  
  87. 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)  
  88. 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  
  89. 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 (  
  90. 2 KB at 4.1 KB/sec)  
  91. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-containers/1.0-alpha-15/plexus-containers-1.0-alpha-15.pom  
  92. 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  
  93. /sec)  
  94. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/1.0.9/plexus-1.0.9.pom  
  95. 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)  
  96. 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  
  97. 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  
  98.  2.4 KB/sec)  
  99. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/1.2-alpha-6/plexus-classworlds-1.2-alpha-6.pom  
  100. 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  
  101. /sec)  
  102. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.3/plexus-utils-1.3.pom  
  103. 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)  
  104. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-3/plexus-io-1.0-alpha-3.pom  
  105. 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)  
  106. 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  
  107. 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  
  108. 5.7 KB/sec)  
  109. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-containers/1.0-alpha-16/plexus-containers-1.0-alpha-16.pom  
  110. 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  
  111. /sec)  
  112. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-classworlds/1.2-alpha-7/plexus-classworlds-1.2-alpha-7.pom  
  113. 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  
  114. /sec)  
  115. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.4.9/plexus-utils-1.4.9.pom  
  116. 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)  
  117. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.6/plexus-interpolation-1.6.pom  
  118. 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)  
  119. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-9/plexus-archiver-1.0-alpha-9.pom  
  120. 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)  
  121. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-1/plexus-io-1.0-alpha-1.pom  
  122. 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)  
  123. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4/maven-archiver-2.4.jar  
  124. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.8/plexus-utils-1.5.8.jar  
  125. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/1.0-alpha-9/plexus-archiver-1.0-alpha-9.jar  
  126. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/1.0-alpha-1/plexus-io-1.0-alpha-1.jar  
  127. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.6/plexus-interpolation-1.6.jar  
  128. 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)  
  129. 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)  
  130. 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)  
  131. 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  
  132. ec)  
  133. 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)  
  134. [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT-sources.jar // 生成了源碼文件.  
  135. [INFO] ------------------------------------------------------------------------  
  136. [INFO] BUILD SUCCESS  
  137. [INFO] ------------------------------------------------------------------------  
  138. [INFO] Total time: 18.822 s  
  139. [INFO] Finished at: 2016-06-22T19:00:32+08:00  
  140. [INFO] Final Memory: 13M/171M  
  141. [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 plain  copy
 
  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 plain  copy
 
  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.   
  7. <modelVersion>4.0.0</modelVersion>  
  8. <groupId>com.maven.chapter3</groupId>  
  9. <artifactId>service</artifactId>  
  10. <version>1.0-SNAPSHOT</version>  
  11. <name>service says hello maven.</name>  
  12. <dependencies>  
  13. <dependency>  
  14. <groupId>junit</groupId>  
  15. <artifactId>junit</artifactId>  
  16. <version>4.7</version>  
  17. <scope>test</scope>  
  18. </dependency>  
  19. </dependencies>  
  20. <build>  
  21.     <plugins>  
  22.      <plugin>  
  23.        <groupId>org.apache.maven.plugins</groupId>  
  24.        <artifactId>maven-compiler-plugin</artifactId>  
  25.        <configuration>  
  26.          <source>1.8</source>  
  27.          <target>1.8</target>  
  28.        </configuration>  
  29.      </plugin>  
  30.      <plugin>  
  31.        <groupId>org.apache.maven.plugins</groupId>  
  32.        <artifactId>maven-shade-plugin</artifactId>  
  33.        <version>1.2.1</version>  
  34.        <executions>  
  35.          <execution>  
  36.            <phase>package</phase>  
  37.            <goals>  
  38.              <goal>shade</goal>  
  39.            </goals>  
  40.            <configuration>   
  41.              <transformers>  
  42.                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  43.                  <mainClass>com.maven.chapter3.service.Hello</mainClass>  
  44.                </transformer>  
  45.              </transformers>  
  46.            </configuration>  
  47.          </execution>  
  48.        </executions>  
  49.      </plugin>  
  50.      <plugin>  
  51.       <groupId>org.apache.maven.plugins</groupId>  
  52.        <artifactId>maven-source-plugin</artifactId>  
  53.        <version>2.1.1</version>  
  54.        <executions>  
  55.          <execution>  
  56.           <id>attach-sources</id>  
  57.            <phase>verify</phase>  
  58.            <goals>  
  59.              <goal>jar-no-fork</goal>  
  60.            </goals>  
  61.          </execution>  
  62.        </executions>  
  63.      </plugin>  
  64.      <plugin>  
  65.       <groupId>org.apache.maven.plugins</groupId>  
  66.        <artifactId>maven-antrun-plugin</artifactId>  
  67.        <version>1.3</version>  
  68.        <executions>  
  69.          <execution>  
  70.           <id>ant-validate</id>  
  71.            <phase>validate</phase>  
  72.            <goals>  
  73.              <goal>run</goal>  
  74.            </goals>  
  75.            <configuration>  
  76.             <tasks>  
  77.             <echo>i am bound to validate phase.</echo>  
  78.             </tasks>  
  79.            </configuration>  
  80.          </execution>  
  81.          <execution>  
  82.           <id>ant-verify</id>  
  83.            <phase>verify</phase>  
  84.            <goals>  
  85.              <goal>run</goal>  
  86.            </goals>  
  87.            <configuration>  
  88.             <tasks>  
  89.             <echo>i am bound to verify phase.</echo>  
  90.             </tasks>  
  91.            </configuration>  
  92.          </execution>  
  93.        </executions>  
  94.      </plugin>  
  95.     </plugins>  
  96.   </build>  
  97. </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)插件詳細的列表在這個地址獲得:
 
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 plain  copy
 
  1. mvn help:describe-Dplugin=compiler-Dgoal=compile  
2.5)想讓  maven-help-plugin 輸出更詳細的信息,能夠加上 detail參數:
[cpp]  view plain  copy
 
  1. mvn help:describe-Dplugin=compiler-Ddetail  
 
【7】 從命令行調用插件
1)intro:運行mvn -h 來顯示 mvn 命令幫助:
[cpp]  view plain  copy
 
  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 plain  copy
 
  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 plain  copy
 
  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 plain  copy
 
  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,從而獲得完整的插件座標;
相關文章
相關標籤/搜索