Maven系列第8篇:你的maven項目構建太慢了,我實在看不下去,帶你一塊兒磨刀!!多數使用maven的人都常常想要的一種功能,可是大多數人都不知道如何使用!!!

maven系列目標:從入門開始開始掌握一個高級開發所須要的maven技能。java

這是maven系列第8篇。mysql

整個maven系列的內容先後是有依賴的,若是以前沒有接觸過maven,建議從第一篇看起,本文尾部有maven完整系列的鏈接。react

本篇涉及到的內容屬於神技能,多數使用maven的人都常常想要的一種功能,可是大多數人都不知道如何使用,廢話很少說,上乾貨。sql

需求背景

咱們須要作一個電商項目,通常都會作成微服務的形式,按業務進行劃分,本次咱們主要以帳戶業務訂單業務爲例,咱們將這兩塊業務分別做爲2個大的模塊來開發,而訂單模塊又會調用帳戶模塊中的接口,因此對帳戶模塊接口部分會有依賴。shell

咱們以maven來搭建項目,項目的目錄結構以下:apache

b2b
    b2b-account
        b2b-account-api
        b2b-account-service
    b2b-order
        b2b-order-api
        b2b-order-service

b2b-account

帳戶模塊,其中包含2個小模塊:b2b-account-api和b2b-account-serviceapi

b2b-account-api

帳戶模塊對外暴露的接口部分,以供外部調用緩存

b2b-account-service

帳戶模塊具體業務實現,依賴於b2b-account-api模塊併發

b2b-order

訂單模塊,也是包含2個小模塊:b2b-order-api和b2b-order-serviceless

b2b-order-api

訂單模塊對外暴露的接口部分,以供外部調用

b2b-order-service

訂單模塊具體業務實現,依賴2個模塊b2b-order-api、b2b-account-api

搭建項目結構

咱們按照上面的需求,使用maven搭建項目結構,具體過程以下。

建立b2b項目

打開idea,點擊File->New->Project,以下圖:

選擇Maven,以下圖:

點擊上圖的Next,輸入座標信息,以下圖:

點擊上圖的Next,輸入Project Nameb2b,以下圖:

點擊上圖的Finish,完成項目的建立,以下圖:

刪除下圖中無用的代碼:

變成了下面這樣:

配置一下idea的maven環境,點擊File->Settings,以下圖:

點擊上面的OK完成配置。

在b2b下建立b2b-account模塊

注意這裏是建立模塊,不是項目了,注意下面的操做過程。

選中b2b項目目錄,以下圖:

點擊File->New->Module,以下圖:

選擇Maven,以下圖:

點擊Next,以下圖:

點擊上面第二個...,將Parent置爲None,以下圖:

輸入座標信息,以下圖:

點擊上圖中的Next,輸入Project nameb2b-account,以下圖:

點擊Finish,完成b2b-account模塊的建立,以下圖:

刪除b2b-accountsrc目錄,以下圖:

項目結構變成了下面這樣:

在b2b-account模塊下面建立b2b-account-api模塊

選中b2b-account,以下圖:

點擊File->New->Module,以下圖:

選擇Maven,以下圖:

點擊上圖中的Next,以下圖:

將上面的Add as module to設置爲b2b-account模塊,表示新增的這個模塊被b2b-account聚合進行管理。

點擊Parent後面的...,選擇None,將Parent對應的值置爲None

以下圖:

輸入座標信息,以下圖:

點擊Next,默認以下圖:

咱們須要對上面3個輸入框的值進行修改,修改爲下面這樣:

點擊Finish,完成建立,以下圖:

在b2b-account模塊下建立b2b-account-service模塊

這個過程參考在b2b-account模塊下面建立b2b-account-api模塊

在b2b下建立b2b-order模塊

這個過程參考在b2b下建立b2b-account模塊

在b2b-order模塊下面建立b2b-order-api模塊

這個過程參考在b2b-account模塊下面建立b2b-account-api模塊

在b2b-order模塊下面建立b2b-order-service模塊

這個過程參考在b2b-account模塊下面建立b2b-account-api模塊

項目結構建立成功

上面都建立成功以後,項目結構以下圖

參與過大型maven項目開發的是否是很熟悉很親切,多數互聯網項目的maven結構就是這樣一大片maven模塊。

引入項目依賴

b2b-account-service依賴於b2b-account-api模塊,因此在b2b-account-service/pom.xml中加入下面配置:

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>b2b-account-api</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

因爲項目的groupId,version都是同樣的,因此直接經過${}這種方式引用了。

b2b-order-service依賴2個模塊b2b-order-api、b2b-account-api,因此在b2b-order-service/pom.xml中加入下面配置:

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>b2b-account-api</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>b2b-order-api</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

此時每一個模塊pom.xml以下

b2b/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.javacode2018</groupId>
    <artifactId>b2b</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>b2b-account</module>
        <module>b2b-order</module>
    </modules>

</project>

b2b-account/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.javacode2018</groupId>
    <artifactId>b2b-account</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>b2b-account-api</module>
        <module>b2b-account-service</module>
    </modules>

</project>

b2b-account-api/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.javacode2018</groupId>
    <artifactId>b2b-account-api</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

b2b-account-service/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.javacode2018</groupId>
    <artifactId>b2b-account-service</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>b2b-account-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

</project>

b2b-order/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.javacode2018</groupId>
    <artifactId>b2b-order</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>b2b-order-api</module>
        <module>b2b-order-service</module>
    </modules>

</project>

b2b-order-api/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.javacode2018</groupId>
    <artifactId>b2b-order-api</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

b2b-order-service/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.javacode2018</groupId>
    <artifactId>b2b-order-service</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>b2b-account-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>b2b-order-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

</project>

注意:上面項目的結構屬於父子結構,使用maven中聚合的方式進行管理的,對應聚合不是太瞭解的,能夠看上篇文章有詳細介紹。

b2b中包含了2個子模塊b2b_accountb2b_order,這3個package都是pom。

b2b_account中包含了2個子模塊,2個子模塊package是默認的jar類型。

b2b_order中包含了2個子模塊,2個子模塊package是默認的jar類型。

反應堆

上面項目都開發好了,咱們須要安裝到本地倉庫,通常狀況下,咱們會這麼作,在b2b/pom.xml所在目錄執行下面命令:

mvn clean install

咱們來感覺一下這個命令的效果:

D:\code\IdeaProjects\b2b>mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-account-service                                                [jar]
[INFO] b2b-account                                                        [pom]
[INFO] b2b-order-api                                                      [jar]
[INFO] b2b-order-service                                                  [jar]
[INFO] b2b-order                                                          [pom]
[INFO] b2b                                                                [pom]
[INFO]
[INFO] ------------------< com.javacode2018:b2b-account-api >------------------
[INFO] Building b2b-account-api 1.0-SNAPSHOT                              [1/7]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account-api ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-account-api ---
[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) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-account-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-account-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-account-api ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account-api ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.pom
[INFO]
[INFO] ----------------< com.javacode2018:b2b-account-service >----------------
[INFO] Building b2b-account-service 1.0-SNAPSHOT                          [2/7]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account-service ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-account-service ---
[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) @ b2b-account-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-account-service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-account-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-account-service ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-account-service ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\target\b2b-account-service-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account-service ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\target\b2b-account-service-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-service\1.0-SNAPSHOT\b2b-account-service-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-service\1.0-SNAPSHOT\b2b-account-service-1.0-SNAPSHOT.pom
[INFO]
[INFO] --------------------< com.javacode2018:b2b-account >--------------------
[INFO] Building b2b-account 1.0-SNAPSHOT                                  [3/7]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account\1.0-SNAPSHOT\b2b-account-1.0-SNAPSHOT.pom
[INFO]
[INFO] -------------------< com.javacode2018:b2b-order-api >-------------------
[INFO] Building b2b-order-api 1.0-SNAPSHOT                                [4/7]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-order-api ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-order-api ---
[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) @ b2b-order-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-order-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-order-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-order-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-order-api ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target\b2b-order-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-order-api ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target\b2b-order-api-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-api\1.0-SNAPSHOT\b2b-order-api-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-api\1.0-SNAPSHOT\b2b-order-api-1.0-SNAPSHOT.pom
[INFO]
[INFO] -----------------< com.javacode2018:b2b-order-service >-----------------
[INFO] Building b2b-order-service 1.0-SNAPSHOT                            [5/7]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-order-service ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-order-service ---
[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) @ b2b-order-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-order-service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-order-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-order-service ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-order-service ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target\b2b-order-service-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-order-service ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target\b2b-order-service-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-service\1.0-SNAPSHOT\b2b-order-service-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-service\1.0-SNAPSHOT\b2b-order-service-1.0-SNAPSHOT.pom
[INFO]
[INFO] ---------------------< com.javacode2018:b2b-order >---------------------
[INFO] Building b2b-order 1.0-SNAPSHOT                                    [6/7]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-order ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-order ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order\1.0-SNAPSHOT\b2b-order-1.0-SNAPSHOT.pom
[INFO]
[INFO] ------------------------< com.javacode2018:b2b >------------------------
[INFO] Building b2b 1.0-SNAPSHOT                                          [7/7]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b ---
[INFO] Installing D:\code\IdeaProjects\b2b\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b\1.0-SNAPSHOT\b2b-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for b2b 1.0-SNAPSHOT:
[INFO]
[INFO] b2b-account-api .................................... SUCCESS [  1.806 s]
[INFO] b2b-account-service ................................ SUCCESS [  0.298 s]
[INFO] b2b-account ........................................ SUCCESS [  0.082 s]
[INFO] b2b-order-api ...................................... SUCCESS [  0.229 s]
[INFO] b2b-order-service .................................. SUCCESS [  0.254 s]
[INFO] b2b-order .......................................... SUCCESS [  0.058 s]
[INFO] b2b ................................................ SUCCESS [  0.069 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.072 s
[INFO] Finished at: 2019-11-20T16:03:27+08:00
[INFO] ------------------------------------------------------------------------

注意上面有這樣的輸出:

[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-account-service                                                [jar]
[INFO] b2b-account                                                        [pom]
[INFO] b2b-order-api                                                      [jar]
[INFO] b2b-order-service                                                  [jar]
[INFO] b2b-order                                                          [pom]
[INFO] b2b                                                                [pom]

maven會根據模塊之間的依賴關係,而後會獲得全部模塊的構建順序,上面共有7個pom.xml文件,也就是7個maven構件,按照上面列出的順序依次進行構建。

能夠看到b2b-account-api是被其餘一些模塊依賴的,因此這個放在了第一個。

mvn命令對多模塊構件時,會根據模塊的依賴關係而獲得模塊的構建順序,這個功能就是maven的反應堆(reactor)作的事情,反應堆會根據模塊之間的依賴關係、聚合關係、繼承關係等等,從而計算得出一個合理的模塊構建順序,因此反應堆的功能是至關強大的。

按需隨意構建

有這樣的一種場景:b2b-account-apib2b-account-serviceb2b-order-service依賴了,因此當b2b-account-api有修改的時候,咱們但願他們3個都可以被從新構建一次,而不是去對全部的模塊都進行從新構建,咱們只但願被影響的模塊都可以參與從新構建,你們有什麼好的辦法?

上面列出的只是2個模塊的功能,真正的電商項目還有不少模塊,若是每次修改一個模塊,咱們都去從新打包全部的模塊,這個構建過程耗時是很是久的,只能乾等着,咱們須要的是按需構建,須要構建哪些模塊讓咱們本身可以隨意指定,這樣也能夠加快構建的速度,因此咱們須要這樣的功能

maven反應堆幫咱們考慮到了這種狀況,mvn命令提供了一些功能能夠幫咱們實現這些操做,咱們看一下mvn的幫助文檔,mvn -h能夠查看幫助,以下:

D:\code\IdeaProjects\b2b>mvn -h

usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
 -am,--also-make                        If project list is specified, also
                                        build projects required by the
                                        list
 -amd,--also-make-dependents            If project list is specified, also
                                        build projects that depend on
                                        projects on the list
 -B,--batch-mode                        Run in non-interactive (batch)
                                        mode (disables output color)
 -b,--builder <arg>                     The id of the build strategy to
                                        use
 -C,--strict-checksums                  Fail the build if checksums don't
                                        match
 -c,--lax-checksums                     Warn if checksums don't match
 -cpu,--check-plugin-updates            Ineffective, only kept for
                                        backward compatibility
 -D,--define <arg>                      Define a system property
 -e,--errors                            Produce execution error messages
 -emp,--encrypt-master-password <arg>   Encrypt master security password
 -ep,--encrypt-password <arg>           Encrypt server password
 -f,--file <arg>                        Force the use of an alternate POM
                                        file (or directory with pom.xml)
 -fae,--fail-at-end                     Only fail the build afterwards;
                                        allow all non-impacted builds to
                                        continue
 -ff,--fail-fast                        Stop at first failure in
                                        reactorized builds
 -fn,--fail-never                       NEVER fail the build, regardless
                                        of project result
 -gs,--global-settings <arg>            Alternate path for the global
                                        settings file
 -gt,--global-toolchains <arg>          Alternate path for the global
                                        toolchains file
 -h,--help                              Display help information
 -l,--log-file <arg>                    Log file where all build output
                                        will go (disables output color)
 -llr,--legacy-local-repository         Use Maven 2 Legacy Local
                                        Repository behaviour, ie no use of
                                        _remote.repositories. Can also be
                                        activated by using
                                        -Dmaven.legacyLocalRepo=true
 -N,--non-recursive                     Do not recurse into sub-projects
 -npr,--no-plugin-registry              Ineffective, only kept for
                                        backward compatibility
 -npu,--no-plugin-updates               Ineffective, only kept for
                                        backward compatibility
 -nsu,--no-snapshot-updates             Suppress SNAPSHOT updates
 -ntp,--no-transfer-progress            Do not display transfer progress
                                        when downloading or uploading
 -o,--offline                           Work offline
 -P,--activate-profiles <arg>           Comma-delimited list of profiles
                                        to activate
 -pl,--projects <arg>                   Comma-delimited list of specified
                                        reactor projects to build instead
                                        of all projects. A project can be
                                        specified by [groupId]:artifactId
                                        or by its relative path
 -q,--quiet                             Quiet output - only show errors
 -rf,--resume-from <arg>                Resume reactor from specified
                                        project
 -s,--settings <arg>                    Alternate path for the user
                                        settings file
 -t,--toolchains <arg>                  Alternate path for the user
                                        toolchains file
 -T,--threads <arg>                     Thread count, for instance 2.0C
                                        where C is core multiplied
 -U,--update-snapshots                  Forces a check for missing
                                        releases and updated snapshots on
                                        remote repositories
 -up,--update-plugins                   Ineffective, only kept for
                                        backward compatibility
 -v,--version                           Display version information
 -V,--show-version                      Display version information
                                        WITHOUT stopping build
 -X,--debug                             Produce execution debug output

上面列出了mvn命令後面的一些選項,有幾個選項本次咱們須要用到,以下:

-pl,--projects <arg>

構件指定的模塊,arg表示多個模塊,之間用逗號分開,模塊有兩種寫法

-pl 模塊1相對路徑 [,模塊2相對路徑] [,模塊n相對路徑]
-pl [模塊1的groupId]:模塊1的artifactId [,[模塊2的groupId]:模塊2的artifactId] [,[模塊n的groupId]:模塊n的artifactId]

舉例:

下面命令都是在b2b/pom.xml來運行

mvn clean install -pl b2b-account
mvn clean install -pl b2b-account/b2b-account-api
mvn clean install -pl b2b-account/b2b-account-api,b2b-account/b2b-account-service
mvn clean install -pl :b2b-account-api,b2b-order/b2b-order-api
mvn clean install -pl :b2b-account-api,:b2b-order-service

-rf,--resume-from <arg>

從指定的模塊恢復反應堆

-am,--also-make

同時構建所列模塊的依賴模塊

-amd,--also-make-dependents

同時構件依賴於所列模塊的模塊

下面咱們經過6個案例細說上面這些用法。

注意:下面全部案例都在b2b/pom.xml所在目錄執行。

案例1

只構建p-account模塊,運行命令

mvn clean install -pl b2b-account

效果以下:

D:\code\IdeaProjects\b2b>mvn clean install -pl b2b-account
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.javacode2018:b2b-account >--------------------
[INFO] Building b2b-account 1.0-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account\1.0-SNAPSHOT\b2b-account-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.907 s
[INFO] Finished at: 2019-11-20T16:12:51+08:00
[INFO] ------------------------------------------------------------------------

能夠看到只構建了b2b-account

案例2

只構建b2b-account-api模塊,運行命令:

mvn clean install -pl b2b-account/b2b-account-api

效果以下:

D:\code\IdeaProjects\b2b>mvn clean install -pl b2b-account/b2b-account-api
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.javacode2018:b2b-account-api >------------------
[INFO] Building b2b-account-api 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account-api ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-account-api ---
[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) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-account-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-account-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-account-api ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account-api ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.853 s
[INFO] Finished at: 2019-11-20T16:14:37+08:00
[INFO] ------------------------------------------------------------------------

上面使用的是相對路徑的寫法,還有2種寫法,你們能夠去試試,以下:

mvn clean install -pl :b2b-account-api
mvn clean install -pl com.javacode2018:b2b-account-api

案例3

構建b2b-account-api和b2b-order-api,運行下面命令:

mvn clean install -pl b2b-account/b2b-account-api,b2b-order/b2b-order-api

效果以下:

D:\code\IdeaProjects\b2b>mvn clean install -pl b2b-account/b2b-account-api,b2b-order/b2b-order-api
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-order-api                                                      [jar]
[INFO]
[INFO] ------------------< com.javacode2018:b2b-account-api >------------------
[INFO] Building b2b-account-api 1.0-SNAPSHOT                              [1/2]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account-api ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-account-api ---
[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) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-account-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-account-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-account-api ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account-api ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.pom
[INFO]
[INFO] -------------------< com.javacode2018:b2b-order-api >-------------------
[INFO] Building b2b-order-api 1.0-SNAPSHOT                                [2/2]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-order-api ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-order-api ---
[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) @ b2b-order-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-order-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-order-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-order-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-order-api ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target\b2b-order-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-order-api ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target\b2b-order-api-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-api\1.0-SNAPSHOT\b2b-order-api-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-api\1.0-SNAPSHOT\b2b-order-api-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for b2b-account-api 1.0-SNAPSHOT:
[INFO]
[INFO] b2b-account-api .................................... SUCCESS [  2.874 s]
[INFO] b2b-order-api ...................................... SUCCESS [  0.393 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.554 s
[INFO] Finished at: 2019-11-20T16:15:57+08:00
[INFO] ------------------------------------------------------------------------

上面輸出中能夠看到只構建了2個目標模塊。

案例4

咱們只修改了b2b-account-api代碼,因此我但願對這個從新打包,而且對這個有依賴的也從新打包,因此須要打包下面3個模塊:

b2b-account-api
b2b-account-service
b2b-order-service

上面列表中的後面兩個是依賴於b2b-account-api的,能夠在b2b/pom.xml中執行下面命令:

mvn clean install -pl b2b-account/b2b-account-api -amd

咱們看一下效果:

D:\code\IdeaProjects\b2b>mvn clean install -pl b2b-account/b2b-account-api -amd
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-account-service                                                [jar]
[INFO] b2b-order-service                                                  [jar]
[INFO]
[INFO] ------------------< com.javacode2018:b2b-account-api >------------------
[INFO] Building b2b-account-api 1.0-SNAPSHOT                              [1/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account-api ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-account-api ---
[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) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-account-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-account-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-account-api ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account-api ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.pom
[INFO]
[INFO] ----------------< com.javacode2018:b2b-account-service >----------------
[INFO] Building b2b-account-service 1.0-SNAPSHOT                          [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account-service ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-account-service ---
[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) @ b2b-account-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-account-service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-account-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-account-service ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-account-service ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\target\b2b-account-service-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account-service ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\target\b2b-account-service-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-service\1.0-SNAPSHOT\b2b-account-service-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-service\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-service\1.0-SNAPSHOT\b2b-account-service-1.0-SNAPSHOT.pom
[INFO]
[INFO] -----------------< com.javacode2018:b2b-order-service >-----------------
[INFO] Building b2b-order-service 1.0-SNAPSHOT                            [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-order-service ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-order-service ---
[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) @ b2b-order-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-order-service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-order-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-order-service ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-order-service ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target\b2b-order-service-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-order-service ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target\b2b-order-service-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-service\1.0-SNAPSHOT\b2b-order-service-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-service\1.0-SNAPSHOT\b2b-order-service-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for b2b-account-api 1.0-SNAPSHOT:
[INFO]
[INFO] b2b-account-api .................................... SUCCESS [  2.440 s]
[INFO] b2b-account-service ................................ SUCCESS [  0.381 s]
[INFO] b2b-order-service .................................. SUCCESS [  0.364 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.508 s
[INFO] Finished at: 2019-11-20T16:05:00+08:00
[INFO] ------------------------------------------------------------------------

從上面輸出中看一下反應堆列出的構建順序:b2b-account-api、b2b-account-service、b2b-order-service。

上面過程給你們捋一捋:

上面命令先會運行-pl b2b-account-api獲得一個反應堆列表,以下,只有一個模塊:

b2b-account-api

而後後面又會執行amd,這個命令會找到對-pl b2b-account-api有依賴的構件,也就是:

b2b-account-service
b2b-order-serivce

而後反應堆會對3個構件進行排序,獲得一個正確的構件順序,以下:

[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-account-service                                                [jar]
[INFO] b2b-order-service                                                  [jar]

而後maven會依次對他們執行:

mvn clean install

案例5

需求:咱們來構建b2b-order-service,但願b2b-account-service依賴的構件也能被同時構建,b2b-account-service依賴於b2b-account-apib2b-order-api,可能b2b-account-service會依賴的更多。

可使用下面命令:

mvn clean install -pl b2b-order/b2b-order-service -am

效果以下:

D:\code\IdeaProjects\b2b>mvn clean install -pl b2b-order/b2b-order-service -am
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-order-api                                                      [jar]
[INFO] b2b-order-service                                                  [jar]
[INFO]
[INFO] ------------------< com.javacode2018:b2b-account-api >------------------
[INFO] Building b2b-account-api 1.0-SNAPSHOT                              [1/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-account-api ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-account-api ---
[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) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-account-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-account-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-account-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-account-api ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-account-api ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\target\b2b-account-api-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-account\b2b-account-api\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-account-api\1.0-SNAPSHOT\b2b-account-api-1.0-SNAPSHOT.pom
[INFO]
[INFO] -------------------< com.javacode2018:b2b-order-api >-------------------
[INFO] Building b2b-order-api 1.0-SNAPSHOT                                [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-order-api ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-order-api ---
[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) @ b2b-order-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-order-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-order-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-order-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-order-api ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target\b2b-order-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-order-api ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\target\b2b-order-api-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-api\1.0-SNAPSHOT\b2b-order-api-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-api\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-api\1.0-SNAPSHOT\b2b-order-api-1.0-SNAPSHOT.pom
[INFO]
[INFO] -----------------< com.javacode2018:b2b-order-service >-----------------
[INFO] Building b2b-order-service 1.0-SNAPSHOT                            [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ b2b-order-service ---
[INFO] Deleting D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ b2b-order-service ---
[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) @ b2b-order-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ b2b-order-service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ b2b-order-service ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ b2b-order-service ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ b2b-order-service ---
[INFO] Building jar: D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target\b2b-order-service-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ b2b-order-service ---
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\target\b2b-order-service-1.0-SNAPSHOT.jar to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-service\1.0-SNAPSHOT\b2b-order-service-1.0-SNAPSHOT.jar
[INFO] Installing D:\code\IdeaProjects\b2b\b2b-order\b2b-order-service\pom.xml to C:\Users\Think\.m2\repository\com\javacode2018\b2b-order-service\1.0-SNAPSHOT\b2b-order-service-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for b2b-account-api 1.0-SNAPSHOT:
[INFO]
[INFO] b2b-account-api .................................... SUCCESS [  2.509 s]
[INFO] b2b-order-api ...................................... SUCCESS [  0.343 s]
[INFO] b2b-order-service .................................. SUCCESS [  0.340 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.455 s
[INFO] Finished at: 2019-11-20T16:46:45+08:00
[INFO] ------------------------------------------------------------------------

從上面輸出中看一下反應堆列出的構建順序:b2b-account-api、b2b-order-api、b2b-order-service。

上面過程給你們捋一捋:

上面命令先會運行-pl b2b-order-service獲得一個反應堆列表:

b2b-order-service

而後後面又會執行am,這個命令會找到-pl b2b-order-service依賴的構件,也就是:

b2b-account-api
b2b-order-api

而後反應堆會對3個構件進行排序,獲得一個正確的構件順序,以下:

[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-order-api                                                      [jar]
[INFO] b2b-order-service                                                  [jar]

而後maven會依次對他們執行:

mvn clean install

案例6

分別執行下面2條mvn命令,先看一下效果:

mvn clean install
mvn clean install -rf b2b-order/b2b-order-service

輸出中咱們取出部份內容,以下。

第一條命令,反應堆產生的構建順序是:

[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-account-service                                                [jar]
[INFO] b2b-account                                                        [pom]
[INFO] b2b-order-api                                                      [jar]
[INFO] b2b-order-service                                                  [jar]
[INFO] b2b-order                                                          [pom]
[INFO] b2b                                                                [pom]

第2條命令,反應堆產生的構建順序是:

[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-order-service                                                  [jar]
[INFO] b2b-order                                                          [pom]
[INFO] b2b                                                                [pom]

在仔細看一下上面2條命令的差異,後面的命令多了-rf b2b-order/b2b-order-service,具體過程以下:

會先執行下面命令

mvn clean install

反應堆會計算出須要構件的模塊順序,以下:

[INFO] Reactor Build Order:
[INFO]
[INFO] b2b-account-api                                                    [jar]
[INFO] b2b-account-service                                                [jar]
[INFO] b2b-account                                                        [pom]
[INFO] b2b-order-api                                                      [jar]
[INFO] b2b-order-service                                                  [jar]
[INFO] b2b-order                                                          [pom]
[INFO] b2b                                                                [pom]

-rf b2b-order/b2b-order-service對上面的反應堆構件順序進行裁剪,將b2b-order/b2b-order-service前面的部分幹掉,從b2b-order/b2b-order-service開始執行構建操做,因此剩下了3個須要構建的模塊。

總結

  1. 須要掌握mvn命令中-pl、-am、-amd、-rf的各類用法
  2. 注意-pl後面的參數的寫法:模塊相對路徑、或者[groupId].artifactId

仍是那句話,上面這些用法你們會常常用到的,建議你們下去了多練練。看和操做,所獲取到的是不能比的,看的過程當中可能以爲一切都知道了,可是實際操做就不同了,可能中間會遇到各類問題,而後本身會想辦法解決這些問題,領會和學到的東西是不同的!

Maven系列目錄

  1. Maven系列:第1篇:Maven未出世前,咱們那些痛苦的經歷!
  2. Maven系列第2篇:安裝、配置、mvn運行過程詳解
  3. Maven系列第3篇:詳解maven解決依賴問題
  4. Maven系列第4篇:倉庫詳解
  5. Maven系列第5篇:私服詳解
  6. Maven系列第6篇:生命週期和插件詳解,高手必備!
  7. Maven系列第7篇:聚合、繼承、單繼承問題詳解,必備技能!

更多好文章

  1. java高併發系列(共34篇)
  2. mysql高手系列(共27篇)
  3. 聊聊db和緩存一致性常見的實現方式
  4. 接口冪等性這麼重要,它是什麼?怎麼實現?
相關文章
相關標籤/搜索