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

64次阅读

共计 11882 个字符,预计需要花费 30 分钟才能阅读完成。

日期 作者 版本 备注
2022-10-05 dingbinthu@163.com V1.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 文件。而后从新导入,编译,运行,所有就都胜利了。

正文完
 0