首页 > 解决方案 > SLF4j 多重绑定,两个依赖都需要

问题描述

首先,我知道这个问题之前发布过,但没有得到回答。

我收到以下警告:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/mypc/.m2/repository/org/slf4j/slf4j-log4j12/1.7.30/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/mypc/.m2/repository/uk/org/lidalia/slf4j-test/1.2.0/slf4j-test-1.2.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]

现在,我知道为了修复它,我必须删除第二个库,但我需要它进行测试。相关条目pom.xml如下:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M3</version>
            <configuration>
                <runOrder>alphabetical</runOrder>
                <reuseForks>false</reuseForks>
                <forkCount>1</forkCount>
                <argLine>@{argLine} -Dfile.encoding=UTF-8</argLine>
                <classpathDependencyExcludes>
                    <classpathDependencyExcludes>org.slf4j:slf4j-log4j12</classpathDependencyExcludes>
                </classpathDependencyExcludes>
            </configuration>
        </plugin>

...

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

    <dependency>
        <groupId>uk.org.lidalia</groupId>
        <artifactId>slf4j-test</artifactId>
        <version>1.2.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

运行测试时不会出现此异常,仅在运行应用程序时才会出现。我运行批处理作业,而不是服务器,所以每次我开始作业时都会出现。在这里提出了同样的问题,但已经快 3 年了,没有提供解决方案。类似的问题是here,但没有解决方案。

排除什么都不做,slf4j 找到的绑定类是slf4j-testjar 的一部分,它不是依赖项。我想要么从类路径中忽略这个包,因为它只用于测试,要么在没有它的情况下编译包。我知道这并不理想,但在我找到永久解决方案之前,我需要一个临时(希望)的解决方案。

任何想法将不胜感激。

标签: javamavenclasspathpom.xmlslf4j

解决方案


推荐阅读