2017-10-06 35 views
1

我正在編寫一個包。我需要slf4j-log4j12與特定的appender運行時。但對於測試,我只需要一個slf4j-simple綁定。所以,我pom.xml看起來是這樣的:覆蓋運行時測試的SLF4J綁定

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <properties> 
     <maven.compiler.target>1.8</maven.compiler.target> 
     <maven.compiler.source>1.8</maven.compiler.source> 
    </properties> 

    <groupId>com.bharani</groupId> 
    <artifactId>LoggingDemo</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <dependencies> 
     <!-- Direct dependencies --> 
     <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.7.25</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>1.7.25</version> 
      <scope>runtime</scope> 
     </dependency> 

     <!-- Test dependencies --> 
     <!-- https://mvnrepository.com/artifact/junit/junit --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.12</version> 
      <scope>test</scope> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core --> 
     <dependency> 
      <groupId>org.assertj</groupId> 
      <artifactId>assertj-core</artifactId> 
      <version>3.8.0</version> 
      <scope>test</scope> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-simple</artifactId> 
      <version>1.7.25</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

</project> 

當我運行我的程序,我得到log4j的日誌預期。但是,當我運行測試,我得到:

SLF4J: Class path contains multiple SLF4J bindings. 
SLF4J: Found binding in [jar:file:/Users/bharani/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: Found binding in [jar:file:/Users/bharani/.m2/repository/org/slf4j/slf4j-simple/1.7.25/slf4j-simple-1.7.25.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] 

...我沒有看到日誌在控制檯進出料口slf4j-simple結合。

我該如何解決這個問題? runtime如何依靠slf4j-log4j12遠離測試?

謝謝。

回答

1

的問題是,範圍runtime的依賴性是包括在測試類路徑,所以SLF4J結束有兩個綁定,並且具有接他們中的一個(所述結合它選擇是可能的隨機的,它的發生,它在你的情況下選擇log4j)。

一個簡單的解決辦法是刪除log4j的從Surefire插件的類路徑綁定:在http://maven.apache.org/surefire/maven-surefire-plugin/examples/configuring-classpath.html

<build> 
    <plugins> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.20.1</version> 
     <configuration> 
     <classpathDependencyExcludes> 
      <classpathDependencyExcludes>org.slf4j:slf4j-log4j12</classpathDependencyExcludes> 
     </classpathDependencyExcludes> 
     </configuration> 
    </plugin> 
    </plugins> 
</build> 

更多信息。

+0

做'mvn測試'工作,但在使用Intellij運行測試時仍然不起作用,因爲Maven集成中存在一個公開問題。請參閱https://intellij-support.jetbrains.com/hc/en-us/community/posts/206253879-IDEA-not-honoring-maven-surefire-properties?page=1#community_comment_115000543170和https://youtrack.jetbrains .COM /問題/ IDEA-52286 – bdhar