Maven依赖关系原则与冲突解决办法(简要版)

Maven依赖关系原则(概念):
相关概念:

级别:

        我们不妨把pom.xml里面的引用的依赖视为第一级,那么依赖中各自引用的依赖视为第二级...第三级...

路径长短:

        我们不妨以从pom.xml出发,到抵达该依赖时经过的“级”的个数来作为路径长短的标准。

最短路径(不同级引用)原则:
若两个版本不同的同一依赖,在不同的级别中,那么优先使用路径最短的那一个,如:

同级(第一级遵循最后、其它级遵循最先)引用原则:

同级别的引用,分两种:
       两个不同版本的同一依赖,所在的级别是第一级;两个不同版本的同一依赖,所在的级别不是第一级。

  • 在第一级,谁后声明,使用谁:

  • 不在第一级,谁先声明,使用谁:

Maven(依赖)冲突解决办法:

第一种:使用【exclusions】排除不需要的依赖。

第二种:使用【dependencyManagement】指定使用哪一个版本的依赖。

注:dependencyManagement的作用有两个:一是指定使用某个依赖的哪一个版本。二是统一管理版本,
        在dependencyManagement指定版本号后,在与dependencyManagement同级的
        dependencies标签里面,引入依赖时,就可以不指定版本号了。

Dependency Management

        Besides inheriting certain top-level elements, parents have elements to configure values for child POMs and transitive dependencies. One of those elements is dependencyManagement.

        dependencyManagement: is used by a POM to help manage dependency information across all of its children. If the my-parent project uses dependencyManagement to define a dependency on junit:junit:4.12, then POMs inheriting from this one can set their dependency giving the groupId=junit and artifactId=junit only and Maven will fill in the version set by the parent. The benefits of this method are obvious. Dependency details can be set in one central location, which propagates to all inheriting POMs.

        Note that the version and scope of artifacts which are incorporated from transitive dependencies are also controlled by version specifications in a dependency management section. This can lead to unexpected consequences. Consider a case in which your project uses two dependences, dep1 and dep2. dep2 in turn also uses dep1, and requires a particular minimum version to function. If you then use dependencyManagement to specify an older version, dep2 will be forced to use the older version, and fail. So, you must be careful to check the entire dependency tree to avoid this problem; mvn dependency:tree is helpful.

第三种(必杀):直接在jar包中删除冲突了的(多余的那个依赖)对应的package包。
        第一种第二种方式就能解决所有maven管辖范围内的依赖冲突了。大部分难以解决的冲突,几乎都是由于引入了jar包而引起的。这里介绍如何解决jar包引起的冲突:使用解压工具(如rar)直接删除jar包里面的不需要的依赖对应的包。

场景举例说明:

       我最近使用使用到了某考勤厂商的sdk依赖jar包。该jar包有点特殊,是由gradle构建的,而且貌似构建得不规范;本人将其引入SpringBoot项目后,发生冲突了,需要将该jar包里面的低版本的阿里云Oss去掉;第一种第二种方式都试了,再网上找了一圈也没找到有效的解决办法,然后回头一想:只要当前环境中存在该jar包需要的依赖就可以了,至于在不在该jar里面就无所谓了,所以我直接使用rar删除了该jar包里面不要的阿里云Oss包,然后重新将jar包导入项目,冲突就解决了。

点击删除保存后,关闭再次打卡jar包发现aliyun对应的包已经没有了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值