Maven中dependencyManagement标签作用

Maven中dependencyManagement标签作用

  1. 在该元素下声明的依赖不会实际引入到模块中,只有在 dependencies 元素下同样声明了该依赖,才会引入到模块中。该元素能够约束 dependencies 下依赖的使用
  2. 即 dependencies 声明的依赖若未指定版本,则使用 dependencyManagement 中指定的版本,否则将覆盖 dependencyManager 中的版本
  3. dependencyManager 可以传递给子模块,所以在子模块中可以引用父模块 dependencyManagement 定义好的依赖

举例

使用 dependencyManagement 声明依赖

    <!--在dependencyManagement 声明的依赖不会实际引入到模块中 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.7.5</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

如果需要使用该依赖,则应该在 dependencies 中重新声明该依赖

    <!--如果需要使用,在dependencies中声明该依赖,无须指定版本-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

 

如果是在子类中,需要先引入父模块,再引用依赖

    <!--引入父模块, parent标签可以理解为java中的继承关系-->
    <parent>
        <groupId>父类 groupId</groupId>
        <artifactId>父类 artifactId</artifactId>
        <version>父类 version</version>
    </parent>


    <!--如果需要使用,在dependencies中声明该依赖,无须指定版本-->
    <dependencies>
        <dependency>
            <!--如果指定了版本号,则会覆盖父模块 dependencyManagement 定义的版本(不建议这                
                做,dependencyManagement的作用本来就是用来统一版本,防止依赖冲突)-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

总结

使用 dependencyManagement 可以统一声明依赖版本,进行集中管理,避免依赖冲突

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值