maven運行過程解析

敘述

項目結構

我們就以一個簡單的例子來分析整個maven運行的過程。構建所使用的項目結構如下:

在這裏插入圖片描述
主要是一個echo項目,其包含了兩個module,分別是api和biz。echo項目的pom.xml的內容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.maven</groupId>
    <artifactId>echo</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>api</module>
        <module>biz</module>
    </modules>
</project>

打包方式解讀

    這裏有個比較費解的地方就是pom。若元素的內容是jar,那麼我們很好理解,也就是說這個項目最終會被打包成一個jar包。那元素爲pom又是什麼意思呢?從字面上的意思來看,這個項目將打包成一個pom。我們不妨去maven倉庫裏去瞧瞧(前提是已經在項目下運行了mvn install命令)。我們發現在<maven倉庫路徑>/org/maven/echo/1.0.0目錄下有一個echo-1.0.0.pom文件,細心的讀者可能已經發現這個文件其實和echo項目中的pom.xml是同一個文件。這樣做的目的是什麼呢?還記得第一篇中說過的PO對象吧,我們說過PO對象也是有繼承關係的,比如說這裏echo項目對應的PO對象就是api項目對應的PO對象的父對象(api項目是echo項目的一個module,在api項目的pom.xml中元素的內容所對應的就是echo項目),而echo項目PO對象的父對象又是哪個呢?答案是Super POM對應的PO對象。這就是maven中project inheritance的概念。當實際執行maven命令的時候,會根據project inheritance關係對項目的pom.xml進行轉化,得到真正執行時所用到的pom.xml,即所謂的effective pom。而pom的作用就在這裏。

    因此可以得到一個結論:所有元素爲pom的項目其實並不會輸出一個可供外部使用,類似於jar包的東西。這類項目的作用有兩個:

管理子項目
例如這裏的api和biz就是echo項目的兩個module。若沒有echo這個父項目,我們需要到api和biz兩個項目下分別執行mvn install命令才能完成整個構建過程,而有了echo這個父項目之後,我們只需在echo項目中執行mvn install即可,maven會解析pom.xml,發現該項目有api和biz兩個module,它會分別到這兩個項目下去執行mvn install命令。當module數量比較多的時候,能大大提高構建的效率。

管理繼承屬性
比如api和biz都需要某個依賴,那麼在echo項目的pom.xml中聲明即可,因爲根據PO對象的繼承關係,api和biz項目會繼承echo項目的依賴,這樣就可以減少一些重複的輸入。


effective pom命令瞭解

    effective pom包含了當前項目的PO對象,直到Super POM對應的PO對象中的信息。要看一個項目的effective pom,只需在項目中執行

mvn help:effective-pom

命令即可查看。maven命令的語法爲

mvn [options] [goal(s)] [phase(s)]

    maven允許你執行一個或者多個goals/phases。很明顯這面的命令help:effective-pom並不是一個phase,那麼也就是說它是一個goal。對這個goal只不過是採用了縮寫的形式,其全稱是這樣的:

org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom

    以分號爲分隔符,包含了groupId,artifactId,version,goal四部分。若groupId爲org.apache.maven.plugins則可以使用上述的簡寫形式。也就是說

mvn help:effective-pom
mvn org.apache.maven.plugins:maven-help-plugin:2.2:effective-pom

是等價的,都是執行了maven-help-plugin這個plugin中的effective-pom這個goal。

    好了,繼續回到effective pom。我們說過maven在真正構建的時候用的就是effective pom,那麼說明effective pom中包含了構建的所有信息,我們以biz項目中的effective pom爲例來看下effective pom長什麼樣子。在biz項目中執行mvn help:effective-pom命令,你會得到如下輸出:

 <?xml version="1.0"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
      <groupId>org.maven</groupId>
      <artifactId>echo</artifactId>
      <version>1.0.0</version>
    </parent>
    <groupId>org.maven</groupId>
    <artifactId>echo-biz</artifactId>
    <version>1.0.0</version>
    <dependencies>
      <dependency>
        <groupId>org.maven</groupId>
        <artifactId>echo-api</artifactId>
        <version>1.0.0</version>
        <scope>compile</scope>
      </dependency>
    </dependencies>
    <build>
      <sourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/java</sourceDirectory>
      <scriptSourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/scripts</scriptSourceDirectory>
      <testSourceDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/java</testSourceDirectory>
      <outputDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/classes</outputDirectory>
      <testOutputDirectory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/test-classes</testOutputDirectory>
      <resources>
        <resource>
          <directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/resources</directory>
        </resource>
      </resources>
      <testResources>
        <testResource>
          <directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/resources</directory>
        </testResource>
      </testResources>
      <directory>/Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target</directory>
      <finalName>echo-biz-1.0.0</finalName>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.4.1</version>
          <executions>
            <execution>
              <id>default-clean</id>
              <phase>clean</phase>
              <goals>
                <goal>clean</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.3.1</version>
          <executions>
            <execution>
              <id>default-install</id>
              <phase>install</phase>
              <goals>
                <goal>install</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.5</version>
          <executions>
            <execution>
              <id>default-resources</id>
              <phase>process-resources</phase>
              <goals>
                <goal>resources</goal>
              </goals>
            </execution>
            <execution>
              <id>default-testResources</id>
              <phase>process-test-resources</phase>
              <goals>
                <goal>testResources</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.10</version>
          <executions>
            <execution>
              <id>default-test</id>
              <phase>test</phase>
              <goals>
                <goal>test</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <executions>
            <execution>
              <id>default-testCompile</id>
              <phase>test-compile</phase>
              <goals>
                <goal>testCompile</goal>
              </goals>
            </execution>
            <execution>
              <id>default-compile</id>
              <phase>compile</phase>
              <goals>
                <goal>compile</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.3.2</version>
          <executions>
            <execution>
              <id>default-jar</id>
              <phase>package</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.7</version>
          <executions>
            <execution>
              <id>default-deploy</id>
              <phase>deploy</phase>
              <goals>
                <goal>deploy</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    </project>

對比biz項目的pom.xml,我們發現effective pom中增加了Super POM中繼承過來的一些配置,比如說定義了biz項目的源碼路徑,以及Lifecycle中各個phase綁定的goal:

[phase]     [goal]
compile     maven-compiler-plugin:2.3.2:compile
package     maven-jar-plugin:2.3.2:jar
install     maven-install-plugin:2.3.1:install
... ...

有了effective pom的概念之後,再來看maven構建的輸出日誌,是不是有點豁然開朗的感覺?

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order: 
[INFO] 
[INFO] echo
[INFO] echo-api
[INFO] echo-biz
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building echo 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo ---
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo/1.0.0/echo-1.0.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building echo-api 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ echo-api ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/api/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ echo-api ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ echo-api ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/api/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ echo-api ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ echo-api ---
[INFO] No tests to run.
[INFO] Surefire report directory: /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

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

[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ echo-api ---
[INFO] Building jar: /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/echo-api-1.0.0.jar
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo-api ---
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/api/target/echo-api-1.0.0.jar to /Users/allstarw/.m2/repository/org/maven/echo-api/1.0.0/echo-api-1.0.0.jar
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/api/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo-api/1.0.0/echo-api-1.0.0.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building echo-biz 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ echo-biz ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ echo-biz ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ echo-biz ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ echo-biz ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ echo-biz ---
[INFO] No tests to run.
[INFO] Surefire report directory: /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

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

[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ echo-biz ---
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ echo-biz ---
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/target/echo-biz-1.0.0.jar to /Users/allstarw/.m2/repository/org/maven/echo-biz/1.0.0/echo-biz-1.0.0.jar
[INFO] Installing /Users/allstarw/workspace/own-proj/misc/maven/echo/biz/pom.xml to /Users/allstarw/.m2/repository/org/maven/echo-biz/1.0.0/echo-biz-1.0.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] echo .............................................. SUCCESS [0.461s]
[INFO] echo-api .......................................... SUCCESS [2.581s]
[INFO] echo-biz .......................................... SUCCESS [0.382s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.593s
[INFO] Finished at: Wed Jul 06 00:22:50 CST 2016
[INFO] Final Memory: 10M/156M
[INFO] ------------------------------------------------------------------------

    日誌的輸出十分清楚,分別構建echo,api,biz這三個項目(因爲biz項目依賴api項目,因此api項目需要首先構建)。對每個項目構建時,將lifecycle中的phase所對應的goal依次執行。現在你能夠看懂maven的輸出日誌了吧?


小結

感謝博主https://www.jianshu.com/p/2f7080a4858c

感謝您的閱讀~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章