日期作者版本备注
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


apollo008
151 声望9 粉丝

走完这一生,如果我和你在一起会变得更好,那我们就在一起,否则我就丢下你。我回顾我最光辉的时刻就是和不同的人在一起,变得更好的最长连续时刻。