daydreamer
daydreamer

Reputation: 91969

Java/Maven: different slf4j versions in different projects causing build errors on integration

I have a project A whose pom.xml has the following dependency on 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>

Now I have different project B whose pom.xml has following dependency on 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>

Now I need to integrate two projects but they don't play well together when they have different versions. So this is what I tried
Approach 1 - Upgrading slf4j in project A After changing the pom.xml to

    <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>

I started to see build errors saying

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.

and tests seem to fail with

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 

Approach 2 - Downgrading slf4j in project B

After changing pom.xml like following

    <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>

I see errors in tests as

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;  

What is the best way to approach this problem?

UPDATE dependency tree is here

[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

Upvotes: 5

Views: 3910

Answers (2)

artbristol
artbristol

Reputation: 32407

Jar projects for inclusion as libraries in other projects should not include anything in the POM other than slf4j-api. It's up to the final project (i.e. project A in your example) to decide on the implementation. For testing project B, it's acceptable to include an implementation such as slf4j-log4j12 and log4j itself, but these should be scoped test in Maven.

Upvotes: 1

Ceki
Ceki

Reputation: 27450

In your question you mention that you upgraded slf4j-api and slf4j-log4j12 to version 1.6.4 in project A's pom.xml but the dependency tree you posted mentions slf4j-api and slf4j-log4j12 version 1.5.0. As long as you settle on a version so that slf4j-api and the version of its binding match, you will be fine.

It does not matter what SLF4J version your other dependencies declare for SLF4J. From the client's perspective all versions of slf4j are binary compatible.

Upvotes: 1

Related Questions