How to transitive slf4j-log4j dependency?

56 Views Asked by At

I am using java for application development and want to write Unit test log statement for application. So following https://projects.lidalia.org.uk/slf4j-test/ this article for same.

Now, when I am trying to use this approach application give me error of multiple dependencies issue in class path for slf4j.

**

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/test/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/test/.m2/repository/com/github/valfirst/slf4j-test/2.0.0/slf4j-test-2.0.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

**

This issue I solved by changing the order of slf4j to the top. Its work fine for me. But I think this is not the best solution. Then googled it and found we can exclude the conflicting jar from classpath for testing. There are multiple jar that pull slf4j dependency transitively.

I tried to exclude them by changing the configuration of pom file but its not working for me. Following is configuration for same. Can someone help me on this?

  <plugins>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>3.1.2</version>
     <configuration>
       <classpathDependencyExcludes>
         <classpathDependencyExcludes>.org.slf4j:slf4j-log4j12</classpathDependencyExcludes>
         <classpathDependencyExcludes>flume-ng-elasticsearch-sink:*:slf4j-log4j12</classpathDependencyExcludes>
       </classpathDependencyExcludes>
       <classpathDependencyScopeExclude>test</classpathDependencyScopeExclude>
     </configuration>
   </plugin>
   </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>org.apache.flume.flume-ng-sinks</groupId>
   <artifactId>flume-ng-elasticsearch-sink</artifactId>
   <version>${apache.flume.version}</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupId>org.apache.flume.flume-ng-sources</groupId>
   <artifactId>flume-kafka-source</artifactId>
   <version>${apache.flume.version}</version>
   <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>com.github.valfirst</groupId>
    <artifactId>slf4j-test</artifactId>
    <version>2.0.0</version>
    <scope>test</scope>
  </dependency>
 </dependencies>
0

There are 0 best solutions below