theboyaply
theboyaply
发布于 2020-08-05 / 540 阅读
0
0

7-maven聚合和继承

原文链接:https://mp.weixin.qq.com/s/-gp7Q9HWiP19ITunnwqdsw

聚合

需求

假设我们需要使用java做一个电商网站,涉及到:pc端网站、h5微站、移动端接口部分,那么我们可以使用maven创建3个项目用于这3块业务的开发,3个项目名称如下:

mall-pc
mall-h5
mall-api

这3个项目的GroupId都是com.theboyaply,ArtifactId取上面的,我们使用maven来搭建项目结构。

创建普通maven项目

使用idea创建第一个mall-pc项目。

打开idea,点击File->New->Project,选择Maven,如下:

maven07-01

点击next,输入项目坐标信息:

idea-create-maven02

上图是以前截的,我们这里的值分别为:

  • GroupId:com.theboyaply
  • ArtifactId:mall-pc
  • Version:1.0-SNAPSHOT

点击next,输入Project namemall-pc,修改项目路径。如下:

maven07-02

点击Finish完成。项目如下:

maven07-03

配置一下idea的maven环境,点击File->Settings,如下图:

maven07-04

使用maven默认的setting.xml地址和本地仓库地址。

再按照上面的步骤,创建另外两个项目mall-h5mall-api

如下:

maven07-05

maven07-06

我们使用mvn package进行打包,需要在每个项目的pom.xml所在目录都去执行一次这个命令,也就是说需要执行3次,这个电商项目还会涉及到后台系统、bi系统、监控系统等等,可能最后会多达10个小项目,那时候我们每次上线都需要执行10次打包操作,这个过程是相当繁琐的。

那么maven有没有更好的办法来解决这个事情呢?

这个用到的就是我们本次要说的maven中的聚合

整个电商我们可以作为一个大的系统,上面的pc端、h5微站、api接口、后台系统、bi系统、监控系统都可以作为里面的一个具体比较大一个模块。

我们使用maven聚合功能来实现上面的需求,我们需要创建一个额外的maven项目mall-aggregator来管理上面3个项目,然后只用在mall-aggregator项目中执行mvn命令,就会自动为其他3个项目自动执行同样的mvn命令。

maven聚合

maven聚合需要创建一个新的maven项目, 用来管理其他的maven构件模块,新的maven项目中需要如下配置:

<modules>
    <module>模块1</module>
    <module>模块2</module>
    <module>模块n</module>
</modules>
<package>pom</package>

新的项目中执行任何mvn命令,都会modules中包含的所有模块执行同样的命令,而被包含的模块不需要做任何特殊的配置,正常的maven项目就行。

注意上面的module元素,这部分是被聚合的模块pom.xml所在目录的相对路径。

package的值必须为pom,这个需要注意。

maven聚合案例

创建聚合项目

创建项目mall-aggregator

打开idea,点击File->New->Project,选择Maven,如下:

maven07-07

点击Next,输入项目坐标信息,如下:

maven07-08

点击Next,输入Project name 为mall-aggregator,如下:

maven07-09

点击Finish,创建成功,如下:

maven07-10

配置一下idea的maven环境,点击File->Settings,与上面的普通maven项目一样,设置到~/.m2路径下,这里就不截图展示了。

删除.ideasrc两个文件夹,聚合项目不需要。删除后项目结构如下:

maven07-11

pom.xml中加入下面配置:

<packaging>pom</packaging>

最后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.theboyaply</groupId>
    <artifactId>mall-aggregator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    
</project>

创建子模块

注意这里说的是模块,不再是创建项目,过程仔细看。

我们先创建mall-pa模块。

idea选中mall-aggregator

maven07-12

然后直接右键->New->Module或者点击File->New->Module都可:

maven07-13

点击选择Module后,如图:

maven07-14

选择Maven,然后点击Next,如下图:

maven07-15

点击Parent选项后面的...按钮(即第二个...),选择None,如下:

说明:

第一个...是设置聚合的。

第二个...是设置继承的,下面会讲到。

maven07-16

maven07-17

输入坐标信息,如下:

maven07-18

点击Next,输入Module name:mall-pc,如下:

maven07-19

然后点击Finish,完成mall-pc模块的创建,如下:

maven07-20

这时我们来看一下mall-aggregator项目的pom.xml:

maven07-21

可以看到比之前多了一个modules的配置。注意这里的packaging配置类型是pom,不是jarwar之类的。

创建模块mall-h5mall-api,这里的创建步骤和mall-pc模块一样,就不演示了。

最终的项目结构如下:

maven07-22

可以看到mall-aggregator项目的pom.xml中的modules元素,包含了我们创建的3个子模块。

<modules>
    <module>mall-pc</module>
    <module>mall-h5</module>
    <module>mall-api</module>
</modules>

这里你可以分别打开3个子模块的pom.xml文件看看内容,你会发现,和我们上面创建普通maven项目的pom.xml没什么区别。

聚合项目下执行maven命令

mall-aggregator项目的根目录下(即pom.xml所在目录),执行mvn package

D:\myResource\maven\maven5\mall-aggregator>mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] mall-pc                                                            [jar]
[INFO] mall-h5                                                            [jar]
[INFO] mall-api                                                           [jar]
[INFO] mall-aggregator                                                    [pom]
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT                                      [1/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mall-pc ---
[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) @ mall-pc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mall-pc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven5\mall-aggregator\mall-pc\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mall-pc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mall-pc ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mall-pc ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-aggregator\mall-pc\target\mall-pc-1.0-SNAPSHOT.jar
[INFO]
[INFO] -----------------------< com.theboyaply:mall-h5 >-----------------------
[INFO] Building mall-h5 1.0-SNAPSHOT                                      [2/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mall-h5 ---
[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) @ mall-h5 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mall-h5 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven5\mall-aggregator\mall-h5\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mall-h5 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mall-h5 ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mall-h5 ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-aggregator\mall-h5\target\mall-h5-1.0-SNAPSHOT.jar
[INFO]
[INFO] ----------------------< com.theboyaply:mall-api >-----------------------
[INFO] Building mall-api 1.0-SNAPSHOT                                     [3/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mall-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) @ mall-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mall-api ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven5\mall-aggregator\mall-api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mall-api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mall-api ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mall-api ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-aggregator\mall-api\target\mall-api-1.0-SNAPSHOT.jar
[INFO]
[INFO] -------------------< com.theboyaply:mall-aggregator >-------------------
[INFO] Building mall-aggregator 1.0-SNAPSHOT                              [4/4]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] mall-pc ............................................ SUCCESS [  3.102 s]
[INFO] mall-h5 ............................................ SUCCESS [  0.140 s]
[INFO] mall-api ........................................... SUCCESS [  0.141 s]
[INFO] mall-aggregator 1.0-SNAPSHOT ....................... SUCCESS [  0.016 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.563 s
[INFO] Finished at: 2020-08-03T01:36:54+08:00
[INFO] ------------------------------------------------------------------------

其中有如下输出:

[INFO] Reactor Build Order:
[INFO]
[INFO] mall-pc                                                            [jar]
[INFO] mall-h5                                                            [jar]
[INFO] mall-api                                                           [jar]
[INFO] mall-aggregator                                                    [pom]

上面这个列出了需要构件的maven构件列表及顺序,共有4个构件,3个jar包,1个pom类型的构件。

然后开始按照上面列出的顺序,一个个开始执行mvn package命令,最后4个都执行成功了,我们来看一下最终产生的效果,如下图:

maven07-23

从上图中我们可以看到3个模块都生成了jar包,说明我们执行了一次mvn package,分别在3个模块中都运行了,我们在来执行一下mvn clean清理代码,感受一下最终效果:

D:\myResource\maven\maven5\mall-aggregator>mvn clean
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] mall-pc                                                            [jar]
[INFO] mall-h5                                                            [jar]
[INFO] mall-api                                                           [jar]
[INFO] mall-aggregator                                                    [pom]
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT                                      [1/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mall-pc ---
[INFO] Deleting D:\myResource\maven\maven5\mall-aggregator\mall-pc\target
[INFO]
[INFO] -----------------------< com.theboyaply:mall-h5 >-----------------------
[INFO] Building mall-h5 1.0-SNAPSHOT                                      [2/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mall-h5 ---
[INFO] Deleting D:\myResource\maven\maven5\mall-aggregator\mall-h5\target
[INFO]
[INFO] ----------------------< com.theboyaply:mall-api >-----------------------
[INFO] Building mall-api 1.0-SNAPSHOT                                     [3/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mall-api ---
[INFO] Deleting D:\myResource\maven\maven5\mall-aggregator\mall-api\target
[INFO]
[INFO] -------------------< com.theboyaply:mall-aggregator >-------------------
[INFO] Building mall-aggregator 1.0-SNAPSHOT                              [4/4]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mall-aggregator ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] mall-pc ............................................ SUCCESS [  0.258 s]
[INFO] mall-h5 ............................................ SUCCESS [  0.038 s]
[INFO] mall-api ........................................... SUCCESS [  0.028 s]
[INFO] mall-aggregator 1.0-SNAPSHOT ....................... SUCCESS [  0.007 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.493 s
[INFO] Finished at: 2020-08-03T01:43:23+08:00
[INFO] ------------------------------------------------------------------------

从上面的输出信息看到,3个子模块都执行了mvn clean命令。这里就不再把清理后的项目结构截图出来了。

上面介绍了pom.xml中的module元素的值为被聚合的模块pom.xml所在的目录路径,可以是相对路径,也可以是绝对路径,上面演示的是相对路径,大家可以自己去玩一下绝对路径的情况。

聚合的功能中,聚合模块的pom.xml中通过modules->module来引用被聚合的模块,被聚合的模块是不用感知自己被聚合了,所以被聚合的模块中pom.xml中是不知道mall-aggregator的存在的。

继承

简介

开发的时候,我们很可能使用到重复的pom依赖,比如mall-pcmall-h5mall-api都需要使用到以下依赖:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.3</version>
</dependency>

3个项目中出现了同样的配置,一个配置出现了多份,作为开发者,我想你们和我一样,是无法接受的,必须要寻求一些方法来解决,需要将公共的部分提取出来共用。

maven也为我们考虑到了这种情况,maven中使用继承来解决这个问题。

继承实现步骤

步骤一

创建一个父maven构件,将依赖信息放在pom.xml中:

<dependencies>
   <dependency>依赖的构件的坐标信息</dependency>
   <dependency>依赖的构件的坐标信息</dependency>
   <dependency>依赖的构件的坐标信息</dependency>
</dependencies>

步骤二

将父构件的package元素的值置为pom:

<packaging>pom</packaging>

注意,上面讲的maven聚合,聚合模块也也是这个配置。

步骤三

在子构件的pom.xml引入父构件的配置:

<parent>
   <groupId>父构件groupId</groupId>
   <artifactId>父构件artifactId</artifactId>
   <version>父构件的版本号</version>
   <relativePath>父构件pom.xml路径</relativePath>
</parent>

relativePath表示父构件pom.xml相对路径,默认是../pom.xml,所以一般情况下父子结构的maven构件在目录结构上一般也采用父子关系。

案例

先创建一个父项目mall-parent,坐标信息如下:

<groupId>com.theboyaply</groupId>
<artifactId>mall-parent</artifactId>
<version>1.0-SNAPSHOT</version>

创建方式和上面聚合案例的一样,完成如下图:

maven07-24

删除掉src目录。

然后创建mall-pc模块(和聚合案例一样,创建的是模块,而不是项目):

idea选中mall-parent:(这里直接使用聚合案例的图片)

maven07-12

然后直接右键->New->Module或者点击File->New->Module都可:(这里直接使用聚合案例的图片)

maven07-13

点击选择Module后,如图:(这里直接使用聚合案例的图片)

maven07-14

点击Next,如图:(这里开始就不是聚合案例的图片了)

maven07-25

从这里开始就和聚合案例不一样了,注意看。

点击第一个...按钮,这个按钮是设置聚合的,所以选中None,如下图:

maven07-26

maven07-27

输入构件信息,如下图:

maven07-28

点击Next,输入Module-name的值为mall-pc,如下图:

maven07-29

点击Finish完成创建,如图:(前面开始创建的时候说删除src目录,我忘记了,请忽略mall-parent下的src目录)

maven07-30

上图中,看一下mall-pc的pom.xml,多了个parent元素,并且这个pom.xml中构件的groupId、version都没有了,这个pom.xml继承了mall-parent/pom.xml中的内容,他们的groupId、version都是一样的,子构件可以从父pom.xml中继承这些内容,所以如果是一样的情况,可以不写。

我们再创建mall-h5mall-api两个模块,步骤参考mall-pc模块的创建过程。

最终项目如下图:

maven07-31

下面我们来看一下4个pom.xml的内容。

mall-parent/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.theboyaply</groupId>
    <artifactId>mall-parent</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

mall-pc/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">
    <parent>
        <artifactId>mall-parent</artifactId>
        <groupId>com.theboyaply</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mall-pc</artifactId>

</project>

mall-h5/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">
    <parent>
        <artifactId>mall-parent</artifactId>
        <groupId>com.theboyaply</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mall-h5</artifactId>
    
</project>

mall-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">
    <parent>
        <artifactId>mall-parent</artifactId>
        <groupId>com.theboyaply</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mall-api</artifactId>

</project>

我们在父构件mall-parent/pom.xml中加入下面的配置:

<packaging>pom</packaging>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.3</version>
    </dependency>
</dependencies>

使用mvn dependency:tree命令在父构件mall-parent/pom.xml所在目录查看一下mall-parent的依赖情况。

mvn dependency:tree 这个插件可以根据pom.xml的配置,列出构件的依赖树信息。

输出如下:

D:\myResource\maven\maven5\mall-parent>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.theboyaply:mall-parent >---------------------
[INFO] Building mall-parent 1.0-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mall-parent ---
[INFO] com.theboyaply:mall-parent:pom:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-web:jar:5.2.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.2.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-core:jar:5.2.1.RELEASE:compile
[INFO] |     \- org.springframework:spring-jcl:jar:5.2.1.RELEASE:compile
[INFO] \- org.mybatis:mybatis-spring:jar:2.0.3:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.599 s
[INFO] Finished at: 2020-08-05T00:28:35+08:00
[INFO] ------------------------------------------------------------------------

我们再来看看mall-pc的依赖情况:

D:\myResource\maven\maven5\mall-parent>cd mall-pc

D:\myResource\maven\maven5\mall-parent\mall-pc>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mall-pc ---
[INFO] com.theboyaply:mall-pc:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-web:jar:5.2.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.2.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-core:jar:5.2.1.RELEASE:compile
[INFO] |     \- org.springframework:spring-jcl:jar:5.2.1.RELEASE:compile
[INFO] \- org.mybatis:mybatis-spring:jar:2.0.3:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.205 s
[INFO] Finished at: 2020-08-05T00:30:19+08:00
[INFO] ------------------------------------------------------------------------

输出信息中的依赖和父构件的依赖情况一样,说明mall-pc继承了mall-parent的依赖。

剩下的mall-h5mall-api的依赖情况也可以自己去验证下。

relativePath元素

这个在上面 继承实现步骤步骤三 提到过。

上面演示的父构件和子构件的目录结构刚好符合父子关系,如果父构件和子构件的目录不是父子关系,比如都位于同等级别的目录或者位于更复杂的目录的时候,此时我们需要在子pom.xmlparent元素中使用relativePath元素来指定父pom.xml相对路径位置,这个值我们上面没有指定,默认是../pom.xml,表示父pom.xml位于子pom.xml的上一级目录,我们的模块刚好符合这种关系,所以这个值省略了。

正确的设置relativePath是非常重要的,这个需要注意,子模块中执行mvn命令的时候,会去找父pom.xml的配置,会先通过relativePath指定的路径去找,如果找不到,会尝试通过坐标在本地仓库进行查找,如果本地找不到,会去远程仓库找,如果远程仓库也没有,会报错。

可继承的元素列表

上面我们看到了groupId、version、dependency中的依赖在子pom.xml中都没有写,这些都是从父pom.xml中继承过来的,还有很多元素也可以被继承过来,下面我们列个清单:

  • groupId:项目组ID,项目坐标的核心元素
  • version:项目版本,项目坐标的核心元素
  • dependency:项目依赖
  • description:项目的描述信息
  • organization:项目的组织信息
  • inceptionYear:项目的创始年份
  • url:项目的url地址
  • developers:项目的开发者信息
  • contributors:项目的贡献者信息
  • distributionManagement:项目的部署配置信息
  • issueManagement:项目的缺陷跟踪系统信息
  • ciManagement:项目的持续集成系统信息
  • scm:项目的版本控制系统信息
  • mailingLists:项目的邮件列表信息
  • properties:自定义的maven属性配置信息
  • dependencyManagement:项目的依赖管理配置
  • repositories:项目的仓库配置
  • build:包括项目的源码目录配置、输出目录配置、插件管理配置等信息
  • reporting:包括项目的报告输出目录配置、报告插件配置等信息

依赖管理(dependencyManagement)

大家是否发现了,上面的继承存在的一个问题,如果我在新增一个子构件,都会默认从父构件中继承依赖的一批构建,父pom.xml中配置的这些依赖的构建可能是其他项目不需要的,可能某个子项目只是想使用其中一个构件,但是上面的继承关系却把所有的依赖都给传递到子构件中了,这种显然是不合适的。

maven中也考虑到了这种情况,可以使用dependencyManagement元素来解决这个问题。

maven提供的dependencyManagement元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性。

在dependencyManagement元素下声明的依赖不会引入实际的依赖,他只是声明了这些依赖,不过它可以对dependencies中使用的依赖起到一些约束作用。

修改mall-parent/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.theboyaply</groupId>
    <artifactId>mall-parent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.2.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>2.0.3</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

可以看到我们将dependencies配置移到dependencyManagement下面了。

我们使用下面命令看一下项目的依赖情况:

D:\myResource\maven\maven5\mall-parent>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.theboyaply:mall-parent >---------------------
[INFO] Building mall-parent 1.0-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mall-parent ---
[INFO] com.theboyaply:mall-parent:pom:1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.443 s
[INFO] Finished at: 2020-08-05T00:44:20+08:00
[INFO] ------------------------------------------------------------------------

D:\myResource\maven\maven5\mall-parent>cd mall-pc

D:\myResource\maven\maven5\mall-parent\mall-pc>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mall-pc ---
[INFO] com.theboyaply:mall-pc:jar:1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.531 s
[INFO] Finished at: 2020-08-05T00:44:46+08:00
[INFO] ------------------------------------------------------------------------

父子构件中都看不到依赖的jar包了,说明父pom.xml中dependencyManagement这些依赖的构建没有被子模块依赖进去。

子模块如果想用到这些配置,可以dependencies进行引用,引用之后,依赖才会真正的起效。

在3个子模块的pom.xml中加入下面配置:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
    </dependency>
</dependencies>

注意这里的dependency没有指定版本。

再次运行上面的命令查看依赖信息:

D:\myResource\maven\maven5\mall-parent>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.theboyaply:mall-parent >---------------------
[INFO] Building mall-parent 1.0-SNAPSHOT
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mall-parent ---
[INFO] com.theboyaply:mall-parent:pom:1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.444 s
[INFO] Finished at: 2020-08-05T00:47:32+08:00
[INFO] ------------------------------------------------------------------------

D:\myResource\maven\maven5\mall-parent>cd mall-pc

D:\myResource\maven\maven5\mall-parent\mall-pc>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mall-pc ---
[INFO] com.theboyaply:mall-pc:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-web:jar:5.2.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.2.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-core:jar:5.2.1.RELEASE:compile
[INFO] |     \- org.springframework:spring-jcl:jar:5.2.1.RELEASE:compile
[INFO] \- org.mybatis:mybatis-spring:jar:2.0.3:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.513 s
[INFO] Finished at: 2020-08-05T00:47:42+08:00
[INFO] ------------------------------------------------------------------------

可以看到,mall-parent构件中没有列出依赖信息,而mall-pc列出了依赖信息。

看下mall-pc/pom.xml的截图:

maven07-32

上面的红框中的依赖构件没有version,左边2个红圈中的2个向上的箭头,表示这个是从父pom.xml中传递过来的,所以version可以省略。

左边的向上箭头可以点过去,传送到父pom.xml中。

再看一下父pom.xml截图效果:

maven07-33

dependencyManagement中定义了依赖的构建,2个向下的箭头表示被子模块有继承。

向下箭头同样可以点过去,传送到子模块pom.xml中。

dependencyManagement不会引入实际的依赖,只有在子类中使用dependency来引入父dependencyManagement声明的依赖之后,依赖的构建才会被真正的引入。

使用dependencyManagement来解决继承的问题,子pom.xml中只用写groupId,artifactId就可以了,其他信息都会从父dependencyManagement中声明的依赖关系中传递过来,通常我们使用这种方式将所有依赖的构建在父pom.xml中定义好,子构件中只需要通过groupId,artifactId就可以引入依赖的构建,而不需要写version,可以很好的确保多个子项目中依赖构件的版本的一致性,对应依赖构件版本的升级也非常方便,只需要在父pom.xml中修改一下就可以了。

单继承问题

上面讲解了dependencyManagement的使用,但是有个问题,只有使用继承的时候,dependencyManagement中声明的依赖才可能被子pom.xml用到,如果我的项目本来就有父pom.xml了,但是我现在想使用另外一个项目dependencyManagement中声明的依赖,此时我们怎么办?这就是单继承的问题。

这种情况在spring-boot、spring-cloud中会遇到,所以大家需要注意,这块一定需要玩转。

当我们想在项目中使用另外一个构件中dependencyManagement声明的依赖,而又不想继承这个项目的时候,可以在我们的项目中使用加入下面配置:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.theboyaply</groupId>
            <artifactId>mall-parent</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>构件2</dependency>
        <dependency>构件3</dependency>
        <dependency>构件n</dependency>
    </dependencies>
</dependencyManagement>

上面这个配置会将mall-parent构件中dependencyManagement元素中声明的所有依赖导入到当前pom.xml的dependencyManagement中。

相当于把下面部分的内容:

<dependency>
    <groupId>com.theboyaply</groupId>
    <artifactId>mall-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

替换成了mall-parent/pom.xmldependencyManagement元素内容,替换之后变成:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.3</version>
</dependency>

注意:这个配置既可以在父pom.xml中添加,也可以在子模块pom.xml中添加。在父pom.xml中添加,所有子模块都可以引用;在子模块pom.xml中添加,仅当前子模块可用

案例

创建项目mall-pom-import,创建方式参照mall-parent

坐标信息如下:

<groupId>com.theboyaply</groupId>
<artifactId>mall-pom-import</artifactId>
<version>1.0-SNAPSHOT</version>

创建完成后如下图:

maven07-34

修改pom.xml文件,在dependencyManagement中声明2个依赖:fastjson和junit。

内容如下:

<?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.theboyaply</groupId>
    <artifactId>mall-pom-import</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.62</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

mall-pom-import/pom.xml所在目录执行mvn install命令将其安装到本地仓库。

D:\myResource\maven\maven5\mall-pom-import>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.theboyaply:mall-pom-import >-------------------
[INFO] Building mall-pom-import 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mall-pom-import ---
[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) @ mall-pom-import ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mall-pom-import ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven5\mall-pom-import\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mall-pom-import ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mall-pom-import ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mall-pom-import ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-pom-import\target\mall-pom-import-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mall-pom-import ---
[INFO] Installing D:\myResource\maven\maven5\mall-pom-import\target\mall-pom-import-1.0-SNAPSHOT.jar to D:\apache\.m2\repository\com\theboyaply\mall-pom-import\1.0-SNAPSHOT\mall-pom-import-1.0-SNAPSHOT.jar
[INFO] Installing D:\myResource\maven\maven5\mall-pom-import\pom.xml to D:\apache\.m2\repository\com\theboyaply\mall-pom-import\1.0-SNAPSHOT\mall-pom-import-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.614 s
[INFO] Finished at: 2020-08-05T01:11:59+08:00
[INFO] ------------------------------------------------------------------------

现在我们想在mall-pc/pom.xml中使用mall-pom-import/pom.xmldependencyManagement元素中定义的fastjsonjunit的依赖。

而我们的mall-pc已经继承了mall-parent了,maven中只能单继承,所以没法通过继承的方式来实现了,那么我们可以在mall-pc/pom.xml中加入下面配置:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.theboyaply</groupId>
            <artifactId>mall-pom-import</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

此时可以使用fastjson的依赖了,mall-pc/pom.xmlproject->dependencies元素中加入下面配置:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
</dependency>

mall-pc/pom.xml如图所示:

maven07-35

可以看到,我们添加了dependencyManagement元素引入了mall-pom-import,并且mall-pc/pom.xml中的fastjson依赖没有指定版本,左边出现向上箭头。说明mall-pom-import传递过来了。

我们可以点击左边的向上箭头,会跳转到本地仓库的mall-pom-import-1.0-SNAPSHOT.pom文件中:

maven07-36

查看mall-pc的依赖信息:

D:\myResource\maven\maven5\mall-parent\mall-pc>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ mall-pc ---
[INFO] com.theboyaply:mall-pc:jar:1.0-SNAPSHOT
[INFO] +- org.springframework:spring-web:jar:5.2.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.2.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-core:jar:5.2.1.RELEASE:compile
[INFO] |     \- org.springframework:spring-jcl:jar:5.2.1.RELEASE:compile
[INFO] +- org.mybatis:mybatis-spring:jar:2.0.3:compile
[INFO] \- com.alibaba:fastjson:jar:1.2.62:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.653 s
[INFO] Finished at: 2020-08-05T01:24:15+08:00
[INFO] ------------------------------------------------------------------------

可以看到,fastjson确实被依赖过来了。

插件管理(pluginManagement)

maven中提供了dependencyManagement来解决继承的问题,同样也提供了解决插件继承问题的pluginManagement元素,在父pom中可以在这个元素中声明插件的配置信息,但是子pom.xml中不会引入此插件的配置信息,只有在子pom.xml中使用plugins->plugin元素正在引入这些声明的插件的时候,插件才会起效,子插件中只需要写groupId、artifactId,其他信息都可以从父构件中传递过来。

案例

mall-parent/pom.xml中添加如下配置:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>attach-source</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

maven-source-plugin这个插件在上一章有介绍过,源码打包的。

上面是将插件maven-source-pluginjar-no-fork目标绑定在default生命周期的verify阶段,verify阶段会在default生命周期的install周期前面执行。

下面我们看效果。

mall-pc/pom.xml所在目录运行命令:

D:\myResource\maven\maven5\mall-parent\mall-pc>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mall-pc ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mall-pc ---
[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) @ mall-pc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mall-pc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven5\mall-parent\mall-pc\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mall-pc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mall-pc ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mall-pc ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mall-pc ---
[INFO] Installing D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT.jar to D:\apache\.m2\repository\com\theboyaply\mall-pc\1.0-SNAPSHOT\mall-pc-1.0-SNAPSHOT.jar
[INFO] Installing D:\myResource\maven\maven5\mall-parent\mall-pc\pom.xml to D:\apache\.m2\repository\com\theboyaply\mall-pc\1.0-SNAPSHOT\mall-pc-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.810 s
[INFO] Finished at: 2020-08-05T01:34:39+08:00
[INFO] ------------------------------------------------------------------------

mvn clean install 会清理代码,然后将打包构件,将构建安装到本地仓库,从输入中可以看到mall-pc-1.0-SNAPSHOT.jar被安装到本地仓库了。

但是没有看到打包源码的插件的运行,说明了mall-pc没有从父pom.xml中继承插件的配置信息,所以插件配置没有起效,现在我们要让插件起效。

mall-pc/pom.xml中加入下面配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如下图:

maven07-37

又一个线上的箭头,说明这个是从父pom.xml中传递过来了,大家仔细看一下上面的配置,插件只写了groupId、artifactId,其他信息都没有写,其他信息都可以从父pom.xml中传递过来,下面我们看一下插件是否起效了,运行下面命令,见证奇迹:

D:\myResource\maven\maven5\mall-parent\mall-pc>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mall-pc ---
[INFO] Deleting D:\myResource\maven\maven5\mall-parent\mall-pc\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mall-pc ---
[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) @ mall-pc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mall-pc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven5\mall-parent\mall-pc\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mall-pc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mall-pc ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mall-pc ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-source-plugin:3.2.0:jar-no-fork (attach-source) @ mall-pc ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT-sources.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mall-pc ---
[INFO] Installing D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT.jar to D:\apache\.m2\repository\com\theboyaply\mall-pc\1.0-SNAPSHOT\mall-pc-1.0-SNAPSHOT.jar
[INFO] Installing D:\myResource\maven\maven5\mall-parent\mall-pc\pom.xml to D:\apache\.m2\repository\com\theboyaply\mall-pc\1.0-SNAPSHOT\mall-pc-1.0-SNAPSHOT.pom
[INFO] Installing D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT-sources.jar to D:\apache\.m2\repository\com\theboyaply\mall-pc\1.0-SNAPSHOT\mall-pc-1.0-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.356 s
[INFO] Finished at: 2020-08-05T01:38:09+08:00
[INFO] ------------------------------------------------------------------------

注意上面的输出中有attach-source,这个就是上面我们插件任务的id,输出中还有mall-pc-1.0-SNAPSHOT-sources.jar信息,说明源码也是打包成功了,最后也是上传到了本地仓库了。

1. 子模块如果引用父模块的配置信息,并修改了版本等,会覆盖父模块的配置

2. 子模块如果添加了自己的配置信息,会与继承自父模块的配置叠加

比如,我们修改mall-pc/pom.xml,配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-source</id>
                    <goals>
                        <goal>help</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

再看一下效果:

D:\myResource\maven\maven5\mall-parent\mall-pc>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:mall-pc >-----------------------
[INFO] Building mall-pc 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mall-pc ---
[INFO] Deleting D:\myResource\maven\maven5\mall-parent\mall-pc\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mall-pc ---
[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) @ mall-pc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mall-pc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven5\mall-parent\mall-pc\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mall-pc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mall-pc ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mall-pc ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-source-plugin:3.2.0:help (attach-source) @ mall-pc ---
[INFO] Apache Maven Source Plugin 3.2.0
  The Maven Source Plugin creates a JAR archive of the source files of the
  current project.

This plugin has 7 goals:

source:aggregate
  Aggregate sources for all modules in an aggregator project.

source:generated-test-jar
  This plugin bundles all the test sources into a jar archive.

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

source:jar
  This plugin bundles all the sources into a jar archive.

source:jar-no-fork
  This goal bundles all the sources into a jar archive. This goal functions the
  same as the jar goal but does not fork the build and is suitable for attaching
  to the build lifecycle.

source:test-jar
  This plugin bundles all the test sources into a jar archive.

source:test-jar-no-fork
  This goal bundles all the test sources into a jar archive. This goal functions
  the same as the test-jar goal but does not fork the build, and is suitable for
  attaching to the build lifecycle.


[INFO]
[INFO] --- maven-source-plugin:3.2.0:jar-no-fork (attach-source) @ mall-pc ---
[INFO] Building jar: D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT-sources.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mall-pc ---
[INFO] Installing D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT.jar to D:\apache\.m2\repository\com\theboyaply\mall-pc\1.0-SNAPSHOT\mall-pc-1.0-SNAPSHOT.jar
[INFO] Installing D:\myResource\maven\maven5\mall-parent\mall-pc\pom.xml to D:\apache\.m2\repository\com\theboyaply\mall-pc\1.0-SNAPSHOT\mall-pc-1.0-SNAPSHOT.pom
[INFO] Installing D:\myResource\maven\maven5\mall-parent\mall-pc\target\mall-pc-1.0-SNAPSHOT-sources.jar to D:\apache\.m2\repository\com\theboyaply\mall-pc\1.0-SNAPSHOT\mall-pc-1.0-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.795 s
[INFO] Finished at: 2020-08-05T01:41:52+08:00
[INFO] ------------------------------------------------------------------------

输出中有下面2行:

[INFO] --- maven-source-plugin:3.2.0:help (attach-source) @ mall-pc ---
[INFO] --- maven-source-plugin:3.2.0:jar-no-fork (attach-source) @ mall-pc ---

说明maven-source-plugin插件执行了2个目标:helpjar-no-fork。此时父子pom.xml中插件配置信息合并了,所以出现了2个目标。具体最终mall-pc/pom.xml中的内容是什么样的,可以通过下面这个命令看到:

mvn help:effective-pom

上面这个命令大家最好去看一下效果,当pom.xml中存在复杂的关系的时候,可以通过这个命令解析得到这个构件最终pom.xml的内容。

聚合与继承的关系

前面已经详解了聚合和继承,想必大家对这块也有了自己的理解。

聚合主要是为了方便多模块快速构建。

而继承主要是为了重用相同的配置。

对于聚合来说,聚合模块是知道被聚合模块的存在的,而被聚合模块是感知不到聚合模块的存在。

对于继承来说,父构件是感知不到子构件的存在,而子构件需要使用parent来引用父构件。

两者的共同点是,聚合模块和继承中的父模块的package属性都必须是pom类型的,同时,聚合模块和父模块中的除了pom.xml,一般都是没有什么内容的。

实际使用是,我们经常将聚合和继承一起使用,能同时使用到两者的优点。

-- end --


评论