Maven + SLF4J:使用需要两个不同SLF4J版本的两个不同依赖关系时版本冲突 [英] Maven + SLF4J: Version conflict when using two different dependencies that require two different SLF4J versions

查看:849
本文介绍了Maven + SLF4J:使用需要两个不同SLF4J版本的两个不同依赖关系时版本冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独立使用这两个依赖项的项目:BoneCP和Hibernate。但是由于SLF4J及其版本冲突,它不起作用,因为BoneCP需要SLF4J 1.5,而Hibernate需要SLF4j 1.6。如你所知,pom.xml中不可能重要的两个不同版本的相同依赖关系。那么我能做什么来解决这个惊人的SLF4J副作用?



我得到的错误是臭名昭着的:

  SLF4J:您的slf4j绑定所请求的版本1.5.10与[1.6]不兼容
SLF4J:请参阅http://www.slf4j.org/codes .html#version_mismatch了解更多详细信息。

我需要添加这个,但不允许使用两个不同版本相同的依赖关系:

 <依赖关系> 
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-log4j12< / artifactId>
< version> 1.5.10< / version>
< scope>已提供< / scope>
< / dependency>

<依赖关系>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-log4j12< / artifactId>
< version> 1.6.2< / version>
< scope>已提供< / scope>
< / dependency>

Maven依赖关系树:

  [INFO] [依赖:tree {execution:default-cli}] 
[INFO] org.mentawai:menta:war:1.0.5-SNAPSHOT
[INFO] + - javax.servlet.jsp:jsp-api:jar:2.0:提供
[INFO] + - javax.servlet:servlet-api:jar:2.5:
[INFO] + - javax.activation :activate:jar:1.1:compile
[INFO] + - javax.mail:mail:jar:1.4:compile
[INFO] + - javax.persistence:persistence-api:jar:1.0:compile
[INFO] + - org.slf4j:slf4j-log4j12:jar:1.5.10:compile
[INFO] | + - org.slf4j:slf4j-api:jar:1.5.10:compile
[INFO] | \- log4j:log4j:jar:1.2.14:compile
[INFO] + - org.hibernate:hibernate-core:jar:3.6.7.Final:compile
[INFO] | + - antlr:antlr:jar:2.7.6:compile
[INFO] | + - commons-collections:commons-collections:jar:3.1:compile
[INFO] | + - dom4j:dom4j:jar:1.6.1:compile
[INFO] | + - org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile
[INFO] | + - org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] | \- javax.transaction:jta:jar:1.1:compile
[INFO] + - javassist:javassist:jar:3.12.1.GA:compile
[INFO] + - junit:junit:jar :4.8.1:test
[INFO] + - c3p0:c3p0:jar:0.9.1.2:compile
[INFO] + - com.h2database:h2:jar:1.2.138:compile
[INFO] + - mysql:mysql-connector-java:jar:5.1.13:compile
[INFO] + - me.soliveirajr:mentawai:jar:2.3.3-SNAPSHOT:compile
[INFO] | + - net.sf.json-lib:json-lib:jar:jdk15:2.3:compile
[INFO] | | + - commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] | | + - commons-logging:commons-logging:jar:1.1.1:compile
[INFO] | | \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] | + - org.jdom:jdom:jar:1.1:compile
[INFO] | + - com.thoughtworks.xstream:xstream:jar:1.3.1:compile
[INFO] | | \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] | + - org.ajaxtags:ajaxtags:jar:1.2-beta3:compile
[INFO] | | + - javax.servlet:jstl:jar:1.0.6:compile
[INFO] | | + - taglibs:standard:jar:1.0.6:compile
[INFO] | | \- net.htmlparser:jericho-html:jar:2.1:compile
[INFO] | + - jgroups:jgroups-all:jar:2.2.9.1:compile
[INFO] | + - me.soliveirajr:menta-container:jar:0.9.8:compile
[INFO] | + - me.soliveirajr:menta-bean:jar:1.1.1:compile
[INFO] | + - me.soliveirajr:menta-regex:jar:0.9.5:compile
[INFO] | + - org.beanshell:bsh:jar:2.0b4:compile
[INFO] | + - com.jolbox:bonecp:jar:0.7.1.RELEASE:compile
[INFO] | | \- com.google.guava:guava:jar:r08:compile
[INFO] | + - velocity:speed-dep:jar:1.4:compile
[INFO] | + - commons-fileupload:commons-fileupload:jar:1.2.2:compile
[INFO] | + - commons-io:commons-io:jar:1.3.2:compile
[INFO] | + - net.tanesha.recaptcha4j:recaptcha4j:jar:0.0.7:compile
[INFO] | \- commons-dbcp:commons-dbcp:jar:1.4:compile
[INFO] | \- commons-pool:commons-pool:jar:1.5.4:compile
[INFO] + - commons-lang:commons-lang:jar:2.5:compile
[INFO] asm:asm:jar:3.2:编译


解决方案

错误消息中提供了 http://www.slf4j.org/codes.html#version_mismatch 状态:


SLF4J绑定指定一个工件,如slf4j-jdk14.jar或
slf4j-log4j12.jar用于绑定slf4j到底层日志
框架,比如java.util.logging或log4j。混合混合不同的
版本的slf4j-api.jar和SLF4J绑定可能会导致问题。对于
示例,如果您使用slf4j-api-1.6.6.jar,那么您还应该使用
使用slf4j-simple-1.6.6.jar,使用slf4j-simple-1.5.5.jar不会
工作。



注意从客户的角度来看,slf4j-api的所有版本都是
兼容
。使用slf4j-api-N.jar编译的客户端代码将为任何N和M运行
完全正确的slf4j-api-M.jar。您只需要
确保您的绑定版本匹配的
slf4j-api.jar。您不必担心项目中给定的依赖关系使用的
slf4j-api.jar的版本。你可以
总是使用任何版本的slf4j-api.jar,只要版本
slf4j-api.jar及其绑定匹配,你应该是可以的。


您需要的是在您的pom.xml中声明对slf4j-log4j12某些版本的SLF4J的依赖。我建议1.6.6是最新的。这将通过Maven的传递性规则将相同版本的slf4j-api拉入您的项目。



所以,只需在你的pom.xml文件中声明以下内容。

 <依赖性> 
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-log4j12< / artifactId>
< version> 1.7.5< / version>
< / dependency>

我假设您实际上不需要在提供的文件中声明slf4j-api和slf4j-log4j12范围。


I have a project that use both dependencies independently: BoneCP and Hibernate. But thanks to SLF4J and its version conflicts it does not work because BoneCP requires SLF4J 1.5 and Hibernate requires SLF4j 1.6. As you know it is not possible to important two different versions of the same dependency in your pom.xml. So what can I do to workaround this amazing SLF4J side-effect???

The error I get is the infamous:

SLF4J: The requested version 1.5.10 by your slf4j binding is not compatible with [1.6]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.

I would need to add this, but same dependency with two different versions is not allowed:

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.10</version>
<scope>provided</scope>
</dependency>   

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.2</version>
<scope>provided</scope>
</dependency>   

Maven dependency tree:

[INFO] [dependency:tree {execution: default-cli}]
[INFO] org.mentawai:menta:war:1.0.5-SNAPSHOT
[INFO] +- javax.servlet.jsp:jsp-api:jar:2.0:provided
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] +- javax.activation:activation:jar:1.1:compile
[INFO] +- javax.mail:mail:jar:1.4:compile
[INFO] +- javax.persistence:persistence-api:jar:1.0:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.5.10:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.5.10:compile
[INFO] |  \- log4j:log4j:jar:1.2.14:compile
[INFO] +- org.hibernate:hibernate-core:jar:3.6.7.Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] |  \- javax.transaction:jta:jar:1.1:compile
[INFO] +- javassist:javassist:jar:3.12.1.GA:compile
[INFO] +- junit:junit:jar:4.8.1:test
[INFO] +- c3p0:c3p0:jar:0.9.1.2:compile
[INFO] +- com.h2database:h2:jar:1.2.138:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.13:compile
[INFO] +- me.soliveirajr:mentawai:jar:2.3.3-SNAPSHOT:compile
[INFO] |  +- net.sf.json-lib:json-lib:jar:jdk15:2.3:compile
[INFO] |  |  +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] |  |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  |  \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] |  +- org.jdom:jdom:jar:1.1:compile
[INFO] |  +- com.thoughtworks.xstream:xstream:jar:1.3.1:compile
[INFO] |  |  \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] |  +- org.ajaxtags:ajaxtags:jar:1.2-beta3:compile
[INFO] |  |  +- javax.servlet:jstl:jar:1.0.6:compile
[INFO] |  |  +- taglibs:standard:jar:1.0.6:compile
[INFO] |  |  \- net.htmlparser:jericho-html:jar:2.1:compile
[INFO] |  +- jgroups:jgroups-all:jar:2.2.9.1:compile
[INFO] |  +- me.soliveirajr:menta-container:jar:0.9.8:compile
[INFO] |  +- me.soliveirajr:menta-bean:jar:1.1.1:compile
[INFO] |  +- me.soliveirajr:menta-regex:jar:0.9.5:compile
[INFO] |  +- org.beanshell:bsh:jar:2.0b4:compile
[INFO] |  +- com.jolbox:bonecp:jar:0.7.1.RELEASE:compile
[INFO] |  |  \- com.google.guava:guava:jar:r08:compile
[INFO] |  +- velocity:velocity-dep:jar:1.4:compile
[INFO] |  +- commons-fileupload:commons-fileupload:jar:1.2.2:compile
[INFO] |  +- commons-io:commons-io:jar:1.3.2:compile
[INFO] |  +- net.tanesha.recaptcha4j:recaptcha4j:jar:0.0.7:compile
[INFO] |  \- commons-dbcp:commons-dbcp:jar:1.4:compile
[INFO] |     \- commons-pool:commons-pool:jar:1.5.4:compile
[INFO] +- commons-lang:commons-lang:jar:2.5:compile
[INFO] \- asm:asm:jar:3.2:compile

解决方案

The link provided in the error message, "http://www.slf4j.org/codes.html#version_mismatch", states:

An SLF4J binding designates an artifact such as slf4j-jdk14.jar or slf4j-log4j12.jar used to bind slf4j to an underlying logging framework, say, java.util.logging or log4j. Mixing mixing different versions of slf4j-api.jar and SLF4J binding can cause problems. For example, if you are using slf4j-api-1.6.6.jar, then you should also use slf4j-simple-1.6.6.jar, using slf4j-simple-1.5.5.jar will not work.

NOTE From the client's perspective all versions of slf4j-api are compatible. Client code compiled with slf4j-api-N.jar will run perfectly fine with slf4j-api-M.jar for any N and M. You only need to ensure that the version of your binding matches that of the slf4j-api.jar. You do not have to worry about the version of slf4j-api.jar used by a given dependendency in your project. You can always use any version of slf4j-api.jar, and as long as the version of slf4j-api.jar and its binding match, you should be fine.

All you need to the is to declare a dependency on slf4j-log4j12 some version of SLF4J in your pom.xml. I would suggest 1.6.6 which is the latest. This will pull the same version of slf4j-api into your project by virtue of Maven's transitivity rules.

So, just declare the following in your pom.xml file.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.5</version>
</dependency>   

I am assuming that you do not actually need to declare slf4j-api and slf4j-log4j12 in the provided scope.

这篇关于Maven + SLF4J:使用需要两个不同SLF4J版本的两个不同依赖关系时版本冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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