除了maven命令行以外,idea和eclips开发工具都提供了查看依赖关系的方法,这里只说原始的方法,工具的方法可以参考其它的文档。

1. 错误信息

相同的代码和pom.xml配置,在一个项目中,可以正常运行,在另外一个项目中,则出现报错:

Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
22:49:28.757 [RuoyiScheduler_Worker-1] ERROR c.z.q.u.AbstractQuartzJob - [execute,50] - 任务执行异常  - :
java.lang.reflect.InvocationTargetException: null
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.zxtz.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:62)
	at com.zxtz.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:34)
	at com.zxtz.quartz.util.QuartzDisallowConcurrentExecution.doExecute(QuartzDisallowConcurrentExecution.java:20)
	at com.zxtz.quartz.util.AbstractQuartzJob.execute(AbstractQuartzJob.java:44)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NoSuchMethodError: com.google.common.util.concurrent.SimpleTimeLimiter.create(Ljava/util/concurrent/ExecutorService;)Lcom/google/common/util/concurrent/SimpleTimeLimiter;
	at org.openqa.selenium.net.UrlChecker.<init>(UrlChecker.java:62)
	at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:188)
	at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:179)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:79)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:157)
	at com.zxtz.quartz.task.RyTask.seleniumProcess(RyTask.java:42)
	at com.zxtz.quartz.task.RyTask.ryNoParams(RyTask.java:75)
	... 10 common frames omitted

值得怀疑的就是两个项目中依赖包的版本不同。

2. 找出两个项目各自依赖的jar

maven提供了输出项目依赖树的命令。

mvn dependency:tree

可以通过参数-Doutput=jar-dependency.txt,将依赖信息输出到文件jar-dependency.txt。

-Dverbose,把artifact的每处依赖都详细显示出来,可用来查看冲突和重复的具体情况,该参数可以列出多级依赖关系。

excludes,includes:通过逗号分离,格式[groupId]:[artifactId]:[type]:[version],支持通配符*

通过该命令,可以输出项目所有的依赖关系。

D:\projects-demo\spring-boot-console>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.example:spring-boot-console >-------------------
[INFO] Building spring-boot-console 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for com.alibaba:druid:jar:1.2.6 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ spring-boot-console ---
[INFO] org.example:spring-boot-console:jar:1.0
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.3.11.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:2.3.11.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:5.2.15.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.3.11.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.3.11.RELEASE:compile
[INFO] |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO] |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
[INFO] |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
[INFO] |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] |  +- org.springframework:spring-core:jar:5.2.15.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.2.15.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.26:compile
[INFO] +- mysql:mysql-connector-java:jar:8.0.25:compile
[INFO] |  \- com.google.protobuf:protobuf-java:jar:3.11.4:compile
[INFO] +- com.baomidou:mybatis-plus-boot-starter:jar:3.4.2:compile
[INFO] |  +- com.baomidou:mybatis-plus:jar:3.4.2:compile
[INFO] |  |  \- com.baomidou:mybatis-plus-extension:jar:3.4.2:compile
[INFO] |  |     +- com.baomidou:mybatis-plus-core:jar:3.4.2:compile
[INFO] |  |     |  +- com.baomidou:mybatis-plus-annotation:jar:3.4.2:compile
[INFO] |  |     |  +- com.github.jsqlparser:jsqlparser:jar:4.0:compile
[INFO] |  |     |  \- org.mybatis:mybatis:jar:3.5.6:compile
[INFO] |  |     \- org.mybatis:mybatis-spring:jar:2.0.5:compile
[INFO] |  \- org.springframework.boot:spring-boot-starter-jdbc:jar:2.3.2.RELEASE:compile
[INFO] |     +- com.zaxxer:HikariCP:jar:3.4.5:compile
[INFO] |     \- org.springframework:spring-jdbc:jar:5.2.8.RELEASE:compile
[INFO] |        \- org.springframework:spring-tx:jar:5.2.8.RELEASE:compile
[INFO] +- com.alibaba:druid-spring-boot-starter:jar:1.2.6:compile
[INFO] |  +- com.alibaba:druid:jar:1.2.6:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.3.11.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.3.11.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.11.4:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.4:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.11.4:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.4:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.4:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.4:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.3.11.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.46:compile
[INFO] |  |  +- org.glassfish:jakarta.el:jar:3.0.3:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.46:compile
[INFO] |  +- org.springframework:spring-web:jar:5.2.15.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.2.15.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.2.15.RELEASE:compile
[INFO] |     +- org.springframework:spring-aop:jar:5.2.15.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:5.2.15.RELEASE:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.5.13:compile
[INFO] |  +- org.apache.httpcomponents:httpcore:jar:4.4.13:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  \- commons-codec:commons-codec:jar:1.11:compile
[INFO] +- org.jsoup:jsoup:jar:1.14.3:compile
[INFO] \- org.seleniumhq.selenium:selenium-java:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-api:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-chrome-driver:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-edge-driver:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-ie-driver:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-opera-driver:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-remote-driver:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-safari-driver:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-support:jar:3.141.59:compile
[INFO]    +- net.bytebuddy:byte-buddy:jar:1.8.15:compile
[INFO]    +- org.apache.commons:commons-exec:jar:1.3:compile
[INFO]    +- com.google.guava:guava:jar:25.0-jre:compile
[INFO]    |  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO]    |  +- org.checkerframework:checker-compat-qual:jar:2.0.0:compile
[INFO]    |  +- com.google.errorprone:error_prone_annotations:jar:2.1.3:compile
[INFO]    |  +- com.google.j2objc:j2objc-annotations:jar:1.1:compile
[INFO]    |  \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
[INFO]    +- com.squareup.okhttp3:okhttp:jar:3.11.0:compile
[INFO]    \- com.squareup.okio:okio:jar:1.14.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.685 s
[INFO] Finished at: 2022-05-06T14:30:06+08:00
[INFO] ------------------------------------------------------------------------

只需要在两个项目中,分别执行依赖树查看,然后进行比较即可。

但是,当项目规模比较大的时候,项目依赖的jar包会非常多,这个时候即使有完整的依赖关系,对比起来也会非常困难。 

如果不需要列出依赖树,只想查看依赖的jar列表,也可以使用这个参数。

D:\projects-demo\spring-boot-console>mvn dependency:list
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.example:spring-boot-console >-------------------
[INFO] Building spring-boot-console 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for com.alibaba:druid:jar:1.2.6 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]
[INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ spring-boot-console ---
[INFO]
[INFO] The following files have been resolved:
[INFO]    org.glassfish:jakarta.el:jar:3.0.3:compile
[INFO]    org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO]    org.springframework:spring-expression:jar:5.2.15.RELEASE:compile
[INFO]    com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.4:compile
[INFO]    com.google.protobuf:protobuf-java:jar:3.11.4:compile
[INFO]    net.bytebuddy:byte-buddy:jar:1.8.15:compile
[INFO]    com.fasterxml.jackson.core:jackson-annotations:jar:2.11.4:compile

3. 查找特定jar包的依赖关系

查找某个jar包被哪些地方依赖。

D:\projects-demo\spring-boot-console>mvn dependency:tree -Dverbose -Dincludes=org.seleniumhq.selenium:selenium-java:jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.example:spring-boot-console >-------------------
[INFO] Building spring-boot-console 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for com.alibaba:druid:jar:1.2.6 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ spring-boot-console ---
[WARNING] Invalid POM for com.alibaba:druid:jar:1.2.6, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] org.example:spring-boot-console:jar:1.0
[INFO] \- org.seleniumhq.selenium:selenium-java:jar:3.141.59:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.581 s
[INFO] Finished at: 2022-05-06T14:55:25+08:00
[INFO] ------------------------------------------------------------------------

可以看出org.seleniumhq.selenium:selenium-java:jar包被主程序依赖。

查看依赖com.google.guava:guava:jar:25.0-jre的jar包有哪些:

D:\projects-demo\spring-boot-console>mvn dependency:tree -Dverbose -Dincludes=com.google.guava:guava:jar:25.0-jre
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.example:spring-boot-console >-------------------
[INFO] Building spring-boot-console 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for com.alibaba:druid:jar:1.2.6 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ spring-boot-console ---
[WARNING] Invalid POM for com.alibaba:druid:jar:1.2.6, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] org.example:spring-boot-console:jar:1.0
[INFO] \- org.seleniumhq.selenium:selenium-java:jar:3.141.59:compile
[INFO]    +- org.seleniumhq.selenium:selenium-chrome-driver:jar:3.141.59:compile
[INFO]    |  \- (com.google.guava:guava:jar:25.0-jre:compile - omitted for duplicate)
[INFO]    +- org.seleniumhq.selenium:selenium-edge-driver:jar:3.141.59:compile
[INFO]    |  \- (com.google.guava:guava:jar:25.0-jre:compile - omitted for duplicate)
[INFO]    +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.141.59:compile
[INFO]    |  \- (com.google.guava:guava:jar:25.0-jre:compile - omitted for duplicate)
[INFO]    +- org.seleniumhq.selenium:selenium-ie-driver:jar:3.141.59:compile
[INFO]    |  \- (com.google.guava:guava:jar:25.0-jre:compile - omitted for duplicate)
[INFO]    +- org.seleniumhq.selenium:selenium-opera-driver:jar:3.141.59:compile
[INFO]    |  \- (com.google.guava:guava:jar:25.0-jre:compile - omitted for duplicate)
[INFO]    +- org.seleniumhq.selenium:selenium-remote-driver:jar:3.141.59:compile
[INFO]    |  \- (com.google.guava:guava:jar:25.0-jre:compile - omitted for duplicate)
[INFO]    +- org.seleniumhq.selenium:selenium-safari-driver:jar:3.141.59:compile
[INFO]    |  \- (com.google.guava:guava:jar:25.0-jre:compile - omitted for duplicate)
[INFO]    +- org.seleniumhq.selenium:selenium-support:jar:3.141.59:compile
[INFO]    |  \- (com.google.guava:guava:jar:25.0-jre:compile - omitted for duplicate)
[INFO]    \- com.google.guava:guava:jar:25.0-jre:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.458 s
[INFO] Finished at: 2022-05-06T14:58:09+08:00
[INFO] ------------------------------------------------------------------------

可以看出com.google.guava:guava:jar:25.0-jre,被多个jar包所依赖。

4. maven自动解决依赖冲突的规则

第一原则:路径最近者优先

A->B->C->X(1.0)

A->D->X(2.0)

则该例子中,X的版本是2.0

第二原则:路径相等,先声明者优先

项目A有如下的依赖关系:

A->B->Y(1.0)

A->C->Y(2.0)

若pom文件中B的依赖坐标先于C进行声明,则最终Y的版本为1.0

5. 依赖信息查看

状态信息以及含义:

最后写着compile的就是编译成功的。

最后写着omitted for duplicate的就是有jar包被重复依赖了,但是jar包的版本是一样的。

最后写着omitted for conflict with xxxx的,说明和别的jar包版本冲突了,而该行的jar包不会被引入。

最后写着version managed from 2.3 ;omitted for duplicate ,表示最终使用commons-pool2最终会使用2.4.2,拒绝使用声明的2.3版本

version managed from 1.16.8 ;表示最终使用lombok:jar:1.16.22版本。