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

时间:2025-03-09 07:29:24

一、依赖冲突产生的原因

调用的某个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: /nexus/content/groups/public/org/apache/maven/shared/maven-invoker/2.0.11/maven-invoker-2.0. (29 kB at 12 kB/s)
Downloaded from aliyun: /nexus/content/groups/public/commons-lang/commons-lang/2.6/commons-lang-2. (284 kB at 115 kB/s)
[INFO] :oa-organ:jar:1.0.0-SNAPSHOT
[INFO] +- :spring-core:jar:3.2.:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] +- :spring-webmvc:jar:3.2.:compile
[INFO] |  +- :spring-beans:jar:3.2.:compile
[INFO] |  \- :spring-expression:jar:3.2.:compile
[INFO] +- :spring-context:jar:3.2.:compile
[INFO] +- :spring-context-support:jar:3.2.:compile
[INFO] +- :spring-aop:jar:3.2.:compile
[INFO] |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- :spring-aspects:jar:3.2.:compile
[INFO] |  \- :aspectjweaver:jar:1.7.4:compile
[INFO] +- :spring-tx:jar:3.2.:compile
[INFO] +- :spring-jdbc:jar:3.2.:compile
[INFO] +- :spring-web:jar:3.2.:compile
[INFO] +- junit:junit:jar:4.13.2:test
[INFO] |  \- :hamcrest-core:jar:1.3:test
[INFO] +- :spring-test:jar:3.2.: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] +- :mybatis:jar:3.2.1:compile
[INFO] +- :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 修改项目中的文件

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

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

<!-- spring依赖 -->
          <dependency>  
            <groupId></groupId>  
            <artifactId>spring-core</artifactId>  
            <version>3.2.</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] ------------------------< :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] :oa-organ:jar:1.0.0-SNAPSHOT
[INFO] +- :spring-core:jar:3.2.:compile
[INFO] +- :spring-webmvc:jar:3.2.:compile
[INFO] |  +- :spring-beans:jar:3.2.:compile
[INFO] |  \- :spring-expression:jar:3.2.:compile
[INFO] +- :spring-context:jar:3.2.:compile
[INFO] +- :spring-context-support:jar:3.2.:compile
[INFO] +- :spring-aop:jar:3.2.:compile
[INFO] |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- :spring-aspects:jar:3.2.:compile
[INFO] |  \- :aspectjweaver:jar:1.7.4:compile
[INFO] +- :spring-tx:jar:3.2.:compile
[INFO] +- :spring-jdbc:jar:3.2.:compile
[INFO] +- :spring-web:jar:3.2.:compile
[INFO] +- junit:junit:jar:4.13.2:test
[INFO] |  \- :hamcrest-core:jar:1.3:test
[INFO] +- :spring-test:jar:3.2.: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] +- :mybatis:jar:3.2.1:compile
[INFO] +- :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] +- :spring-core:jar:3.2.:compile

下面的这一行

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

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

实验成功

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