Maven实战-第十七篇(Maven中的依赖管理)

Maven实战-第十七篇(Maven中的依赖管理)

通过上一篇的介绍我们知道在Maven中有很多标签都是可以被继承的。其中dependencies标签就是当中的一个。并且我们也知道dependencies标签是配置项目的依赖的。那我们是不是可以在父pom中配置项目中的依赖,然后让子模块继承呢?这样就解决了重复配置依赖的问题了。我们的子项目就不需要配置依赖信息了。下面我们按照上面说的验证一下。我们在父项目中引入依赖,然后看子项目是否能自动继承这个依赖。

  • 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>cn.ma-zhe</groupId>
    <artifactId>midai-mazhe-maven</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>mazhe-maven-mall</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
  • 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>midai-mazhe-maven</artifactId>
        <groupId>cn.ma-zhe</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mazhe-maven-mall</artifactId>

</project>

我们之前的内容中分享了,如何在项目中通过Maven命令的方式查看项目依赖,下面我们使用一下。查询的命令有很多,我们这次使用mvn dependency:tree命令查看。首先我们先查看一下父项目的依赖情况。

  • midai-mazhe-maven
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] midai-mazhe-maven                                                  [pom]
[INFO] mazhe-maven-mall                                                   [jar]
[INFO] 
[INFO] --------------------< cn.ma-zhe:midai-mazhe-maven >---------------------
[INFO] Building midai-mazhe-maven 1.0-SNAPSHOT                            [1/2]
[INFO]   from pom.xml
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ midai-mazhe-maven ---
[INFO] cn.ma-zhe:midai-mazhe-maven:pom:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:4.12:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT                             [2/2]
[INFO]   from mazhe-maven-mall/pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ mazhe-maven-mall ---
[INFO] cn.ma-zhe:mazhe-maven-mall:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:4.12:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for midai-mazhe-maven 1.0-SNAPSHOT:
[INFO] 
[INFO] midai-mazhe-maven .................................. SUCCESS [  1.771 s]
[INFO] mazhe-maven-mall ................................... SUCCESS [  0.121 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.298 s
[INFO] Finished at: 2023-08-09T06:02:02+08:00
[INFO] ------------------------------------------------------------------------

看上面输出的日志,我们看父项目中成功的引入了junit依赖了。下面我们用同样的方式,看一下子项目的依赖情况。

  • mazhe-maven-mall
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ mazhe-maven-mall ---
[INFO] cn.ma-zhe:mazhe-maven-mall:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:4.12:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.132 s
[INFO] Finished at: 2023-08-09T06:05:59+08:00
[INFO] ------------------------------------------------------------------------

我们看子模块输出的日志信息,发现也成功的引入了junit依赖。这也就是Maven提供的继承的功能。


但通过上面的验证,我们是否发现一个问题。如果父模块配置的依赖子模块不一定都会使用父模块的依赖,当有的子模块并不需要依赖这多么依赖时,但因为继承的原因就会导致子模块引入很多无用的依赖,但实际上子模块并不需要这些依赖,然后还需要添加额外的配置排除依赖,这样反而造成了麻烦。所以在Maven中配置依赖的时候,如果确定并不是所有的子模块都使用的依赖,我们可以不使用dependencies标签进行依赖的配置。Maven中提供了dependencyManagement标签,通过它依然可以控制子模块的依赖,但此标签和dependencies标签不同,它并不会实际引入依赖。而是如果子模块需要引入父模块的依赖时,可以按照正常的依赖方式引入,但是可以对部分参数进行忽略。下面我们看一下dependencyManagement标签具体的配置:

  • pom.xml(父项目)
 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</dependencyManagement>

我们同样按照上面的方式看一下,通过<dependencyManagement>标签配置的依赖,子项目是否能成功的继承。

  • 父项目日志
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] midai-mazhe-maven                                                  [pom]
[INFO] mazhe-maven-mall                                                   [jar]
[INFO] 
[INFO] --------------------< cn.ma-zhe:midai-mazhe-maven >---------------------
[INFO] Building midai-mazhe-maven 1.0-SNAPSHOT                            [1/2]
[INFO]   from pom.xml
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ midai-mazhe-maven ---
[INFO] cn.ma-zhe:midai-mazhe-maven:pom:1.0-SNAPSHOT
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT                             [2/2]
[INFO]   from mazhe-maven-mall/pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ mazhe-maven-mall ---
[INFO] cn.ma-zhe:mazhe-maven-mall:jar:1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for midai-mazhe-maven 1.0-SNAPSHOT:
[INFO] 
[INFO] midai-mazhe-maven .................................. SUCCESS [  1.631 s]
[INFO] mazhe-maven-mall ................................... SUCCESS [  0.108 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.147 s
[INFO] Finished at: 2023-08-09T06:34:02+08:00
[INFO] ------------------------------------------------------------------------

我们看父项目日志的输出,发现虽然我们配置了依赖,但是父项目并没有引入这个依赖。这个就是<dependencyManagement>标签的作用,它并不会在项目中直接引入依赖。下面我们验证一下子项目是否成引入这个依赖。

  • 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>midai-mazhe-maven</artifactId>
        <groupId>cn.ma-zhe</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mazhe-maven-mall</artifactId>

</project>

同样输入查看依赖的命令,看子项目是否能成功的引入这个依赖。

  • 子项目日志
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ mazhe-maven-mall ---
[INFO] cn.ma-zhe:mazhe-maven-mall:jar:1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.187 s
[INFO] Finished at: 2023-08-09T06:43:46+08:00
[INFO] ------------------------------------------------------------------------

我们通过日志发现通过<dependencyManagement>标签配置的依赖,父子项目都不会引入依赖。那么如何引入依赖呢?我们修改一下子项目的配置信息:

  • 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>midai-mazhe-maven</artifactId>
        <groupId>cn.ma-zhe</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mazhe-maven-mall</artifactId>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

</project>

我们再次执行一下查看依赖的命令,来看一下输出和上次是否会有不同。

  • 子项目日志
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ mazhe-maven-mall ---
[INFO] cn.ma-zhe:mazhe-maven-mall:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:4.12:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.134 s
[INFO] Finished at: 2023-08-09T06:46:38+08:00
[INFO] ------------------------------------------------------------------------

我们看上面的日志发现,虽然我们没有在子pom.xml中指定相应的版本,但它依然成功的引入该依赖,但实际上版本是Maven中必须要指定的参数,所以这就使用了Maven中继承的功能了,它继承了父项目中依赖的版本了。这也是<dependencyManagement>标的作用。当配置此标签时,并不会直接引入依赖,而是必须当我们子项目中指定引入具体的哪个依赖,才会引入,并且可以缺省相应的配置参数,也就是版本等信息。当然我们也可以指定其他的版本,这样就不会使用父类pom中指定的版本了,可以理解为子项目把父项目的配置覆盖了。

  • pom(子项目)
<?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>midai-mazhe-maven</artifactId>
        <groupId>cn.ma-zhe</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mazhe-maven-mall</artifactId>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
    </dependencies>

</project>

在查看一下依赖的信息。

  • 子项目日志
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ mazhe-maven-mall ---
[INFO] cn.ma-zhe:mazhe-maven-mall:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:4.13:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.120 s
[INFO] Finished at: 2023-08-09T06:53:47+08:00
[INFO] ------------------------------------------------------------------------

我们看日志的信息,子项目成功的把父项目的继承的信息给覆盖了,这就是Maven中<dependencyManagement>标签的使用。

给TA打赏
共{{data.count}}人
人已打赏
Maven

Maven实战-第十六篇(Maven中的继承)

2023-8-8 7:01:58

Maven

Maven实战-第十八篇(Maven中的默认约定)

2023-8-13 7:22:42