Maven 依赖解析(冲突) [英] Maven dependency resolution (conflicted)
问题描述
假设我有四个项目:
- 项目 A(依赖于 B 和 D)
- 项目 B(依赖于 D)
- 项目 C(依赖于 D)
- 项目 D
在这种情况下,如果我运行项目 A,Maven 将正确解析对 D 的依赖关系.如果我理解正确,Maven 总是采用最短路径的依赖关系.由于 D 是 A 的直接依赖项,因此将使用它而不是在 B 中指定的 D.
In this scenario if I run project A, Maven will correctly resolve the dependency to D. If I understand this correctly Maven always takes the dependency with the shortest path. Since D is a direct dependency of A it will be used rather then, the D which is specified within B.
但现在假设这个结构:
- 项目 A(依赖于 B 和 C)
- 项目 B(依赖于 D)
- 项目 C(依赖于 D)
- 项目 D
在这种情况下,解析 D 的路径具有相同的深度.发生的情况是Maven会发生冲突.我知道可以告诉 Maven 他应该排除依赖项.但我的问题是如何解决此类问题.我的意思是在现实世界的应用程序中,您有很多依赖项,也可能有很多冲突.
In this case the paths to resolving D have the same depth. What happens is that Maven will have a conflict. I know that it is possible to tell Maven that he should exclude dependencies. But my question is how to address such kind of problems. I mean in a real world application you have a lot of dependencies and possibly a lot of conflicts as well.
最佳实践解决方案是否真的要排除某些内容,或者是否有其他可能的解决方案?当我突然收到 ClassNotFound Exception 时,我发现很难处理,因为某些版本已经更改,这导致 Maven 采取不同的依赖项.当然,知道这个事实会让我们更容易猜测问题是依赖冲突.
Is the best practice solution really to exclude stuff or are there other possible solutions to this? I find it very hard to deal with when i suddenly get a ClassNotFound Exception because some versions have changed, which caused Maven to take a different dependency. Of course, knowing this fact makes it a little bit easier to guess that the problem is a dependency conflict.
我使用的是 maven 2.1-SNAPSHOT.
I'm using maven 2.1-SNAPSHOT.
推荐答案
解决此类情况的 Maven 方法是在项目的根 pom 中包含一个
部分,您可以在其中指定将使用哪个库的哪个版本.
The maven way of resolving situations like this is to include a <dependencyManagement>
section in your project's root pom, where you specify which version of which library will be used.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>
现在无论依赖项请求哪个版本的库 foo:bar,1.2.3 版本将始终用于此项目和所有子项目.
Now no matter which version of library foo:bar is requested by a dependency, version 1.2.3 will always be used for this project and all sub-projects.
参考:
这篇关于Maven 依赖解析(冲突)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!