Maven依赖解析(冲突) [英] Maven dependency resolution (conflicted)

查看:143
本文介绍了Maven依赖解析(冲突)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有四个项目:


  • 项目A(依赖于B和D)

  • 项目B(依赖于D)

  • 项目C(依赖于D)

  • 项目D

  • Project A (has a dependency on B and D)
  • Project B (has a dependency on D)
  • Project C (has a dependency on D)
  • Project 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

  • Project A (has a dependency on B and C)
  • Project B (has a dependency on D)
  • Project C (has a dependency on D)
  • Project 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 adress such kind of problems. I mean in a real world application you have a lot of depenendcies 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 differend dependency. Of cause 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.

推荐答案

解决此类情况的主要方法是包含< dependencyManagement> 部分在项目的根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.

参考:

  • Dependency Management

这篇关于Maven依赖解析(冲突)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆