当前位置: 首页 > news >正文

Maven系列第8篇:大型Maven项目,快速按需任意构建

本篇涉及到的内容属于神技能,多数使用maven的人都经常想要的一种功能,但是大多数人都不知道如何使用,废话不多说,上干货。

需求背景

我们需要做一个电商项目,一般都会做成微服务的形式,按业务进行划分,本次我们主要以账户业务订单业务为例,我们将这两块业务分别作为2个大的模块来开发,而订单模块又会调用账户模块中的接口,所以对账户模块接口部分会有依赖。

我们以maven来搭建项目,项目的目录结构如下:

b2bb2b-accountb2b-account-apib2b-account-serviceb2b-orderb2b-order-apib2b-order-service
b2b-account

账户模块,其中包含2个小模块:b2b-account-api和b2b-account-service

b2b-account-api

账户模块对外暴露的接口部分,以供外部调用

b2b-account-service

账户模块具体业务实现,依赖于b2b-account-api模块

b2b-order

订单模块,也是包含2个小模块:b2b-order-api和b2b-order-service

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 -husage: mvn [options] [<goal(s)>] [<phase(s)>]Options:-am,--also-make                        If project list is specified, alsobuild projects required by thelist-amd,--also-make-dependents            If project list is specified, alsobuild projects that depend onprojects on the list-B,--batch-mode                        Run in non-interactive (batch)mode (disables output color)-b,--builder <arg>                     The id of the build strategy touse-C,--strict-checksums                  Fail the build if checksums don'tmatch-c,--lax-checksums                     Warn if checksums don't match-cpu,--check-plugin-updates            Ineffective, only kept forbackward 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 POMfile (or directory with pom.xml)-fae,--fail-at-end                     Only fail the build afterwards;allow all non-impacted builds tocontinue-ff,--fail-fast                        Stop at first failure inreactorized builds-fn,--fail-never                       NEVER fail the build, regardlessof project result-gs,--global-settings <arg>            Alternate path for the globalsettings file-gt,--global-toolchains <arg>          Alternate path for the globaltoolchains file-h,--help                              Display help information-l,--log-file <arg>                    Log file where all build outputwill go (disables output color)-llr,--legacy-local-repository         Use Maven 2 Legacy LocalRepository behaviour, ie no use of_remote.repositories. Can also beactivated by using-Dmaven.legacyLocalRepo=true-N,--non-recursive                     Do not recurse into sub-projects-npr,--no-plugin-registry              Ineffective, only kept forbackward compatibility-npu,--no-plugin-updates               Ineffective, only kept forbackward compatibility-nsu,--no-snapshot-updates             Suppress SNAPSHOT updates-ntp,--no-transfer-progress            Do not display transfer progresswhen downloading or uploading-o,--offline                           Work offline-P,--activate-profiles <arg>           Comma-delimited list of profilesto activate-pl,--projects <arg>                   Comma-delimited list of specifiedreactor projects to build insteadof all projects. A project can bespecified by [groupId]:artifactIdor by its relative path-q,--quiet                             Quiet output - only show errors-rf,--resume-from <arg>                Resume reactor from specifiedproject-s,--settings <arg>                    Alternate path for the usersettings file-t,--toolchains <arg>                  Alternate path for the usertoolchains file-T,--threads <arg>                     Thread count, for instance 2.0Cwhere C is core multiplied-U,--update-snapshots                  Forces a check for missingreleases and updated snapshots onremote repositories-up,--update-plugins                   Ineffective, only kept forbackward compatibility-v,--version                           Display version information-V,--show-version                      Display version informationWITHOUT 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系列第8篇:大型Maven项目,快速按需任意构建

本篇涉及到的内容属于神技能&#xff0c;多数使用maven的人都经常想要的一种功能&#xff0c;但是大多数人都不知道如何使用&#xff0c;废话不多说&#xff0c;上干货。 需求背景 我们需要做一个电商项目&#xff0c;一般都会做成微服务的形式&#xff0c;按业务进行划分&am…...

卷积神经网络(CNN)的组成结构以及其优点

卷积神经网络&#xff08;Convolutional Neural Network&#xff0c;简称CNN&#xff09;是一种深度学习模型&#xff0c;主要用于处理具有网格结构的数据&#xff0c;如图像和视频。它的结构包含以下几个关键组件&#xff1a; 卷积层&#xff08;Convolutional Layer&#xff…...

[③ADRV902x]: Digital Filter Configuration(接收端)

前言 本篇博客主要总结了ADRV9029 Rx接收端链路中各个滤波器的配置。配置不同的滤波器系数以及不同的参数&#xff0c;可以对输入的数字信号灵活得做decimation处理&#xff0c;decimation信号抽取&#xff0c;就是降低信号采样率的过程。 Receiver Signal Path 下图为接收端…...

企业安全—DevSecOps概述详情

0x00 前言 SDL存在的问题在于体量过于庞大&#xff0c;不利于快速进行适配和进行&#xff0c;所以就有了DevSecOps&#xff0c;实际上是因为敏捷开发也就是DevOps的推进&#xff0c;并且坐上了云服务模式的火车&#xff0c;所以这一系列的东西都开始普及。DevSecOps作为DevOps…...

数据结构与算法(十):动态规划与贪心算法

参考引用 Hello 算法 Github&#xff1a;hello-algo 1. 动态规划算法 动态规划将一个问题分解为一系列更小的子问题&#xff0c;并通过存储子问题的解来避免重复计算&#xff0c;从而大幅提升时间效率 问题&#xff1a;给定一个共有 n 阶的楼梯&#xff0c;你每步可以上 1 阶或…...

【C++代码】安排行程,N皇后,解数独--代码随想录

题目&#xff1a;重新安排行程 给你一份航线列表 tickets &#xff0c;其中 tickets[i] [fromi, toi] 表示飞机出发和降落的机场地点。请你对该行程进行重新规划排序。所有这些机票都属于一个从 JFK&#xff08;肯尼迪国际机场&#xff09;出发的先生&#xff0c;所以该行程必…...

SpringCloud Alibaba【二】nacos

nacos配置与使用 nacos初步使用nacos安装与配置创建命名空间 nacos使用与配置创建新项目作为父项目 创建nacos服务端项目pom.xmlapplication.yml启动类 创建nacos客户端项目pom.xml application.yml启动类 启动测试 nacos配置负载均衡改造生产者nacos-provider-projectcontroll…...

C++中的fsanitize指令

一个集成在 gcc、clang 编译器中的编译指令&#xff0c;可以有效测试程序中的一些诸如数组越界、未定义行为等情况。 举个例子&#xff1a; #include <bits/stdc.h> using namespace std;const int maxn2e55,mxr1e5,maxm1e75; int head[maxn],nxt[maxn],to[maxn],f[max…...

【AI视野·今日Robot 机器人论文速览 第五十八期】Thu, 19 Oct 2023

AI视野今日CS.Robotics 机器人学论文速览 Thu, 19 Oct 2023 Totally 25 papers &#x1f449;上期速览✈更多精彩请移步主页 Daily Robotics Papers InViG: Benchmarking Interactive Visual Grounding with 500K Human-Robot Interactions Authors Hanbo Zhang, Jie Xu, Yuch…...

Java截取(提取)子字符串(substring()),Java分割字符串(split())

在 String 中提供了两个截取字符串的方法&#xff0c;一个是从指定位置截取到字符串结尾&#xff0c;另一个是截取指定范围的内容。下面对这两种方法分别进行介绍。 1. substring(int beginIndex) 形式 此方式用于提取从索引位置开始至结尾处的字符串部分。调用时&#xff0c…...

从厨房间到股市:家庭主妇的华美转身

我一直是一个安于现状的家庭主妇。生活中&#xff0c;我热爱烹饪、园艺和照顾家人&#xff0c;但我也渴望能有更多的自我实现和价值感。在机缘巧合下&#xff0c;我接触到了卓扬网&#xff0c;一个专业的股票投资平台。从那刻起&#xff0c;我的人生发生了翻天覆地的变化。 初…...

Oracle 数据库的锁排查方法

关键字 oracle lock 问题描述 Oracle 数据库上锁问题如何排查 解决问题思路 准备数据 create table lock_test(name varchar(10),age varchar(10));insert into lock_test values(ff,10); insert into lock_test values(yy,20); insert into lock_test values(ll,30);Orac…...

混合精度训练原理之float16和float32数据之间的互相转换

混合精度训练原理之float16和float32数据之间的互相转换 本篇文章参考&#xff1a;全网最全-混合精度训练原理 上述文章已经讲解的比较详细&#xff0c;本文只是从数值角度分析&#xff1a; 1. float32转入float16的精度误差 2. 在深度学习的混精度训练当中&#xff0c;当参数…...

网络协议--ICMP:Internet控制报文协议

6.1 引言 ICMP经常被认为是IP层的一个组成部分。它传递差错报文以及其他需要注意的信息。ICMP报文通常被IP层或更高层协议&#xff08;TCP或UDP&#xff09;使用。一些ICMP报文把差错报文返回给用户进程。 ICMP报文是在IP数据报内部被传输的&#xff0c;如图6-1所示。 ICMP…...

《红蓝攻防对抗实战》三.内网探测协议出网之HTTP/HTTPS协议探测出网

目录 一. 在 Windows 操作系统中探测 HTTP/HTTPS 出网 1. Bitsadmin 命令 2.Certuil 命令 2.Linux系统探测HTTP/HTTPS出网 1.Curl命令 2.Wget命令 对目标服务器探测 HTTP/HTTPS 是否出网时&#xff0c;要根据目标系统类型执行命令&#xff0c;不同类型的操作系统使用的探…...

【Win11】系统重装教程(最新最详细)

目录 一.简介 二.用U盘制作PE系统 三、安装系统 软件&#xff1a;Windows 11版本&#xff1a;21H2语言&#xff1a;简体中文大小&#xff1a;5.14G安装环境&#xff1a;PE系统&#xff0c;至少7代处理器硬件要求&#xff1a;CPU2.0GHz 内存4G(或更高&#xff09;下载通道①丨…...

如何构建一个外卖微信小程序

随着外卖行业的不断发展&#xff0c;越来越多的商家开始关注外卖微信小程序的开发。微信小程序具有使用方便、快速上线、用户覆盖广等优势&#xff0c;成为了商家们的首选。 那么&#xff0c;如何快速开发一个外卖微信小程序呢&#xff1f;下面就让我们来看看吧&#xff01; 首…...

小知识(5) el-table行样式失效问题

一、实现效果 子级呈现不同颜色去区分 二、最初代码 tips: 我这里使用的vue3 elementplus <el-table :row-class-name"tableRowClassName" >... </el-table>function tableRowClassName({ row, rowIndex }) {if (row.children.length 0) {return …...

【Docker】Docker数据的存储

默认情况下&#xff0c;在运行中的容器里创建的文件&#xff0c;被保存在一个可写的容器层里&#xff0c;如果容器被删除了&#xff0c;则对应的数据也随之删除了。 这个可写的容器层是和特定的容器绑定的&#xff0c;也就是这些数据无法方便的和其它容器共享。 Docker主要提…...

hive字段关键字问题处理

最近在xxl_job部署shell调度任务时,发现在编写Hql时&#xff0c;对一些使用关键字命名的字段无法解析&#xff0c;按开发规范&#xff0c;字段命名不应该有关键字,但是数据来源是第三方,无法修改,需要通过flume对从kafka的数据到hdfs上&#xff0c;数据是json格式,所以需要对关…...

HTTP、WebSocket、SSE 对比

特性HTTPWebSocketSSE (Server-Sent Events)通信模式请求-响应&#xff08;单向&#xff09;全双工双向通信服务器到客户端的单向通信连接方式短连接&#xff08;默认&#xff09;长连接长连接协议基础TCP&#xff08;HTTP/1.1, HTTP/2&#xff09;基于HTTP升级基于HTTP数据格式…...

如何让AI自己检查全文?使用OCR和LLM实现自动“全文校订”(可DIY校订规则)

详细流程及描述参见仓库&#xff08;如果有用的话&#xff0c;请给个收藏&#xff09;&#xff1a; GitHub - xurongtang/DocRevision_Proj: A simple project about how to revist docment (such as your academic paper) in a automatic way with the help of OCR and LLM.A…...

go语言的锁

本篇文章主要讲锁&#xff0c;主要会涉及go的sync.Mutex和sync.RWMutex。 一.锁的概念和发展 1.1 锁的概念 所谓的加锁和解锁其实就是指一个数据是否被占用了&#xff0c;通过Mutex内的一个状态来表示。 例如&#xff0c;取 0 表示未加锁&#xff0c;1 表示已加锁&#xff…...

Python读取PDF:文本、图片与文档属性

在日常的数据采集、文档归档与信息挖掘过程中&#xff0c;PDF格式因其版式固定、内容稳定而被广泛使用。Python 开发者若希望实现 PDF 内容的自动化提取&#xff0c;选择一个易用且功能完善的库至关重要。本文将介绍如何用Python实现 PDF文本读取、图片提取 以及 文档属性读取 …...

云原生 DevOps 实践路线:构建敏捷、高效、可观测的交付体系

&#x1f4dd;个人主页&#x1f339;&#xff1a;一ge科研小菜鸡-CSDN博客 &#x1f339;&#x1f339;期待您的关注 &#x1f339;&#x1f339; 一、引言&#xff1a;DevOps 与云原生的深度融合 在传统软件工程范式下&#xff0c;开发与运维之间存在天然的壁垒。开发希望尽快…...

bug 记录 - 使用 el-dialog 的 before-close 的坑

需求说明 弹窗中内嵌一个 form 表单 原始代码 <script setup lang"ts"> import { reactive, ref } from "vue" import type { FormRules } from element-plus const ruleFormRef ref() interface RuleForm {name: stringregion: number | null } …...

10_聚类

描述 聚类&#xff08;clustering&#xff09;是将数据集划分成组的任务&#xff0c;这些组叫作簇&#xff08;cluster&#xff09;。其目标是划分数据&#xff0c;使得一个簇内的数据点非常相似且不同簇内的数据点非常不同。与分类算法类似&#xff0c;聚类算法为每个数据点分…...

功能测试、性能测试、安全测试详解

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 一、功能测试 1、单接口功能 手工测试中的单个业务模块&#xff0c;一般对应一个接口 例如&#xff1a; 登录业务------登录接口 加入购物车业务------加入购…...

vue2中的render函数

<script> export default {components: {},name: "renderElems",render (h, context) {return this.$attrs.vnode;},updated() {} } </script> <style scoped> </style>分析一下上面.vue组件&#xff1a; 组件结构&#xff1a; 这是一个非…...

Mac电脑_钥匙串操作选项变灰的情况下如何删除?

Mac电脑_钥匙串操作选项变灰的情况下如何删除&#xff1f; 这时候 可以使用相关的终端命令进行操作。 下面附加文章《Mac电脑_钥匙串操作的终端命令》。 《Mac电脑_钥匙串操作的终端命令》 &#xff08;来源&#xff1a;百度~百度AI 发布时间&#xff1a;2025-06&#xff09;…...