使用 Maven 构建时 slf4j 版本冲突
我意识到我的一个项目使用 slf4j 1.5.8
而 Hibernate 使用 slf4j 1.6
。使用 Maven 构建时,它会下载两个 jar,但我猜使用了 1.5.8 的类文件。因此,当我运行该程序时,出现以下错误:
SLF4J: The requested version 1.5.8 by your slf4j binding is not compatible with [1.6]
在 pom.xml
中,我已将
<dependencyManagement>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
</dependencyManagement>
1.5.8 作为依赖项的一部分,因此它是自行下载的。
I realised that one of my projects uses slf4j 1.5.8
and Hibernate uses slf4j 1.6
. While building with Maven it downloads both jars but I guess the class files of 1.5.8 are used. So, when I run the program i get following error:
SLF4J: The requested version 1.5.8 by your slf4j binding is not compatible with [1.6]
In pom.xml
I have put
<dependencyManagement>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
</dependencyManagement>
The 1.5.8 is part of dependency so it's downloaded on its own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您自己发现的,有两个库(Hibernate 和其他库)在两个不同版本中传递导入 SLF4J。不幸的是,maven 正在使用旧版本(在这种情况下,Maven 应选择一些依赖项的规则)。解决方案是在导入旧版本 SLF4J 的依赖项中添加
exclusion
(com.example:foo-bar
是此处的示例):如果您仍然遇到此问题,问题:
查找 1.5.8 版本并将其从导入它的所有库中排除。
As you discovered yourself, there are two libraries (Hibernate and some other) transitively importing SLF4J in two different versions. Unfortunately the older version is being picked up by maven (there are some rules which dependency should be chosen by Maven in this situation). The solution is to add the
exclusion
in the dependency that imports older version of SLF4J (com.example:foo-bar
is example here):If you still experience this problem, issue:
Look for 1.5.8 version and exclude it from all libraries importing it.
排除是完全没有必要的,而且可能会产生误导。相反,明确在您的项目 pom 文件中包含所需版本的 slf4j-api。就是这样!
这种方法利用了 Maven 的传递规则:最接近的依赖声明获胜。
Excluding is quite unnecessary and maybe quite misleading. Instead, explicitly include the slf4j-api with the desired version in your projects pom file. That's it!
This approach takes advantage of Maven's transitivity rules: the nearest dependency declaration wins.
您可以通过以下方式排除错误的版本:
you can exclude the wrong version with something like this: