首页 > 工作 > maven依赖版本冲突解决的误区

maven依赖版本冲突解决的误区

忘记是哪位大仙的说法,记忆中一直是maven依赖树中存在同一jar的多版本时,会自动依赖最新版本。

今天查找项目依赖时,发现在二级依赖中同时存在两个版本,maven引入的却是老版本。

翻了一下官方文档,只说是依赖于最近的版本。实测发现,同级中存在多个版本时,先依赖哪个版本就用哪个版本。

最靠谱的方法,就是直接依赖最新版本,也就是最近的依赖,会覆盖掉子级的低版本依赖。


附官方地址及说法:
http://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html

Resolving conflicts using the dependency tree

A project’s dependency tree can be expanded to display dependency conflicts. For example, to find out why Commons Collections 2.0 is being used by the Maven Dependency Plugin, we can execute the following in the project’s directory:

mvn dependency:tree -Dverbose -Dincludes=commons-collections

The verbose flag instructs the dependency tree to display conflicting dependencies that were omitted from the resolved dependency tree. In this case, the goal outputs:

[INFO] [dependency:tree]
[INFO] org.apache.maven.plugins:maven-dependency-plugin:maven-plugin:2.0-alpha-5-SNAPSHOT
[INFO] +- org.apache.maven.reporting:maven-reporting-impl:jar:2.0.4:compile
[INFO] |  \- commons-validator:commons-validator:jar:1.2.0:compile
[INFO] |     \- commons-digester:commons-digester:jar:1.6:compile
[INFO] |        \- (commons-collections:commons-collections:jar:2.1:compile - omitted for conflict with 2.0)
[INFO] \- org.apache.maven.doxia:doxia-site-renderer:jar:1.0-alpha-8:compile
[INFO]    \- org.codehaus.plexus:plexus-velocity:jar:1.1.3:compile
[INFO]       \- commons-collections:commons-collections:jar:2.0:compile

Thus we can see that Commons Collections 2.0 was chosen over 2.1 since it is nearer, and by default Maven resolves version conflicts with a nearest-wins strategy.

More specifically, in verbose mode the dependency tree shows dependencies that were omitted for: being a duplicate of another; conflicting with another’s version and/or scope; and introducing a cycle into the dependency tree.

分类: 工作 标签: ,
  1. ThinkingQuest
    2013年12月8日03:56 | #1

    嗯,是最近依赖。直接写在pom中的优先级最高。 此外依赖层次越接近优先级越高。

  1. 本文目前尚无任何 trackbacks 和 pingbacks.