使用dropwizard的阴影jar我得到:类路径包含多个SLF4J绑定

Kar*_*ell 5 logback slf4j dropwizard

我可能在这里做了些蠢事.我的dropwizard设置有一些(小的)错误.运行着色jar工作正常,但在执行集成测试时,我收到此警告:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/graphhopper/web/target/graphhopper-web-0.11-SNAPSHOT.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/user/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.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 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
INFO  [2018-04-08 18:44:27,653] org.eclipse.jetty.util.log: Logging initialized @1090ms to org.eclipse.jetty.util.log.Slf4jLog
Run Code Online (Sandbox Code Playgroud)

graphhopper-web-0.11-SNAPSHOT.jar是带有dropwizard(带有logback)的阴影jar.

通常意味着类路径上不止一个slf4j绑定,但我可以拒绝这个理论,只有slf4j-api存在,加上dropwizard的logback依赖.我还用Netbeans和.分析了依赖图

mvn dependency:tree -Dverbose -Dincludes=org.slf4j

(见这里的输出)但找不到有问题的东西.

是否可以将阴影jar(带有logback)以某种方式与其他jar(包括logb​​ack)一起放入classpath中mvn clean install?我怎么能避免这个?

通过以下方式重现:

git clone https://github.com/graphhopper/graphhopper
cd web
mvn clean install
Run Code Online (Sandbox Code Playgroud)

看到这个问题.

Tar*_*ani 2

问题是 jarlogback-classic-1.2.3.jar具有实际文件/org/slf4j/impl/StaticLoggerBinder.class,因此 jar 排除不起作用。

您需要configuration更改maven-shade-plugin

<configuration>
    <createDependencyReducedPom>true</createDependencyReducedPom>
    <filters>
        <filter>
          <artifact>ch.qos.logback:logback-classic</artifact>
          <excludes>
            <exclude>org/slf4j/impl/**</exclude>
          </excludes>
        </filter>
        <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
        </filter>
    </filters>
</configuration>
Run Code Online (Sandbox Code Playgroud)

一旦你这样做了,一切都会好起来,警告就会消失

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.graphhopper:graphhopper-web >-------------------
[INFO] Building GraphHopper Web 0.11-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ graphhopper-web ---
[INFO] Deleting /Users/tarun.lalwani/Desktop/tarunlalwani.com/tarunlalwani/workshop/ub16/so/graphhopper/web/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ graphhopper-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 118 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ graphhopper-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 19 source files to /Users/tarun.lalwani/Desktop/tarunlalwani.com/tarunlalwani/workshop/ub16/so/graphhopper/web/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ graphhopper-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ graphhopper-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to /Users/tarun.lalwani/Desktop/tarunlalwani.com/tarunlalwani/workshop/ub16/so/graphhopper/web/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ graphhopper-web ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.graphhopper.http.WebHelperTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.151 sec - in com.graphhopper.http.WebHelperTest
Running com.graphhopper.http.IPFilterTest
20:29:46.762 [main] DEBUG com.graphhopper.http.IPFilter - whitelist:[4.5.67.1, 1.2.3.4]
20:29:46.765 [main] DEBUG com.graphhopper.http.IPFilter - blacklist:[8.9.7.3]
20:29:46.765 [main] DEBUG com.graphhopper.http.IPFilter - blacklist:[4.5.67.1, 1.2.3.4]
20:29:46.766 [main] DEBUG com.graphhopper.http.IPFilter - whitelist:[4.5.67.1, 1.2.3.4]
20:29:46.766 [main] DEBUG com.graphhopper.http.IPFilter - whitelist:[1.2.3*, 4.5.67.1, 7.8.*.3]
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec - in com.graphhopper.http.IPFilterTest

Results :

Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
Run Code Online (Sandbox Code Playgroud)

编辑 1:2018 年 4 月 26 日

来自https://github.com/graphhopper/graphhopper/issues/1328的更新

我认为问题在于您在构建的验证阶段运行 Dropwizard 应用程序集成测试作为集成测试。该阶段在打包阶段之后调用,在打包阶段创建一个着色 jar 并用它替换原始 jar。

[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /home/travis/build/graphhopper/graphhopper/web/target/graphhopper-web-0.11-SNAPSHOT.jar with /home/travis/build/graphhopper/graphhopper/web/target/graphhopper-web-0.11-SNAPSHOT-shaded.jar
Run Code Online (Sandbox Code Playgroud)

因此,类路径中有两个 jars,带有两个 slf4j 绑定(graphhopper-web-0.11-SNAPSHOT 和 logback-classic-1.2.3.jar)。无需创建阴影 jar 来针对 Dropwizard 应用程序运行集成测试,它可以作为单元测试运行。因此,如果您配置 graphhopper-web 在测试阶段运行其集成测试,则不会出现警告:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <excludes>
                    <exclude>com.graphhopper.http.**</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <argLine>-Xmx100m -Xms100m</argLine>
                <includes>
                    <include>com.graphhopper.http.**</include>
                </includes>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

另一种选择是将它们从 *IT 重命名为 *Test,这样它们就不会自动匹配为集成测试。