Java学习笔记之二:Maven中依赖使用exclusions设置,来解决依赖冲突

一、依赖冲突产生的原因

调用的某个A包依赖于B包,B又依赖于C 和D 但是C依赖于E的1.0版本,D依赖于E的2.0版本  1.0跟2.0冲突了。

常见解决办法:直接使用2.0版本,删除1.0的依赖

原因:正常开源是向下依赖的,也就是说新版本都是包含老版本内容的。所以一般都可以直接使用2.0,这样1.0的依赖就需要我们手工把他干掉,这样就不冲突啦

二、CDM命令行:

2.1查看依赖树

进入到项目路径,输入依赖树命令

E:\Develop\eclipse\workspace\oa-organ>mvn dependency:tree

返回如下:

...
Downloaded from aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-invoker/2.0.11/maven-invoker-2.0.11.jar (29 kB at 12 kB/s)
Downloaded from aliyun: http://maven.aliyun.com/nexus/content/groups/public/commons-lang/commons-lang/2.6/commons-lang-2.6.jar (284 kB at 115 kB/s)
[INFO] com.xxxx.oa:oa-organ:jar:1.0.0-SNAPSHOT
[INFO] +- org.springframework:spring-core:jar:3.2.8.RELEASE:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.2.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.2.8.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:3.2.8.RELEASE:compile
[INFO] |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework:spring-aspects:jar:3.2.8.RELEASE:compile
[INFO] |  \- org.aspectj:aspectjweaver:jar:1.7.4:compile
[INFO] +- org.springframework:spring-tx:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-web:jar:3.2.8.RELEASE:compile
[INFO] +- junit:junit:jar:4.13.2:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.springframework:spring-test:jar:3.2.8.RELEASE:test
[INFO] +- log4j:log4j:jar:1.2.12:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.6:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.6:compile
[INFO] +- org.mybatis:mybatis:jar:3.2.1:compile
[INFO] +- org.mybatis:mybatis-spring:jar:1.2.0:compile
[INFO] \- mysql:mysql-connector-java:jar:5.1.47:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  39.332 s
[INFO] Finished at: 2021-11-27T23:06:45+08:00
[INFO] ------------------------------------------------------------------------

2.2 修改项目中的pom.xml文件

在原有的依赖dependency中增加如下

下面<exclusions></exclusions>中间这一段是新增的

<!-- spring依赖 -->
          <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>3.2.8.RELEASE</version>  
                <exclusions>
                    <exclusion>
                      <groupId>commons-logging</groupId>
                      <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
        </dependency>  

2.3再次查看依赖树

E:\Develop\eclipse\workspace\oa-organ>mvn dependency:tree

返回如下

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< com.xxxx.oa:oa-organ >------------------------
[INFO] Building oa-organ 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ oa-organ ---
[WARNING] The artifact xml-apis:xml-apis:jar:2.0.2 has been relocated to xml-apis:xml-apis:jar:1.0.b2
[INFO] com.xxxx.oa:oa-organ:jar:1.0.0-SNAPSHOT
[INFO] +- org.springframework:spring-core:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.2.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.2.8.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:3.2.8.RELEASE:compile
[INFO] |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework:spring-aspects:jar:3.2.8.RELEASE:compile
[INFO] |  \- org.aspectj:aspectjweaver:jar:1.7.4:compile
[INFO] +- org.springframework:spring-tx:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:3.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-web:jar:3.2.8.RELEASE:compile
[INFO] +- junit:junit:jar:4.13.2:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.springframework:spring-test:jar:3.2.8.RELEASE:test
[INFO] +- log4j:log4j:jar:1.2.12:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.6:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.6:compile
[INFO] +- org.mybatis:mybatis:jar:3.2.1:compile
[INFO] +- org.mybatis:mybatis-spring:jar:1.2.0:compile
[INFO] \- mysql:mysql-connector-java:jar:5.1.47:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.938 s
[INFO] Finished at: 2021-11-27T23:36:43+08:00
[INFO] ------------------------------------------------------------------------

与上面对比

[INFO] +- org.springframework:spring-core:jar:3.2.8.RELEASE:compile

下面的这一行

[INFO] |  \- commons-logging:commons-logging:jar:1.1.3:compile

由于刚刚加了exclusions参数设置,导致commons-logging失效

实验成功

自训:学Java,每天进步一点点,年底达成初级。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值