Maven的生命周期和插件(五)

jopen 9年前發布 | 11K 次閱讀 Maven 項目構建

前面我們已經講過坐標、依賴以及倉庫,Maven的另外兩個核心概念是生命周期和插件。生命周期和插件二者協同工作,密不可分。

1. Maven生命周期基本概念

1) Maven的生命周期就是為了對所有的構建過程進行抽象和統一。Maven總結了一套高度完善的、易擴展的生命周期,包括了項目的清理、初始化、編譯、測試、打包、集成測試、驗證、部署和站點生成等幾乎所有的構建步驟。

2) Maven的生命周期是抽象的,生命周期本身不做任何實際的工作,在Maven的設計中,實際的任務都交給插件來完成。其實這種思想和設計模式中的模版方法(Template Method)很相似。

2. 生命周期詳解

1)三套生命周期

Maven有三套相互獨立的生命周期,分別為clean、default和site。clean生命周期的目的是清理項目,default生命周期的目的是構建項目,而site生命周期的目的是建立項目站點。

2)clean生命周期

clean生命周期的目的是清理項目,它包括如下三個階段:

a)pre-clean:執行一些清理前需要完成的工作。

b)clean:清理上一次構建的文件。

c)post-clean:執行一些清理后需要完成的工作。

3) default生命周期

default生命周期定義了真正構建時所需要執行的所有步驟,它是所有生命周期中最核心的部分。

a)validate

b)initialize

c)generate-sources

d)process-sources

e)generate-resources

f) process-resources

g)compile:編譯項目的主源碼,即編譯src/main/java目錄下的Java文件至項目輸出的主classpath中。

h)process-classes

i)generate-test-sources

j)process-test-sources

k)generate-test-resources

l)process-test-resources

m)test-compile:編譯項目的測試代碼,即編譯src/test/java目錄下的Java文件至項目輸出的測試classpath目錄中。

n)process-test-classes

o)test:使用單元測試框架運行測試。

p)prepare-package

q)package:接受編譯好的代碼,打包成可發布的格式。如:.jar

r)pre-integration-test

s)integration-test

t)post-integration-test

u)verify

v)install:將包安裝到Maven本地倉庫,供本地其他Maven項目使用。

w)deploy:將最終的包復制到遠程倉庫,供其他開發人員和Maven項目使用。

4) site生命周期

site生命周期的目的是建立和發布項目站點。

a)pre-site:執行一些在生成項目站點之前需要完成的工作。

b)site:生成項目站點文檔。

c)post-site:執行一些在生成項目站點之后需要完成的工作。

d)site-deploy:將生成的項目站點發布到服務器上。

5)Window下的命令行執行Maven任務

主要是調用Maven的生命周期的階段,以下幾個常用的命令:

mvn clean、mvn test、mvn clean install、mvn clean deploy site-deploy。

3. 插件詳解

1)插件目標

我們已經知道,Maven的核心僅僅定義了抽象的生命周期,具體的任務是交給插件完成的,插件以獨立的構件形式存在的。對于插件本身,為了能夠復用代碼,它往往能夠完成多個任務。每個任務就是一個插件目標。

2) 插件綁定

Maven的生命周期與插件是相互綁定的,用以完成實際的構建任務。具體而言,是生命周期的階段和插件的目標相互綁定,以完成一項具體的任務。如:項目編譯這一任務,它對應了default生命周期中的compile這一階段,而maven-compiler-plugin這一插件的compile目標能夠完成該任務。

3)內置綁定

Maven為一些主要的生命周期階段綁定了很多插件目標。比如:clean生命周期中的clean階段,與maven-clean-plugin:clean綁定。

下面我對hello-world項目執行mvn clean install命令,(本人直接使用eclipse執行,命令為clean install),看下面執行了哪些插件目標:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting D:\code\maven\hello-world\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\code\maven\hello-world\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\code\maven\hello-world\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: D:\code\maven\hello-world\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.alvinliang.maven.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.067 sec

Results :

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

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
[INFO] Building jar: D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world ---
[INFO] Installing D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\code\maven\hello-world\pom.xml to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.715s
[INFO] Finished at: Mon Jan 20 22:28:09 CST 2014
[INFO] Final Memory: 16M/167M
[INFO] ------------------------------------------------------------------------

4)自定義綁定

除了內置綁定外,用戶可以自己選擇將某個插件目標綁定到生命周期的某個階段上。例如:創建項目的源碼,沒有對應的內置綁定,這時候就需要用戶自己自定義綁定。

4. 插件的配置

1)命令行插件配置

用戶可以在Maven命令中使用-D參數,并伴隨一個參數鍵=參數值的形式,來配置插件的參數。

如:mvn install -Dmaven.test.skip = true

2)Eclipse中執行 install -Dmaven.test.skip = true,結果如下:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello-world ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world ---
[INFO] Installing D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\code\maven\hello-world\pom.xml to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.481s
[INFO] Finished at: Mon Jan 20 22:35:59 CST 2014
[INFO] Final Memory: 13M/100M
[INFO] ------------------------------------------------------------------------

5. 使用maven-help-plugin描述插件

在cmd中執行mvn help:describe -Dplugin=compiler,結果如下:

[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ standalone-pom ---
[INFO] org.apache.maven.plugins:maven-compiler-plugin:3.1

Name: Maven Compiler Plugin
Description: The Compiler Plugin is used to compile the sources of your
  project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-compiler-plugin
Version: 3.1
Goal Prefix: compiler

This plugin has 3 goals:

compiler:compile
  Description: Compiles application sources

compiler:help
  Description: Display help information on maven-compiler-plugin.
    Call mvn compiler:help -Ddetail=true -Dgoal=<goal-name> to display
    parameter details.

compiler:testCompile
  Description: Compiles application test sources.

For more information, run 'mvn help:describe [...] -Ddetail'


來自: http://my.oschina.net/liangbo/blog/194581

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!