maven生命週期和插件詳解

常用命令

mvn clean
mvn compile
mvn test
mvn package
mvn install
mvn install -Dmaven.test.skip=true
mvn deploy
mvn help:system

用戶屬性Properties的使用

用戶可以在properties中自定義一些用戶屬性,然後可以在其他地方使用${屬性名稱}這種方式進行引用。

<properties>
    <spring.group>org.springframework</spring.group>
    <spring.version>5.2.1.RELEASE</spring.version>
</properties>

 <dependency>
        <groupId>${spring.group}</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
</dependency>

生命週期

我們開發一個項目的時候,通常有這些環節:創建項目、編寫代碼、清理已編譯的代碼、編譯代碼、執行單元測試、打包、集成測試、驗證、部署、生成站點等,這些環節組成了項目的生命週期,這些過程也叫做項目的構建過程

maven中生命週期詳解

maven將項目的生命週期抽象成了3套生命週期,每套生命週期又包含多個階段,每套中具體包含哪些階段是maven已經約定好的,但是每個階段具體需要做什麼,是用戶可以自己指定的

maven中定義的3套生命週期:

  • clean生命週期
  • default生命週期
  • site生命週期

每套生命週期中有多個階段,每套中的多個階段是有先後順序的,並且後面的階段依賴於前面的階段,而用戶可以直接使用mvn命令來調用這些階段去完成項目生命週期中具體的操作

mvn 生命週期階段

maven中的3套生命週期相當於maven定義了3個類來解決項目生命週期中需要的各種操作,每個類中有多個方法,這些方法就是指具體的階段,方法名稱就是階段的名稱,每個類的方法是有順序的,當執行某個方法的時候,這個方法前面的方法也會執行。具體每個方法中需要執行什麼,這個是通過插件的方式讓用戶去配置的,所以非常靈活。
用戶執行mvn 階段名稱就相當於調用了具體的某個方法.

clean生命週期

在這裏插入圖片描述
用戶可以通過mvn pre-clean來調用clean生命週期中的pre-clean階段需要執行的操作。

調用mvn post-clean會執行上面3個階段所有的操作,上文中有說過,每個生命週期中的後面的階段會依賴於前面的階段,當執行某個階段的時候,會先執行其前面的階段.

default生命週期

這個是maven主要的生命週期,主要被用於構建應用,包含了23個階段
在這裏插入圖片描述

site生命週期

site生命週期的目的是建立和發佈項目站點,Maven能夠基於pom.xml所包含的信息,自動生成一個友好的站點,方便團隊交流和發佈項目信息。主要包含以下4個階段:
在這裏插入圖片描述

mvn命令和生命週期
mvn 階段1 [階段2] [階段n]

多個階段的名稱之間用空格隔開。

mvn clean deploy

這個命令也比較常用,會先按順序執行clean生命週期的[pre-clean,clean]這個閉區間內所有的階段,然後按序執行default生命週期[validate,deploy]這個閉區間內的所有階段(也就是default生命週期中的所有階段)。這個命令內部包含了清理上次構建的結果、編譯代碼、運行單元測試、打包、將打好的包安裝到本地倉庫、將打好的包發佈到私服倉庫。

項目pom.xml中常用配置

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 配置maven編譯的時候採用的編譯器版本 -->
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        <!-- 指定源代碼是什麼版本的,如果源碼和這個版本不符將報錯,maven中執行編譯的時候會用到這個配置,默認是1.5,這個相當於javac命令後面的-source參數 -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <!-- 該命令用於指定生成的class文件將保證和哪個版本的虛擬機進行兼容,maven中執行編譯的時候會用到這個配置,默認是1.5,這個相當於javac命令後面的-target參數 -->
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

maven 的每個階段具體做的事情是由maven插件來完成的。

有很多類似於maven-xxxx-plugin:版本:xxx這樣的內容,這個就是表示當前在運行這個插件來完成對應階段的操作,mvn 階段明明執行的是階段,但是實際輸出中確實插件在幹活,那麼階段是如何和插件關聯起來的呢?插件又是什麼呢?

Maven插件

maven插件主要是爲maven中生命週期中的階段服務的,maven中只是定義了3套生命週期,以及每套生命週期中有哪些階段,具體每個階段中執行什麼操作,完全是交給插件去幹的

插件可以通過mvn命令的方式調用直接運行,或者將插件和maven生命週期的階段進行綁定,然後通過mvn 階段的方式執行階段的時候,會自動執行和這些階段綁定的插件。

插件目標

每個插件中可能爲了代碼可以重用,一個插件可能包含了多個功能,比如編譯代碼的插件,可以編譯源代碼、也可以編譯測試代碼;插件中的每個功能就叫做插件的目標(Plugin Goal),每個插件中可能包含一個或者多個插件目標(Plugin Goal)。

目標參數

插件目標是用來執行任務的,那麼執行任務肯定是有參數配的,這些就是目標的參數,每個插件目標對應於java中的一個類,參數就對應於這個類中的屬性

列出插件所有目標:
mvn 插件goupId:插件artifactId[:插件version]:help
mvn 插件前綴:help

例如:

mvn org.apache.maven.plugins:maven-clean-plugin:help

輸出內容如下:

[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven-chat06 ---
[INFO] org.apache.maven.plugins:maven-clean-plugin:2.5

Maven Clean Plugin
  The Maven Clean Plugin is a plugin that removes files generated at build-time
  in a project's directory.

This plugin has 2 goals:

clean:clean
  Goal which cleans the build.
  This attempts to clean a project's working directory of the files that were
  generated at build-time. By default, it discovers and deletes the directories
  configured in project.build.directory, project.build.outputDirectory,
  project.build.testOutputDirectory, and project.reporting.outputDirectory.

  Files outside the default may also be included in the deletion by configuring
  the filesets tag.

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

上面列出了maven-clean-plugin這個插件所有的目標,有2個,分別是clean:clean、clean:help,分號後面的部分是目標名稱,分號前面的部分是插件的前綴,每個目標的後面包含對這個目標的詳細解釋說明

查看插件目標參數列表
mvn 插件goupId:插件artifactId[:插件version]:help -Dgoal=目標名稱 -Ddetail
mvn 插件前綴:help -Dgoal=目標名稱 -Ddetail

上面命令中的-Ddetail用戶輸出目標詳細的參數列表信息,如果沒有這個,目標的參數列表不會輸出出來

例如:

mvn org.apache.maven.plugins:maven-clean-plugin:help -Dgoal=help -Ddetail

輸出內容:

[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven-chat06 ---
[INFO] org.apache.maven.plugins:maven-clean-plugin:2.5

Maven Clean Plugin
  The Maven Clean Plugin is a plugin that removes files generated at build-time
  in a project's directory.

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

  Available parameters:

    detail (Default: false)
      If true, display all settable properties for each goal.
      Expression: ${detail}

    goal
      The name of the goal for which to show help. If unspecified, all goals
      will be displayed.
      Expression: ${goal}

    indentSize (Default: 2)
      The number of spaces per indentation level, should be positive.
      Expression: ${indentSize}

    lineLength (Default: 80)
      The maximum length of a display line, should be positive.
      Expression: ${lineLength}

上面列出了clean插件的help目標的詳細參數信息

注意上面參數詳細參數說明中有Expression: ${xxx}這樣的部分,這種表示給這個運行的目標傳參,可以通過mvn -Dxxx這種方式傳參,xxx爲${xxx}中的xxx部分,這個xxx有時候和目標參數的名稱不一致,所以這點需要注意,運行帶參數的目標

例如:執行 目標任務 -Dgoal=help 傳入參數 -Ddetail=false

mvn org.apache.maven.plugins:maven-clean-plugin:help -Dgoal=help -Ddetail=false

輸出內容:

[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven-chat06 ---
[INFO] org.apache.maven.plugins:maven-clean-plugin:2.5

Maven Clean Plugin
  The Maven Clean Plugin is a plugin that removes files generated at build-time
  in a project's directory.

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

上面傳了一個detail=false,上面未輸出目標的詳細參數信息

命令行運行插件

mvn 插件goupId:插件artifactId[:插件version]:插件目標 [-D目標參數1] [-D目標參數2] [-D目標參數n]
mvn 插件前綴:插件目標  [-D目標參數1] [-D目標參數2] [-D目標參數n]
案例
插件有哪些目標
mvn org.apache.maven.plugins:maven-surefire-plugin:help

輸出內容:

[INFO] --- maven-surefire-plugin:2.12.4:help (default-cli) @ maven-chat06 ---
[INFO] Maven Surefire Plugin 2.12.4
  Surefire is a test framework project.

This plugin has 2 goals:

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

surefire:test
  Run tests using Surefire.

maven-surefire-plugin插件有2個目標help和test,描述中可以看出test目標是用來運行測試用例的。

test目標對應的參數列表
mvn org.apache.maven.plugins:maven-surefire-plugin:help -Dgoal=test -Ddetail=true

輸出內容:

[INFO] --- maven-surefire-plugin:2.12.4:help (default-cli) @ maven-chat06 ---
[INFO] Maven Surefire Plugin 2.12.4
  Surefire is a test framework project.

surefire:test
  Run tests using Surefire.

  Available parameters:

    skip (Default: false)
      Set this to 'true' to bypass unit tests entirely. Its use is NOT
      RECOMMENDED, especially if you enable it using the 'maven.test.skip'
      property, because maven.test.skip disables both running the tests and
      compiling the tests. Consider using the skipTests parameter instead.

看一下skip這個參數說明,這個參數默認是false,如果設置爲true的時候,項目將跳過測試代碼的編譯和測試用例的執行,可以maven.test.skip這個屬性來進行命令行傳參,將其傳遞給test目標的skip參數,這個通過-D傳遞的參數名稱就和目標參數名稱不一樣了,所以需要注意-D後面並不一定是參數名稱。

先看一下不加參數的效果

mvn org.apache.maven.plugins:maven-surefire-plugin:test

輸出內容:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.javacode2018:maven-chat06 >--------------------
[INFO] Building maven-chat06 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven-chat06 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

maven.skip.test=true 時,將其傳遞給test目標的skip參數,來設置 skip 的值,效果如下:

mvn org.apache.maven.plugins:maven-surefire-plugin:test -Dmaven.test.skip=true

輸出內容:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.javacode2018:maven-chat06 >--------------------
[INFO] Building maven-chat06 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven-chat06 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

對比一下上面2個輸出,下面的多了一行如下

[INFO] Tests are skipped.

說明跳過了測試的執行。

插件傳參的2種方式

一種通過-D後面跟用戶屬性的方式給用戶傳參,還有一種方式,在pom.xml中properties的用戶自定義屬性中進行配置,properties中加入

<maven.test.skip>true</maven.test.skip>

執行命令

mvn org.apache.maven.plugins:maven-surefire-plugin:test

輸出內容

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.javacode2018:maven-chat06 >--------------------
[INFO] Building maven-chat06 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven-chat06 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

輸出中也有Tests are skipped.,說明也跳過了測試,和-Dmaven.test.skip=true效果一樣。

插件目標是如何和生命週期關聯起來的呢?

獲取插件目標詳細描述信息的另外一種方式

mvn help:describe -Dplugin=插件goupId:插件artifactId[:插件version] -Dgoal=目標名稱 -Ddetail
mvn help:describe -Dplugin=插件前綴 -Dgoal=目標名稱 -Ddetail

上面這個命令調用的是help插件的describe這個目標,這個目標可以列出其他指定插件目標的詳細信息

執行命令

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-surefire-plugin -Dgoal=test -Ddetail

輸出內容

[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven-chat06 ---
[INFO] Mojo: 'surefire:test'
surefire:test
  Description: Run tests using Surefire.
  Implementation: org.apache.maven.plugin.surefire.SurefirePlugin
  Language: java
  Bound to phase: test

  Available parameters:

    additionalClasspathElements
      Additional elements to be appended to the classpath.

    argLine
      User property: argLine
      Arbitrary JVM options to set on the command line.

    skip (Default: false)
      User property: maven.test.skip
      Set this to 'true' to bypass unit tests entirely. Its use is NOT
      RECOMMENDED, especially if you enable it using the 'maven.test.skip'
      property, because maven.test.skip disables both running the tests and
      compiling the tests. Consider using the skipTests parameter instead.

和上面獲取插件目標參數詳情列表對比一下,上面這個更詳細一些,參數說明中多了一行User property: 屬性名稱,這個屬性名稱可以通過兩種方式傳遞

  • mvn命令-D屬性名稱的方式傳遞
  • pom.xml中properties中定義的方式指定。

現在可以知道我們一直用的-Dmaven.test.skip爲什麼可以跳過測試代碼的編譯和單元測試的執行了吧。(通過-D 傳入參數,來控制目標的參數的屬性值)

插件前綴

可以通過下面命令查看到插件的前綴:

mvn help:describe -Dplugin=插件goupId:插件artifactId[:插件version]

輸出內容如下:

Name: Maven Surefire Plugin
Description: Surefire is a test framework project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-surefire-plugin
Version: 2.18.1
Goal Prefix: surefire

輸出中的Goal Prefix:部分對應的就是插件的前綴,上面這個插件的前綴是surefire

使用前綴來運行插件
mvn surefire:test

上面通過別名來運行插件maven-surefire-plugin的test目標

上面用了很多mvn help:這個命令,這個調用的是maven-help-plugin插件的功能,help是插件的前綴,它的座標是:

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-help-plugin</artifactId>
    <version>3.2.0</version>
</dependency>

插件和生命週期階段綁定

maven只是定義了生命週期中的階段,而沒有定義每個階段中具體的實現,這些實現是由插件的目標來完成的,所以需要將階段和插件目標進行綁定,來讓插件目標幫助生命週期的階段做具體的工作,生命週期中的每個階段支持綁定多個插件的多個目標。

當我們將生命週期中的階段和插件的目標進行綁定的時候,執行mvn 階段就可以執行和這些階段綁定的插件目標

maven內置插件以及綁定

maven內部已經提供了很多默認的插件,而將一些階段默認和這些插件階段綁定好了,所以我們不用做任何配置就可以執行清理代碼、編譯代碼、測試、打包、安裝到本地倉庫、上傳到遠程倉庫等階段的操作,是因爲maven已經默認給這些階段綁定好了插件目標,所以不需要我們再去配置,就直接可以運行,這些都是maven內置綁定幫我們做的事情

maven內置綁定

clean生命週期階段與插件綁定關係
在這裏插入圖片描述

clean週期中只有clean階段默認綁定了maven-clean-plugin插件的clean目標maven-clean-plugin插件的clean目標作用就是刪除項目的輸出目錄。

default生命週期階段與插件綁定關係

在這裏插入圖片描述

site生命週期階段與插件綁定關係
在這裏插入圖片描述

mvn clean

[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ maven-application ---

這個表示調用的插件是:maven-clean-plugin,版本是:2.5,插件的目標是:clean

mvn test
該命令調用default生命週期的test階段,實際上會從default生命週期的第一個階段(validate)開始執行一直到test階段結束
mvn test會調用下面一些插件的目標:

maven-resources-plugin:resources
maven-compiler-plugin:compile
maven-resources-plugin:testResources
maven-compiler-plugin:testCompile
maven-surefile-plugin:test
自定義綁定

除了默認綁定的一些操作,我們自己也可以將一些階段綁定到指定的插件目標上來完成一些操作,這種自定義綁定讓maven項目在構件的過程中可以執行更多更豐富的操作。

常見的一個案例是:創建項目的源碼jar包,將其安裝到本地倉庫中,內置插件綁定關係中沒有涉及到這一步的任務,所以需要用戶自己配置。

插件maven-source-plugin的jar-no-fork可以幫助我們完成該任務,我們將這個目標綁定在default生命週期的verify階段上面,這個階段沒有任何默認綁定,verify是在測試完成之後並將構件安裝到本地倉庫之前執行的階段,在這個階段我們生成源碼,配置如下:

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <!-- 使用插件需要執行的任務 -->
                    <execution>
                        <!-- 任務id -->
                        <id>attach-source</id>
                        <!-- 任務中插件的目標,可以指定多個 -->
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                        <!-- 綁定的階段 -->
                        <phase>verify</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

執行命令

mvn install

輸出內容如下:

[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-rpc ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-rpc ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\gitproject\maven-demo\maven-rpc\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-rpc ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\gitproject\maven-demo\maven-rpc\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-rpc ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-rpc ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-rpc ---
[INFO] Building jar: E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-source-plugin:3.2.1:jar-no-fork (attach-source) @ maven-rpc ---
[INFO] Building jar: E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT-sources.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-rpc ---
[INFO] Installing E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT.jar to F:\JDMavenRepository\com\myke\maven-rpc\1.0-SNAPSHOT\maven-rpc-1.0-SNAPSHOT.jar
[INFO] Installing E:\gitproject\maven-demo\maven-rpc\pom.xml to F:\JDMavenRepository\com\myke\maven-rpc\1.0-SNAPSHOT\maven-rpc-1.0-SNAPSHOT.pom
[INFO] Installing E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT-sources.jar to F:\JDMavenRepository\com\myke\maven-rpc\1.0-SNAPSHOT\maven-rpc-1.0-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

可以看出調用了我們配置的插件生成源碼jar,上面的括號中的attach-source就是pom.xml中配置的任務id。

[INFO] Installing E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT-sources.jar to F:\JDMavenRepository\com\myke\maven-rpc\1.0-SNAPSHOT\maven-rpc-1.0-SNAPSHOT-sources.jar

可以看到將源碼安裝到本地倉庫了。

怎麼查看插件的默認綁定呢?
mvn help:describe -Dplugin=插件goupId:插件artifactId[:插件version] -Dgoal=目標名稱 -Ddetail
mvn help:describe -Dplugin=插件前綴 -Dgoal=目標名稱 -Ddetail

我們看一下插件source的jar-no-fork目標默認的綁定:

$ mvn help:describe -Dplugin=source -Dgoal=jar-no-fork -Ddetail
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-rpc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven-rpc ---
[INFO] Mojo: 'source:jar-no-fork'
source:jar-no-fork
  Description: This goal bundles all the sources into a jar archive. This
    goal functions the same as the jar goal but does not fork the build and is
    suitable for attaching to the build lifecycle.
  Implementation: org.apache.maven.plugins.source.SourceJarNoForkMojo
  Language: java
  Bound to phase: package

  Available parameters:

    archive
      The archive configuration to use. See Maven Archiver Reference.
      Note: Since 3.0.0 the resulting archives contain a maven descriptor. If
      you need to suppress the generation of the maven descriptor you can
      simply achieve this by using the archiver configuration..

    attach (Default: true)
      User property: maven.source.attach
      Specifies whether or not to attach the artifact to the project

    classifier (Default: sources)
      User property: maven.source.classifier
      (no description available)

    defaultManifestFile (Default:
    ${project.build.outputDirectory}/META-INF/MANIFEST.MF)
      Required: true
      Path to the default MANIFEST file to use. It will be used if
      useDefaultManifestFile is set to true.

    excludeResources (Default: false)
      User property: maven.source.excludeResources
      Specifies whether or not to exclude resources from the sources-jar. This
      can be convenient if your project includes large resources, such as
      images, and you don't want to include them in the sources-jar.

    excludes
      List of files to exclude. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    finalName (Default: ${project.build.finalName})
      The filename to be used for the generated archive file. For the
      source:jar goal, '-sources' is appended to this filename. For the
      source:test-jar goal, '-test-sources' is appended.

    forceCreation (Default: false)
      User property: maven.source.forceCreation
      Whether creating the archive should be forced. If set to true, the jar
      will always be created. If set to false, the jar will only be created
      when the sources are newer than the jar.

    includePom (Default: false)
      User property: maven.source.includePom
      Specifies whether or not to include the POM file in the sources-jar.

    includes
      List of files to include. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    outputDirectory (Default: ${project.build.directory})
      The directory where the generated archive file will be put.

    outputTimestamp (Default: ${project.build.outputTimestamp})
      Timestamp for reproducible output archive entries, either formatted as
      ISO 8601 yyyy-MM-dd'T'HH:mm:ssXXX or as an int representing seconds since
      the epoch (like SOURCE_DATE_EPOCH).

    skipSource (Default: false)
      User property: maven.source.skip
      A flag used to disable the source procedure. This is primarily intended
      for usage from the command line to occasionally adjust the build.

    useDefaultExcludes (Default: true)
      User property: maven.source.useDefaultExcludes
      Exclude commonly excluded files such as SCM configuration. These are
      defined in the plexus FileUtils.getDefaultExcludes()

    useDefaultManifestFile (Default: false)
      User property: maven.source.useDefaultManifestFile
      Set this to true to enable the use of the defaultManifestFile.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

上面輸出中有個Bound to phase: package,表示默認綁定在了package階段上。

我們可以在default生命週期的第一個階段validate綁定清理代碼的插件,那我們來通過自定義綁定來實現一下,project->build->plugins元素中加入下面配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <!-- 使用插件需要執行的任務 -->
        <execution>
            <!-- 任務中插件的目標,可以指定多個 -->
            <id>clean-target</id>
            <goals>
                <goal>clean</goal>
            </goals>
            <!-- 綁定的階段 -->
            <phase>validate</phase>
        </execution>
    </executions>
</plugin>

執行命令

$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-rpc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (clean-target) @ maven-rpc ---
[INFO] Deleting E:\gitproject\maven-demo\maven-rpc\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-rpc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-rpc ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\gitproject\maven-demo\maven-rpc\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

輸出中有:

[INFO] --- maven-clean-plugin:2.5:clean (clean-target) @ maven-rpc ---
[INFO] Deleting E:\gitproject\maven-demo\maven-rpc\target

這個表示運行了代碼清理的功能,進行了代碼清理.相當於 mvn clean compile

POM.xml插件配置詳解
插件目標共享參數配置

build->plugins->plugin中配置:

<!-- 插件參數配置,對插件中所有的目標起效 -->
<configuration>
    <目標參數名>參數值</目標參數名>
</configuration>

configuration節點下配置目標參數的值,節點名稱目標的參數名稱,上面這種配置對當前插件的所有目標起效,也就是說這個插件中所有的目標共享此參數配置。

案例
          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <!-- 插件參數配置,對插件中所有的目標起效 -->
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

執行命令

$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-rpc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (clean-target) @ maven-rpc ---
[INFO] Deleting E:\gitproject\maven-demo\maven-rpc\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-rpc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-rpc ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\gitproject\maven-demo\maven-rpc\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-rpc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\gitproject\maven-demo\maven-rpc\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-rpc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-rpc ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

可以看到Test are skipped,說明跳過了測試,到此爲止,跳過測試已經講了3種了:

1. mvn -Dmaven.test.skip=tue
2. properties中配置<maven.test.skip>true</maven.test.skip>
3. build中配置插件參數的方式

上面這個配置參數方式對當前插件的所有目標有效,如果想對指定的目標進行配置呢,用下面的方式。

對指定的目標參數配置

project->build->plugins->plugin->executions->execution元素中進行配置,如下:

<!-- 這個地方配置只對當前任務有效 -->
<configuration>
    <目標參數名>參數值</目標參數名>
</configuration>

上面這種配置常用於自定義插件綁定,只對當前任務有效。

案例
<!--跳過測試用例:對指定的目標參數配置-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                            <goal>help</goal>
                        </goals>
                        <phase>pre-clean</phase>
                        <!-- 這個地方配置只對當前任務有效 -->
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

上面自定義了一個綁定,在clean週期的pre-clean階段綁定了插件maven-surefire-plugin的兩個目標test和helpexecution元素沒有指定id,所以默認id是default

執行命令

$ mvn pre-clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-rpc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default) @ maven-rpc ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:help (default) @ maven-rpc ---
[INFO] Maven Surefire Plugin 2.12.4
  Surefire is a test framework project.

This plugin has 2 goals:

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

surefire:test
  Run tests using Surefire.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

可以看到上面輸出中運行了插件的兩個目標

獲取maven插件信息

可以通過下面命令獲取插件詳細介紹信息

mvn help:describe -Dplugin=插件goupId:插件artifactId[:插件version] -Dgoal=目標名稱 -Ddetail
mvn help:describe -Dplugin=插件前綴 -Dgoal=目標名稱 -Ddetail

更多maven插件的幫助文檔可以參考maven的官方網站,上面有詳細的介紹,地址:

http://maven.apache.org/plugins/
插件解析機制

爲了方便用戶使用和配置插件,maven不需要用戶提供完整的插件座標信息,就可以解析到正確的插件,不過我建議使用插件配置的時候最好還是配置完整的座標信息.

插件倉庫

插件的是在pluginRepositories->pluginRepository元素中配置的,如下:

<pluginRepositories>
    <pluginRepository>
        <id>myplugin-repository</id>
        <url>http://repo1.maven.org/maven2/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

插件的默認groupId
pom.xml中配置插件的時候,如果是官方的插件,可以省略groupId

編譯代碼插件

 <!--編譯代碼-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

上面這個插件用於編譯代碼的,編譯代碼的時候需要指定編譯器的版本,源碼的版本,目標代碼的版本,都是用的是1.8。

插件前綴的解析

使用mvn命令調用插件的時候,可以使用插件的前綴來代替繁瑣的插件座標的方式,那麼maven是如何根據插件的前綴找到對應的插件的呢?

插件前綴與插件groupId:artifactId是一一對應的關係,這個關係的配置存儲在倉庫的元數據中。目錄:repository/org/apache/maven/plugins/查看 maven-metadata xml文件,
在這裏插入圖片描述

查看項目最終pom.xml文件

我們的pom.xml默認會繼承maven頂級的一個父類pom.xml,頂級的pom.xml中指定了很多默認的配置,如生命週期中的階段和很多插件的綁定,這些如果我們想看到,到哪裏看呢?

mvn命令在項目中執行的時候,我們的pom.xml和父類的pom.xml最終會進行合併,當我們的pom.xml寫的比較複雜的時候,最終合併之後是什麼效果呢,我們可以通過下面這個命令查看:

mvn help:effective-pom

輸出部分內容如下:

<build>
    <sourceDirectory>E:\gitproject\maven-demo\maven-rpc\src\main\java</sourceDirectory>
    <scriptSourceDirectory>E:\gitproject\maven-demo\maven-rpc\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>E:\gitproject\maven-demo\maven-rpc\src\test\java</testSourceDirectory>
    <outputDirectory>E:\gitproject\maven-demo\maven-rpc\target\classes</outputDirectory>
    <testOutputDirectory>E:\gitproject\maven-demo\maven-rpc\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>E:\gitproject\maven-demo\maven-rpc\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>E:\gitproject\maven-demo\maven-rpc\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>E:\gitproject\maven-demo\maven-rpc\target</directory>
    <finalName>maven-rpc-1.0-SNAPSHOT</finalName>

參考

maven生命週期和插件詳解
maven插件的幫助文檔

發佈了19 篇原創文章 · 獲贊 2 · 訪問量 1420
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章