Home > Enterprise >  Resolving slf4j multiple bindings, tracing which artifact is root cause?
Resolving slf4j multiple bindings, tracing which artifact is root cause?

Time:05-26

I've tried to follow the other slf4j and other answers from the web to no avail. I think my scenario is a little different.. I started with a maven spring-boot jpa app, all works good. I am working with some company's sdk but it's not maven. They provide a jar and jar-with-dependencies. This package will be com.example:theirlib:jar:1.0

When I install standalone jar, it cannot find required dependencies. I tried to add them to pom but run into unsupported repository layout legacy. The permanent solutions to this looks to be log4j related, without manually installing those jars.

The following artifacts could not be resolved: javax.jms:jms:jar:1.1, com.sun.jdmk:jmxtools:jar:1.2.1, com.sun.jmx:jmxri:jar:1.2.1: Could not transfer artifact javax.jms:jms:jar:1.1 from/to java.net (https://maven-repository.dev.java.net/nonav/repository): Cannot access https://maven-repository.dev.java.net/nonav/repository with type legacy using the available connector factories: BasicRepositoryConnectorFactory: Cannot access https://maven-repository.dev.java.net/nonav/repository with type legacy using the available layout factories: Maven2RepositoryLayoutFactory: Unsupported repository layout legacy -> [Help 1]

But those were just the first build errors so I tried the jar-with-dependencies, which throws multiple slf4j bindings error.

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/user/.m2/repository/com/example/theirlib/1.0/theirlib-1.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/user/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding.
SLF4J: Your binding is version 1.5.5 or earlier.
SLF4J: Upgrade your binding to version 1.6.x.
...

I am struggling to find the correct artifacts to put in exclusions, if exclusions even works since the installed jar is bundled w/ dependencies?? This seemed like an easier path to solve than trying to include all the correct dependencies and versions for theirlib in this code. Alternatively, I tried to exclusions with the spring dependency but still fail.

First I looked at the dependency tree:

[INFO] com.crush.sample:demo:jar:0.0.1-SNAPSHOT
[INFO]  - org.springframework.boot:spring-boot-starter-data-jpa:jar:2.7.0:compile
[INFO] |   - org.springframework.boot:spring-boot-starter-aop:jar:2.7.0:compile
[INFO] |  |   - org.springframework:spring-aop:jar:5.3.20:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.9.7:compile
[INFO] |   - org.springframework.boot:spring-boot-starter-jdbc:jar:2.7.0:compile
[INFO] |  |   - com.zaxxer:HikariCP:jar:4.0.3:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:5.3.20:compile
[INFO] |   - jakarta.transaction:jakarta.transaction-api:jar:1.3.3:compile
[INFO] |   - jakarta.persistence:jakarta.persistence-api:jar:2.2.3:compile
[INFO] |   - org.hibernate:hibernate-core:jar:5.6.9.Final:compile
[INFO] |  |   - org.jboss.logging:jboss-logging:jar:3.4.3.Final:compile
[INFO] |  |   - net.bytebuddy:byte-buddy:jar:1.12.10:compile
[INFO] |  |   - antlr:antlr:jar:2.7.7:compile
[INFO] |  |   - org.jboss:jandex:jar:2.4.2.Final:compile
[INFO] |  |   - com.fasterxml:classmate:jar:1.5.1:compile
[INFO] |  |   - org.hibernate.common:hibernate-commons-annotations:jar:5.1.2.Final:compile
[INFO] |  |  \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.6:compile
[INFO] |  |      - org.glassfish.jaxb:txw2:jar:2.3.6:compile
[INFO] |  |      - com.sun.istack:istack-commons-runtime:jar:3.0.12:compile
[INFO] |  |     \- com.sun.activation:jakarta.activation:jar:1.2.2:runtime
[INFO] |   - org.springframework.data:spring-data-jpa:jar:2.7.0:compile
[INFO] |  |   - org.springframework.data:spring-data-commons:jar:2.7.0:compile
[INFO] |  |   - org.springframework:spring-orm:jar:5.3.20:compile
[INFO] |  |   - org.springframework:spring-context:jar:5.3.20:compile
[INFO] |  |  |  \- org.springframework:spring-expression:jar:5.3.20:compile
[INFO] |  |   - org.springframework:spring-tx:jar:5.3.20:compile
[INFO] |  |   - org.springframework:spring-beans:jar:5.3.20:compile
[INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.36:compile
[INFO] |  \- org.springframework:spring-aspects:jar:5.3.20:compile
[INFO]  - com.example:theirlib:jar:1.0:compile  // jar with dependencies
[INFO]  - org.postgresql:postgresql:jar:9.4.1208:compile
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.7.0:test
[INFO]     - org.springframework.boot:spring-boot-starter:jar:2.7.0:compile
[INFO]    |   - org.springframework.boot:spring-boot:jar:2.7.0:compile
[INFO]    |   - org.springframework.boot:spring-boot-autoconfigure:jar:2.7.0:compile
[INFO]    |   - org.springframework.boot:spring-boot-starter-logging:jar:2.7.0:compile
[INFO]    |  |   - ch.qos.logback:logback-classic:jar:1.2.11:compile
[INFO]    |  |  |  \- ch.qos.logback:logback-core:jar:1.2.11:compile
[INFO]    |  |   - org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile
[INFO]    |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO]    |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
[INFO]    |   - jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO]    |  \- org.yaml:snakeyaml:jar:1.30:compile
[INFO]     - org.springframework.boot:spring-boot-test:jar:2.7.0:test
[INFO]     - org.springframework.boot:spring-boot-test-autoconfigure:jar:2.7.0:test
[INFO]     - com.jayway.jsonpath:json-path:jar:2.7.0:test
[INFO]    |  \- net.minidev:json-smart:jar:2.4.8:test
[INFO]    |     \- net.minidev:accessors-smart:jar:2.4.8:test
[INFO]    |        \- org.ow2.asm:asm:jar:9.1:test
[INFO]     - jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:compile
[INFO]    |  \- jakarta.activation:jakarta.activation-api:jar:1.2.2:compile
[INFO]     - org.assertj:assertj-core:jar:3.22.0:test
[INFO]     - org.hamcrest:hamcrest:jar:2.2:test
[INFO]     - org.junit.jupiter:junit-jupiter:jar:5.8.2:test
[INFO]    |   - org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
[INFO]    |  |   - org.opentest4j:opentest4j:jar:1.2.0:test
[INFO]    |  |   - org.junit.platform:junit-platform-commons:jar:1.8.2:test
[INFO]    |  |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO]    |   - org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test
[INFO]    |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test
[INFO]    |     \- org.junit.platform:junit-platform-engine:jar:1.8.2:test
[INFO]     - org.mockito:mockito-core:jar:4.5.1:test
[INFO]    |   - net.bytebuddy:byte-buddy-agent:jar:1.12.10:test
[INFO]    |  \- org.objenesis:objenesis:jar:3.2:test
[INFO]     - org.mockito:mockito-junit-jupiter:jar:4.5.1:test
[INFO]     - org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO]    |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO]     - org.springframework:spring-core:jar:5.3.20:compile
[INFO]    |  \- org.springframework:spring-jcl:jar:5.3.20:compile
[INFO]     - org.springframework:spring-test:jar:5.3.20:test
[INFO]    \- org.xmlunit:xmlunit-core:jar:2.9.0:test

And now try many combinations of exclusions:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.crush.sample</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jul-to-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>theirlib</artifactId>
            <version>1.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
                <version>9.4.1208</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

What's the best way to solve this?

CodePudding user response:

if you are using intellij , a tool which i often use is enter image description here

P.S you must make sure "thierlibrary" was not shaded , otherwise you cannot solve it with normal maven exclusions

CodePudding user response:

I resolved this by looking at the exploded bundled jar and brute forcing all the dependencies into my project pom and using the standalone jar. Everything is happy for now..

  • Related