1. 程式人生 > >Maven的生命週期和外掛

Maven的生命週期和外掛

(尊重勞動成果,轉載請註明出處:https://blog.csdn.net/qq_25827845/article/details/83795622冷血之心的部落格)

快速導航:

Maven基礎概念和安裝配置教程

Maven的倉庫和settings.xml配置檔案

Maven的座標與依賴

Maven的生命週期和外掛

Maven的聚合與繼承

 

目錄

Maven的生命週期

生命週期的定義

clean生命週期

default生命週期

site生命週期

Maven的外掛

外掛的目標

外掛的繫結

內建繫結

自定義繫結

外掛的配置

總結


       在上邊的學習中,我們介紹了Maven是一種強大的構建工具,能夠幫助我們自動下載構件,並且通過給每一個構件確定其座標,實現了構件在倉庫中的儲存。在這篇文章中,我們主要介紹Maven的生命週期和其外掛的使用。

Maven的生命週期

生命週期的定義

        Maven的生命週期就是為了對所有的構建過程進行抽象和統一。在大量專案的構建過程中,Maven總結出了一套高度完善的,易於擴充套件的生命週期,包括專案的清理,初始化,編譯,測試,打包,整合測試,驗證,部署和生成站點

等構建步驟。在我們日常的maven使用中,一條條簡單的命令,mvn clean, mvn package等都是在執行Maven的某個生命週期階段。

        Maven提供了三套獨立的生命週期:clean, default 和 site ,接下來我們分別介紹三套生命週期:

clean生命週期

        clean生命週期的目的是清理專案,刪除前一次構建在target資料夾下生成的各個Jar包等,它包含以下三個階段:

  • pre-clean:執行一些清理前需要完成的工作
  • clean:清理上一次構建生成的檔案
  • post-clean:執行一些清理後需要完成的工作

舉例:

我們在命令列中輸入: mvn clean 就是在呼叫clean生命週期的clean階段,實際執行了pre-cleanclean階段

default生命週期

       default生命週期定義了真正構建專案中需要執行的所有步驟,它包含的階段如下:

  • validate
  • initialize
  • generate-sources
  • process-sources
  • generate-resources
  • process-resources
  • compile:編譯專案的主原始碼
  • process-classes
  • generate-test-sources
  • process-test-sources
  • generate-test-resources
  • process-test-resources
  • test-compile
  • process-test-classed
  • test:使用單元測試框架執行測試,測試程式碼不會被打包或部署
  • prepare-package
  • package:接受編譯好的程式碼,打包成可釋出的格式,jar/war等
  • pre-integration-test
  • integration-test
  • post-integration-test
  • verify
  • install
  • deploy

此處我們注意下 install 生命週期階段,若我們在當前的maven專案中執行 mvn install ,那麼將執行validate到 install 的所有生命週期階段,結果就是將我們當前的專案打包並且安裝在了本地倉庫

但是install外掛還有一個目標 install-file 該外掛目標可以將我們普通Java專案到處的jar包安裝到本地倉庫。舉例如下:

mvn install:install-file -Dfile=testJar.jar -DgroupId=com.jar -DartifactId=mainywq -Dversion=1.0-SNAPSHOT -Dpackaging=jar  

  

舉例:

mvn test 就是在呼叫default生命週期的test階段,實際執行了validatetest階段之間的所有階段

mvn clean package 就是在呼叫clean生命週期的clean階段和default生命週期的package階段,實際執行了pre-cleanclean階段和default生命週期validatepackage階段之間的所有階段

mvn clean install 和 mvn clean deploy 所呼叫的生命週期階段請各位自行分析(對於聰明的你來說一定沒什麼難度^_^)

site生命週期

       site生命週期的目的是建立和釋出專案站點,Maven可以給予pom所包含的資訊,生成一個站點,方便團隊交流和釋出專案資訊,其生命週期階段包含:

  • pre- site
  • site:生成專案站點文件
  • post-site
  • site-deploy:將生成的專案站點發布到伺服器上

提問:

mvn clean deploy site-deploy命令所執行的生命週期的階段都有哪些呢?(對於聰明的你來說一定沒什麼難度^_^)

 

好的,介紹了三套Maven生命週期之後,我們會有一個疑問,“生命週期是抽象的,那麼實際的任務如package,install等階段的任務都是怎麼實現的呢?” 答案就是我們接下來要介紹的主角:外掛(plugin)

Maven的外掛

        生命週期的各個階段都是抽象的概念,真正幹活的是一個個的外掛,外掛是以獨立的構件形式存在,我們將maven的生命週期的各個階段與maven的外掛進行繫結,當我們執行mvn命令其實就是在指揮著一個個的外掛在幹活。

外掛的目標

        maven的外掛以獨立的構件形式存在,為了能夠複用程式碼,使得一個外掛可以完成多個任務,我們定義了外掛目標(Plugin Goal),每一個目標都可以完成不同的功能。

舉例:

        maven-dependency-plugin外掛具有多個功能,比如分析專案依賴,還能列出專案的依賴樹等。就是使用了analyze, tree和list等外掛目標區分的。

dependency : analyze     dependency : tree      denpendency : list

以上冒號左邊的表示外掛字首dependency,冒號右邊的表示外掛的目標Goal

外掛的繫結

        外掛的繫結分為內建繫結和自定義繫結,以下分別介紹。

內建繫結

        maven的生命週期的各個階段與maven外掛互相繫結,從而完成具體的構件任務。default生命週期的compile階段與maven-compiler-plugin外掛的目標compile進行繫結的示意圖如下所示:

為了能夠讓使用者更加方便的構建專案,maven將大多數主要的生命週期階段都綁定了很多外掛的目標。如下所示:

我們在一個專案中執行mvn clean package來觀察下會執行哪些生命週期的階段,哪些外掛目標會被執行。結果如下:

[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ passport-core ---
[INFO] Deleting D:\MiWork\passport\server\passport\passport-java-core\target
[INFO]
[INFO] --- build-helper-maven-plugin:1.9.1:add-source (add-source) @ passport-core ---
[INFO] Source directory: D:\MiWork\passport\server\passport\passport-java-core\src\thrift\gen-java added.
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ passport-core ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO] Copying 8 resources
[INFO] Copying 317 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.4:compile (default-compile) @ passport-core ---
[INFO] Compiling 317 source files to D:\MiWork\passport\server\passport\passport-java-core\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ passport-core ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.4:testCompile (default-testCompile) @ passport-core ---
[INFO] Compiling 35 source files to D:\MiWork\passport\server\passport\passport-java-core\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ passport-core ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ passport-core ---
[INFO] Building jar: D:\MiWork\passport\server\passport\passport-java-core\target\passport-core-0.0.26-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.304 s
[INFO] Finished at: 2018-11-07T21:30:33+08:00
[INFO] Final Memory: 25M/267M

上邊結果中我加粗的地方就是執行的外掛,比如maven-clean-plugin:2.5:clean 的含義是說執行了2.5版本的maven-clean-plugin外掛的 clean 的目標。

注意:

       前邊我們說了mvn clean package 就是在呼叫clean生命週期的clean階段和default生命週期的package階段,,實際執行了pre-cleanclean階段和default生命週期validatepackage階段之間的所有階段。但是有一些生命週期階段是沒有繫結外掛的,所以外掛被執行的個數當然會小於validate到package的階段的個數。

自定義繫結

        前面說了maven的內建繫結,當然maven為了滿足使用者多元化的構建過程,是允許我們自定義的選擇將某個外掛繫結到生命週期的某個階段的。

        接下來,我們將maven-source-plugin外掛的jar-no-fork任務繫結到verify生命週期階段,我們只需要在pom中增加如下的配置即可:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-source-plugin</artifactId> // 外掛
            <version>3.0.1</version>
            <executions>                             // 可以配置執行的任務
                <execution>
                    <id>attach-sources</id>     // 任務id
                    <phase>verify</phase>      // 生命週期verify階段
                    <goals>
                        <goal>jar-no-fork</goal> // 外掛目標
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

該段配置可以幫助我們構建專案的原始碼jar包,我們在命令列中執行mvn verify,會執行validate一直到verify階段的所有階段,結果如下:

[INFO] ------------------------------------------------------------------------
[INFO] Building passport-core 0.0.26-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- build-helper-maven-plugin:1.9.1:add-source (add-source) @ passport-core ---
[INFO] Source directory: D:\MiWork\passport\server\passport\passport-java-core\src\thrift\gen-java added.
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ passport-core ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO] Copying 8 resources
[INFO] Copying 317 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.4:compile (default-compile) @ passport-core ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ passport-core ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.4:testCompile (default-testCompile) @ passport-core ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ passport-core ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ passport-core ---
[INFO] Building jar: D:\MiWork\passport\server\passport\passport-java-core\target\passport-core-0.0.26-SNAPSHOT.jar
[INFO]
[INFO] --- maven-source-plugin:3.0.1:jar-no-fork (attach-sources) @ passport-core ---
[INFO] Building jar: D:\MiWork\passport\server\passport\passport-java-core\target\passport-core-0.0.26-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.648 s
[INFO] Finished at: 2018-11-07T21:58:44+08:00
[INFO] Final Memory: 25M/357M
         可以看到我們剛剛自定義的繫結外掛maven-source-plugin的目標jar-no-fork被執行,並且在專案的構建輸出的/target/下出現了-source包。

外掛的配置

       在專案的配置檔案pom中,我們可以對外掛進行一個全域性的配置,,我們可以在<build></build>中配置<plugins></plugins>來搞定,舉例如下:

    <plugins> 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <fork>true</fork>
                    <verbose>true</verbose>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <sourcepath>
                            ${project.basedir}/src/main/java
                        </sourcepath>
                    </compilerArguments>
                    <compilerArgument>-XDignore.symbol.file</compilerArgument>
                </configuration>
            </plugin>
    </plugins> 

該配置表示我們要編譯Java1.6版本的原始檔,生成與JVM1.6相容的位元組碼檔案。

       我們還可以在pom中進行外掛任務的配置,在上邊自定義繫結中我們其實已經進行了配置,再來看以下外掛任務配置程式碼:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-source-plugin</artifactId> // 外掛
            <version>3.0.1</version>
            <executions>                             // 配置執行的任務
                <execution>
                    <id>attach-sources</id>     // 任務id
                    <phase>verify</phase>      // 生命週期verify階段
                    <goals>
                        <goal>jar-no-fork</goal> // 外掛目標
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
 

我們來翻譯以下這段配置,那就是:

將外掛maven-source-plugin的目標jar-no-fork繫結到生命週期的verify階段,並且該任務的id為attach-sources.

從構建的輸出日誌中我們也可以看出來,如下所示:

總結

          這節內容比較重要,闡述了maven中的兩個重要概念生命週期和外掛。正是生命週期階段和外掛的互相繫結,我們才能真正完成專案的構建。常用的外掛如下:有需要深入瞭解的可以自行查閱相關資料。

  • maven-assembly-plugin :自定義打包方式,還是很有趣的,感興趣的可以研究哦

  • maven-enforcer-plugin :展示專案依賴衝突

  • maven-antrun-plugin :執行ant指令碼

  • maven-dependency-plugin :分析專案依賴

  • maven-clean-plugin :清理

  • maven-compiler-plugin :編譯

  • maven-source-plugin :原始碼

  • maven-war-plugin :打包

  • maven-jar-plugin:打包

  • maven-surefire-plugin :測試

  • maven-resources-plugin :資原始檔處理

 

如果對你有幫助,記得點贊哦~歡迎大家關注我的部落格,可以進群366533258一起交流學習哦~

本群給大家提供一個學習交流的平臺,內設菜鳥Java管理員一枚、精通演算法的金牌講師一枚、Android管理員一枚、藍芽BlueTooth管理員一枚、Web前端管理一枚以及C#管理一枚。歡迎大家進來交流技術。