一招解决以Maven构建的Java项目中jar包依赖错乱问题

日期作者版本备注
2022-10-05dingbinthu@163.comV1.0

实际工作中我们经常遇到以maven构建的java工程中遇到jar包依赖混乱问题,这时候经常要用到

mvn dependency:tree -Dverbose

mvn dependency:tree -Dverbose -Dincludes=groupId:artifactId:version -Dexcludes=groupId:artifactId:version

命令来查看jar错综依赖关系,并排查解决。

举例:

对我的一个java工程,我用命令:mvn clean install -U 编译时报错如下:

22-11-19 15:27:19,639 INFO  com.mchange.v2.log.MLog(MLog.java:80) ## MLog clients using log4j logging.
22-11-19 15:27:19,782 INFO  com.mchange.v2.c3p0.C3P0Registry(C3P0Registry.java:204) ## Initializing c3p0-0.9.1.1 [built 15-March-2007 01:32:31; debug? true; trace: 10]
22-11-19 15:27:19,832 WARN  com.mchange.v2.c3p0.DriverManagerDataSource(DriverManagerDataSource.java:108) ## Could not load driverClass com.mysql.cj.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at com.mchange.v2.c3p0.DriverManagerDataSource.ensureDriverLoaded(DriverManagerDataSource.java:101)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:133)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
    at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

显然这个maven构建提示找不到com.mysql.cj.jdbc.Driver 这个类(这个类是mysql8.0以及以后版本的 jdbc类),显示是在c3p0-0.9.1.1 这个jar包里调用的。查看我的pom.xml里c3p0相关的依赖配置如下:

        <!-- jdbc utils -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.7</version>
        </dependency>
        <!-- jdbc utils -->

看见我 明明配置了c3p0-0.9.5.5版本,但这里没起作用,还是用了c3p0-0.9.1.1 这个jar包版本。前者可以加载com.mysql.cj.jdbc.Driver,后者不行。

现在的问题是怎么找到c3p0-0.9.1.1来自哪个jar包呢?因为pom.xml中并未显示配置这个依赖。解决方案就是通过大杀器命令:mvn dependency:tree -Dverbose 。执行提示如下:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for qxsearch.net:rpt-parser:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 106, column 21
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 136, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building rpt-parser 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The artifact mysql:mysql-connector-java:jar:8.0.31 has been relocated to com.mysql:mysql-connector-j:jar:8.0.31
[WARNING] The artifact org.apache.commons:commons-io:jar:1.3.2 has been relocated to commons-io:commons-io:jar:1.3.2
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ rpt-parser ---
[WARNING] While downloading mysql:mysql-connector-java:8.0.31
  This artifact has been relocated to com.mysql:mysql-connector-j:8.0.31.
  MySQL Connector/J artifacts moved to reverse-DNS compliant Maven 2+ coordinates.


[WARNING] While downloading org.apache.commons:commons-io:1.3.2
  This artifact has been relocated to commons-io:commons-io:1.3.2.
  https://issues.sonatype.org/browse/MVNCENTRAL-244


[INFO] qxsearch.net:rpt-parser:jar:1.0-SNAPSHOT
[INFO] +- com.mysql:mysql-connector-j:jar:8.0.31:compile
[INFO] |  \- com.google.protobuf:protobuf-java:jar:3.19.4:compile
[INFO] +- com.mchange:c3p0:jar:0.9.5.5:compile
[INFO] |  \- com.mchange:mchange-commons-java:jar:0.2.19:compile
[INFO] +- commons-dbutils:commons-dbutils:jar:1.7:compile
[INFO] +- org.apache.turbine:turbine:jar:4.0-M2:compile
[INFO] |  +- org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.10:compile
[INFO] |  +- (commons-collections:commons-collections:jar:3.2.2:compile - omitted for duplicate)
[INFO] |  +- commons-configuration:commons-configuration:jar:1.10:compile
[INFO] |  |  +- (commons-lang:commons-lang:jar:2.6:compile - omitted for duplicate)
[INFO] |  |  \- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.2)
[INFO] |  +- org.apache.commons:commons-email:jar:1.4:compile
[INFO] |  |  +- com.sun.mail:javax.mail:jar:1.5.2:compile
[INFO] |  |  |  \- (javax.activation:activation:jar:1.1:compile - omitted for conflict with 1.1.1)
[INFO] |  |  \- javax.activation:activation:jar:1.1.1:compile
[INFO] |  +- (commons-io:commons-io:jar:2.4:compile - omitted for conflict with 1.3.2)
[INFO] |  +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.9.2:compile
[INFO] |  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.2)
[INFO] |  |  \- (commons-collections:commons-collections:jar:3.2.1:compile - omitted for conflict with 3.2.2)
[INFO] |  +- ecs:ecs:jar:1.4.2:compile
[INFO] |  +- org.apache.fulcrum:fulcrum-factory:jar:1.0.4:compile
[INFO] |  |  \- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  +- org.apache.fulcrum:fulcrum-pool:jar:1.0.4:compile
[INFO] |  |  +- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  |  \- (org.apache.fulcrum:fulcrum-factory:jar:1.0.4:compile - omitted for duplicate)
[INFO] |  +- org.apache.fulcrum:fulcrum-localization:jar:1.0.6:compile
[INFO] |  |  +- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  |  \- (commons-lang:commons-lang:jar:2.4:compile - omitted for conflict with 2.6)
[INFO] |  +- org.apache.fulcrum:fulcrum-parser:jar:1.0.3:compile
[INFO] |  |  +- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  |  +- (commons-lang:commons-lang:jar:2.6:compile - omitted for duplicate)
[INFO] |  |  +- (org.apache.fulcrum:fulcrum-pool:jar:1.0.4:compile - omitted for duplicate)
[INFO] |  |  \- (commons-io:commons-io:jar:2.4:runtime - omitted for duplicate)
[INFO] |  +- org.apache.fulcrum:fulcrum-intake:jar:1.2.0:compile
[INFO] |  |  +- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  |  +- (org.apache.fulcrum:fulcrum-parser:jar:1.0.3:compile - omitted for duplicate)
[INFO] |  |  +- (commons-lang:commons-lang:jar:2.6:compile - omitted for duplicate)
[INFO] |  |  \- org.apache.commons:commons-pool2:jar:2.3:compile
[INFO] |  +- org.apache.fulcrum:fulcrum-crypto:jar:1.0.7:compile
[INFO] |  |  \- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  +- org.apache.fulcrum:fulcrum-xslt:jar:1.1.0:compile
[INFO] |  |  \- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  +- org.apache.fulcrum:fulcrum-mimetype:jar:1.0.5:compile
[INFO] |  |  \- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  +- org.apache.torque:torque-runtime:jar:4.0:compile
[INFO] |  |  +- (commons-collections:commons-collections:jar:3.2.1:compile - omitted for conflict with 3.2.2)
[INFO] |  |  +- (commons-configuration:commons-configuration:jar:1.6:compile - omitted for conflict with 1.10)
[INFO] |  |  +- commons-dbcp:commons-dbcp:jar:1.3:compile
[INFO] |  |  |  \- (commons-pool:commons-pool:jar:1.5.4:compile - omitted for conflict with 1.5.6)
[INFO] |  |  +- (commons-lang:commons-lang:jar:2.6:compile - omitted for duplicate)
[INFO] |  |  +- (commons-logging:commons-logging:jar:1.1.1:compile - omitted for conflict with 1.2)
[INFO] |  |  +- commons-pool:commons-pool:jar:1.5.6:compile
[INFO] |  |  +- org.apache.jcs:jcs:jar:1.3:compile
[INFO] |  |  |  +- (commons-logging:commons-logging:jar:1.1:compile - omitted for conflict with 1.2)
[INFO] |  |  |  \- concurrent:concurrent:jar:1.0:compile
[INFO] |  |  \- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  +- org.apache.fulcrum:fulcrum-security-api:jar:1.1.0:compile
[INFO] |  |  +- (commons-lang:commons-lang:jar:2.5:compile - omitted for conflict with 2.6)
[INFO] |  |  +- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  |  \- (org.apache.fulcrum:fulcrum-crypto:jar:1.0.7:compile - omitted for duplicate)
[INFO] |  +- org.apache.fulcrum:fulcrum-quartz:jar:1.1.0:compile
[INFO] |  |  +- org.quartz-scheduler:quartz:jar:2.0.0:compile
[INFO] |  |  |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  |  |  +- c3p0:c3p0:jar:0.9.1.1:compile
[INFO] |  |  |  \- (org.slf4j:slf4j-api:jar:1.6.1:compile - omitted for conflict with 1.7.6)
[INFO] |  |  \- (org.apache.avalon.framework:avalon-framework-api:jar:4.3.1:compile - omitted for duplicate)
[INFO] |  +- log4j:log4j:jar:1.2.17:compile
[INFO] |  +- org.apache.velocity:velocity:jar:1.7:compile
[INFO] |  |  +- (commons-collections:commons-collections:jar:3.2.1:compile - omitted for conflict with 3.2.2)
[INFO] |  |  \- (commons-lang:commons-lang:jar:2.4:compile - omitted for conflict with 2.6)
[INFO] |  \- com.thoughtworks.xstream:xstream:jar:1.4.4:compile
[INFO] |     +- xmlpull:xmlpull:jar:1.1.3.1:compile
[INFO] |     \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] +- junit:junit:jar:4.12:compile
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] +- org.mockito:mockito-all:jar:1.10.19:test
[INFO] +- org.slf4j:slf4j-api:jar:1.7.6:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.6:compile
[INFO] |  +- (org.slf4j:slf4j-api:jar:1.7.6:compile - omitted for duplicate)
[INFO] |  \- (log4j:log4j:jar:1.2.17:compile - omitted for duplicate)
[INFO] +- org.apache.commons:commons-lang3:jar:3.1:compile
[INFO] +- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- commons-io:commons-io:jar:1.3.2:compile
[INFO] +- com.itextpdf:itextpdf:jar:5.5.13:compile
[INFO] +- org.apache.pdfbox:pdfbox:jar:2.0.13:compile
[INFO] |  +- (org.apache.pdfbox:fontbox:jar:2.0.13:compile - omitted for duplicate)
[INFO] |  \- (commons-logging:commons-logging:jar:1.2:compile - omitted for duplicate)
[INFO] +- org.apache.pdfbox:fontbox:jar:2.0.13:compile
[INFO] |  \- (commons-logging:commons-logging:jar:1.2:compile - omitted for duplicate)
[INFO] \- com.alibaba:fastjson:jar:1.2.54:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.019 s
[INFO] Finished at: 2022-11-19T15:45:51+08:00
[INFO] Final Memory: 11M/47M
[INFO] ------------------------------------------------------------------------

在以上文本中搜索c3p0关键词很容易发现c3p0-0.9.1.1 来自org.apache.turbine:turbine:jar:4.0-M2:compile 这个jar包,于是只需要在pom.xml中找到这个jar包的依赖配置,添加 exclusions即可,内容如下:

<dependency>
    <groupId>org.apache.turbine</groupId>
    <artifactId>turbine</artifactId>
    <version>4.0-M2</version>
    <exclusions>
        <exclusion>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
        </exclusion>
    </exclusions>
</dependency>

但是不幸的是你发现这样修改之后再次执行mvn dependency:tree -Dverbose 还是有c3p0-0.9.1.1的依赖,原因是exclusions 只会排除当前jar包的直接子模块。于是查询发现c3p0-0.9.1.1其实是在org.apache.turbine:turbine:jar:4.0-M2:compile的子模块:org.apache.fulcrum:fulcrum-quartz 中,于是修改exclusions如下:

<dependency>
    <groupId>org.apache.turbine</groupId>
    <artifactId>turbine</artifactId>
    <version>4.0-M2</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.fulcrum</groupId>
            <artifactId>fulcrum-quartz</artifactId>
        </exclusion>
    </exclusions>
</dependency>

再次执行mvn dependency:tree -Dverbose 就没有c3p0-0.9.1.1的依赖了,表明修改有效。

但是这时候如果在idea中直接运行你会发现还是会报同样的错误,原因是因为idea中依赖已经在工程被加载之初固化到Project Structure中Project Settings中的Dependencies中了。如下图所示。简单的解决方案就是删除掉该工程idea相关的配置.idea目录和xxx.iml文件。然后重新导入,编译,运行,一切就都成功了。

image-20221119160901118

0 条评论
请不要发布违法违规有害信息,如发现请及时举报或反馈
还没有人评论呢,速度抢占沙发!
相关文章
  • @[toc] 前言 所有博客文件目录索引:博客目录索引(持续更新) package打包出现类 xxx位置:程序包 xxx.xxx 问题描述:明明程序能够正常运行跑,为什么打包单独一...

  • 1. IDEA配置Maven环境 先在IDEA中配置Maven环境: 选择 IDEA中 File --> Settings 搜索 maven 设置 IDEA 使用本地安装的 Maven,...

  • 由char和byte的关系引申出去——总结一下java中的字符编码相关知识 一、字符编码 手持两把锟斤拷,口中直呼烫烫烫   在文章伊始,先来复习一下计算机中关于编码的一些基础知识,着重理清以...

  • 1.1 Java概述 如何快速学习Java技术 graph TD A(需求: 1.工作需要 2.跳槽,对方要求 3.技术控)-->B(看看是否使用传统技术解决: 1.能解决,但是不完美 2.解决不了...

  • 一.小结 1.字符串是封装在String类中的对象。要创建一个字符串,可以使用11种构造方法之一,也可以使用字符串直接量进行简捷初始化。 2.String对象是不可变的,它的内容不能改变。为了提高效率...

  • 一.小结 1.使用二维数组来存储表格 2.可以使用以下语法来声明二维数组变量: 元素类型[ ] [ ]数组变量 3.可以使用以下语法来创建二维数组变量: new 元素类型 [行的个数][列的个数] 4...

  • 顺序结构 Java的执行过程就是顺序结构,除非说明,不然都是一句接一句执行。 选择结构 if选择结构 if(布尔表达式1){ //布尔表达式1为true执行 } else if(布尔表达式2)...

  • 1. 方法引用 1.1. 一种引用方法的轻量级语法 1.1.1. 提供了一种简短的语法 1.1.2. 标准语法为Classname::methodName 1.2. 凡是使用Lambda表达式的地方,...

  • 作者:小傅哥 博客:https://bugstack.cn 源码:https://github.com/fuzhengwei/java-algorithms 沉淀、分享、成长,让自己和他人都能有所收...

  • 作者:Apache Dubbo Contributor 陈景明 背景 在一些业务场景, 往往需要自定义异常来满足特定的业务, 主流用法是在catch里抛出异常, 例如: public void de...

  • 日志概念 1. 日志文件 日志文件是用于记录系统操作事件的文件集合 1.1 调试日志 1.2 系统日志 系统日志是记录系统中硬件、软件和系统问题的信息,同时还可以监视系统中发生的事件。用户可以通过它来...

  • 概述上图是说明了JavaFX 平台各个各个架构组件,我们将简要描述每个组件的作用以及各个组件之间的联系。最上层是Java FX 公开API(给开发者使用的)Scene Graph。在Java FX 公...

  • 1. 反射的概念 反射 机制指的是,程序在运行时能够获取自身的信息。在 java 中只要给定类的名字,就能够获取类的所有属性和方法。 反射是 Java 中很多高级特性的基础,比如 注解、动态代理 以及...

  • 一.小结 1.一个boolean变量可以存储值true或false 2.关系运算符(,>=)和数值及字符一起运算 3.布尔运算符&&,||  ,|  和  ^对布尔值和布尔变量进行计算 4.当对p1&...

  • 基本的 ls mv midir就不说了。 一. 后端部署 1.1 启动jar包 # 默认会将所有日志存放到一个当前目录中新建的 nohup.out文件中 nohup java -jar -Duser...

  • 前言 好久没写文章了, 今天之所以突然心血来潮, 是因为昨天出现了这样一个情况: 我们公司的某个手机APP后端的用户(customer)微服务出现内存泄露, 导致OutOfMemoryError, 但...

  • Logback 算是JAVA 里一个老牌的日志框架,从06年开始第一个版本,迭代至今也十几年了。不过logback最近一个稳定版本还停留在 2017 年,好几年都没有更新;logback的兄弟 slf...

  • 员工管理系统开发 一、DAO接口 package com.lanson.dao; import com.lanson.pojo.Emp; import java.util.List; /** ...

  • 项目开发中会经常使用到各种枚举值,枚举值一般都是固定的,不会随意改变其中的值。 比如性别分为男女,确定之后一般都不会轻易改变,这时候使用枚举值就非常地方便。很多 时候,在页面中传入的参数就是枚举值中的...

  • 前几天刷博客时,无意中看到一篇名为《CopyOnWriteArrayList真的完全线程安全吗》博客。心中不禁泛起疑问,它就是线程安全的啊,难道还有啥特殊情况?我们知道CopyOnWrite的核心思想...