5

我有一个project Awhopom.xml具有以下依赖项slf4j

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

现在我有不同project B的人pom.xml有以下依赖slf4j

   </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.3</version>
    </dependency>

现在我需要集成两个项目,但是当它们有不同的版本时,它们不能很好地协同工作。所以这就是我尝试的方法1 - 在更改为之后升级
slf4jproject Apom.xml

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

我开始看到构建错误说

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.

并且测试似乎失败了

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:115)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:102)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 

方法 2 - 降级 slf4jproject B

改变pom.xml如下

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.5.10</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.3</version>
    </dependency>

我在测试中看到错误

testPutDocument(com.org.sparrow.business.persist.MongoServiceTest): org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
  testPutDocuments(com.org.sparrow.business.persist.MongoServiceTest): org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
  testGetDocument(com.org.sparrow.business.persist.MongoServiceTest): org.slf4j.helpers.MessageFormatter.arrayFormat(Ljava/lang/String;[Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;  

解决这个问题的最佳方法是什么?

更新 依赖树在这里

[INFO] +- commons-codec:commons-codec:jar:1.4:compile
[INFO] +- spy:spymemcached:jar:2.6:compile
[INFO] +- com.google.code.findbugs:annotations:jar:1.3.9:compile
[INFO] +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] +- redis.clients:jedis:jar:1.4.0:compile
[INFO] +- com.googlecode:hibernate-memcached:jar:1.2.2:compile
[INFO] |  \- spy:memcached:jar:2.4.2:compile
[INFO] +- j2ssh:core:jar:0.2.9-SR:compile
[INFO] +- emma:maven-emma-plugin:jar:0.5:test
[INFO] |  +- ant:ant:jar:1.6.3:test
[INFO] |  \- emma:emma_ant:jar:2.0.5312:test
[INFO] +- emma:emma:jar:2.0.4217:test
[INFO] |  +- commons-jelly:commons-jelly-tags-regexp:jar:1.0:test
[INFO] |  \- regexp:regexp:jar:1.3:test
[INFO] +- org.apache.commons:commons-email:jar:1.1:compile
[INFO] |  +- javax.mail:mail:jar:1.4:compile
[INFO] |  \- javax.activation:activation:jar:1.1:compile
[INFO] +- hsqldb:hsqldb:jar:1.8.0.7:test
[INFO] +- org.aspectj:aspectjrt:jar:1.6.1:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.6.1:compile
[INFO] +- org.springframework:spring-context-support:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-web:jar:3.0.5.RELEASE:compile
[INFO] |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework:spring-tx:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:3.0.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-asm:jar:3.0.5.RELEASE:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- org.springframework:spring-context:jar:3.0.5.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-beans:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-agent:jar:2.5.6:compile
[INFO] +- org.springframework:spring-aop:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-aspects:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-core:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-web:jar:3.0.5.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-cas-client:jar:3.0.5.RELEASE:compile
[INFO] |  \- org.jasig.cas:cas-client-core:jar:3.1.10:compile
[INFO] +- org.springframework.security:spring-security-config:jar:3.0.5.RELEASE:compile
[INFO] +- org.jasig.cas.client:cas-client-core:jar:3.2.0:compile
[INFO] +- org.hibernate:hibernate-envers:jar:3.5.6-Final:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:3.5.6-Final:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  |  \- javax.transaction:jta:jar:1.1:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.0.Final:compile
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile
[INFO] |  +- org.hibernate:hibernate-annotations:jar:3.5.6-Final:compile
[INFO] |  \- org.hibernate:hibernate-tools:jar:3.2.0.ga:compile
[INFO] |     +- org.beanshell:bsh:jar:2.0b4:compile
[INFO] |     +- freemarker:freemarker:jar:2.3.8:compile
[INFO] |     \- org.hibernate:jtidy:jar:r8-20060801:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.5.6-Final:compile
[INFO] |  \- cglib:cglib:jar:2.2:compile
[INFO] |     \- asm:asm:jar:3.1:compile
[INFO] +- org.hibernate:hibernate-c3p0:jar:3.5.6-Final:compile
[INFO] +- org.hibernate:hibernate-validator:jar:4.1.0.Final:compile
[INFO] |  \- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] +- org.springframework:spring-hibernate3:jar:2.0.8:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.5.10:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.5.10:compile
[INFO] +- javassist:javassist:jar:3.4.GA:compile
[INFO] +- c3p0:c3p0:jar:0.9.1.2:compile
[INFO] +- eu.medsea:utils:jar:1.0:compile
[INFO] +- velocity-tools:velocity-tools-view:jar:1.4:compile
[INFO] +- velocity:velocity:jar:1.5:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.1:compile
[INFO] |  \- oro:oro:jar:2.0.8:compile
[INFO] +- axis:axis:jar:1.4:compile
[INFO] |  +- org.apache.axis:axis-jaxrpc:jar:1.4:compile
[INFO] |  +- org.apache.axis:axis-saaj:jar:1.4:compile
[INFO] |  \- commons-discovery:commons-discovery:jar:0.2:runtime
[INFO] +- junit:junit:jar:4.8.2:compile
[INFO] +- org.jmock:jmock-junit4:jar:2.5.1:compile
[INFO] |  \- org.jmock:jmock:jar:2.5.1:compile
[INFO] |     +- org.hamcrest:hamcrest-core:jar:1.1:compile
[INFO] |     \- org.hamcrest:hamcrest-library:jar:1.1:compile
[INFO] +- xalan:xalan:jar:2.7.0:compile
[INFO] |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] +- xerces:xercesImpl:jar:2.4.0:compile
[INFO] +- jdom:jdom:jar:1.0:compile
[INFO] +- net.sourceforge.jexcelapi:jxl:jar:2.6:compile
[INFO] +- log4j:log4j:jar:1.2.15:compile
[INFO] +- jaxen:jaxen:jar:1.1.1:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  \- xom:xom:jar:1.0:compile
[INFO] |     +- xerces:xmlParserAPIs:jar:2.6.2:compile
[INFO] |     \- com.ibm.icu:icu4j:jar:2.6.1:compile
[INFO] +- mapquest:mapquest:jar:5.3.0:compile
[INFO] +- jfree:jfreechart:jar:1.0.0:compile
[INFO] |  \- jfree:jcommon:jar:1.0.0:compile
[INFO] +- org.fraid.fraid:fraid:jar:1.5:compile
[INFO] +- com.thoughtworks.xstream:xstream:jar:1.3.1:compile
[INFO] |  \- xpp3:xpp3_min:jar:1.1.4c:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.13:compile
[INFO] +- joda-time:joda-time:jar:1.6:compile
[INFO] +- nl.jqno.equalsverifier:equalsverifier:jar:0.6.2:test
[INFO] |  +- org.objenesis:objenesis:jar:1.1:test
[INFO] |  \- cglib:cglib-nodep:jar:2.2:test
[INFO] +- org.easymock:easymockclassextension:jar:2.5.2:test
[INFO] |  \- org.easymock:easymock:jar:2.5.2:test
[INFO] +- org.springframework:spring-test:jar:3.0.5.RELEASE:compile
[INFO] +- cdyne:cdyne:jar:3.0:compile
[INFO] |  \- javax.xml:jaxrpc-api:jar:1.1:compile
[INFO] +- json:json:jar:0.0.1:compile
[INFO] +- org.dbunit:dbunit:jar:2.4.8:test
[INFO] +- org.mongodb:mongo-java-driver:jar:2.2:compile
[INFO] +- com.google.code.gson:gson:jar:1.4:compile
[INFO] +- xml-security:xmlsec:jar:1.3.0:compile
[INFO] \- javatar:javatar:jar:2.5:compile
4

2 回答 2

1

在您的问题中,您提到您在项目A的 pom.xml 中将 slf4j-api 和 slf4j-log4j12 升级到版本1.6.4但您发布的依赖关系树提到了 slf4j-api 和 slf4j-log4j12 版本1.5.0。只要你确定一个版本,使 slf4j-api 和它的绑定版本匹配,你会没事的。

您的其他依赖项为 SLF4J 声明的 SLF4J 版本无关紧要。从客户端的角度来看,所有版本的 slf4j 都是二进制兼容的

于 2012-07-17T15:43:13.530 回答
1

作为库包含在其他项目中的 Jar 项目不应在 POM 中包含除 slf4j-api 之外的任何内容。由最终项目(即您的示例中的项目 A)决定实施。对于测试项目 B,可以包含诸如 slf4j-log4j12 和 log4j 本身之类的实现,但这些应该test在 Maven 中进行。

于 2012-07-17T21:24:42.663 回答