maven解决jar包冲突

本文介绍了如何使用Maven的`mvn dependency:tree`命令来查看项目的依赖树,以便于理解jar包间的依赖关系。同时,针对因重复依赖导致的错误,如阿里的jar包与jaxb-api和gson的冲突,展示了如何通过`exclusions`排除不需要的依赖,从而避免冲突并成功构建项目。
摘要由CSDN通过智能技术生成

1.查看maven依赖树

命令:mvn dependency:tree

在项目目录下执行mvn dependency:tree,可以比较直观看到jar包的依赖关系

E:\develop\web\project\travel>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< com.hzh:travel >---------------------------
[INFO] Building travel 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ travel ---
[INFO] com.hzh:travel:war:1.0-SNAPSHOT
[INFO] +- mysql:mysql-connector-java:jar:5.1.30:runtime
[INFO] +- org.mybatis:mybatis:jar:3.5.1:compile
[INFO] +- org.projectlombok:lombok:jar:1.16.20:compile
[INFO] +- commons-beanutils:commons-beanutils:jar:1.9.2:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  \- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] +- com.fasterxml.jackson.dataformat:jackson-dataformat-xml:jar:2.9.9:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.9.9:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.9:compile
[INFO] |  +- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.9.9:compile
[INFO] |  +- org.codehaus.woodstox:stax2-api:jar:3.1.4:compile
[INFO] |  \- com.fasterxml.woodstox:woodstox-core:jar:5.1.0:compile
[INFO] +- redis.clients:jedis:jar:2.9.0:compile
[INFO] |  \- org.apache.commons:commons-pool2:jar:2.4.2:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.322 s
[INFO] Finished at: 2020-12-15T22:23:23+08:00
[INFO] ------------------------------------------------------------------------

2.用exclusion排除重复依赖的jar包

因为我的项目用到阿里云服务jar包,可能在其他地方也用到了jaxb-api和gson包就报错了,报错如下:

15-Dec-2020 22:34:40.169 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.startup.ContextConfig.processAnnotationsJar Unable to process Jar entry [module-info.class] from Jar [file:/E:/develop/web/project/travel/target/travel-1.0-SNAPSHOT/WEB-INF/lib/jaxb-api-2.3.1.jar] for annotations
15-Dec-2020 22:34:40.279 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.startup.ContextConfig.processAnnotationsJar Unable to process Jar entry [module-info.class] from Jar [file:/E:/develop/web/project/travel/target/travel-1.0-SNAPSHOT/WEB-INF/lib/gson-2.8.6.jar] for annotations

通过依赖树找到对应的jar包,排除就不报错了

    <!-- 阿里云短信包 -->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.5.3</version>
      <exclusions>
        <exclusion>
          <groupId>javax.xml.bind</groupId>
          <artifactId>jaxb-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.google.code.gson</groupId>
          <artifactId>gson</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值