美文网首页
maven继承的使用-05

maven继承的使用-05

作者: 誓俭草 | 来源:发表于2019-08-28 22:30 被阅读0次

maven相关知识点-05

maven的继承

为什么需要继承机制?
由于非 compile 范围的依赖信息是不能在“依赖链”中传递的,所以有需要的工程只能单独配置。例如:

模块 依赖
Hello <dependency>

<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency> |
| HelloFriend | <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version><scope>test</scope>
</dependency> |
| MakeFriend | <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency> |

此时如果项目需要将各个模块的junit版本统一为4.9,那么到各个工程中手动修改无疑是非常不可取的。
使用继承机制就可以将这样的依赖信息统一提取到父工程模块中进行统一管理。

创建父工程

创建父工程和创建一般的 Java 工程操作一致,唯一需要注意的是:打包方式处要设置为 pom。

在子工程中引用父工程

<parent>
<!-- 父工程坐标 -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<relativePath>从当前目录到父项目的 pom.xml 文件的相对路径</relativePath>
</parent>

此时如果子工程的 groupId 和 version 如果和父工程重复则可以删除。

在父工程中管理依赖

将 Parent 项目中的 dependencies 标签,用 dependencyManagement 标签括起来

<dependencyManagement>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
</dependencies>
</dependencyManagement>

在子项目中重新指定需要的依赖,删除范围和版本号

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

相关文章

网友评论

      本文标题:maven继承的使用-05

      本文链接:https://www.haomeiwen.com/subject/jhpvectx.html