【问题标题】:sl4j version conflicts in gradle with Storm 1.0.1 and elasticsearch 5.2gradle 中的 sl4j 版本与 Storm 1.0.1 和 elasticsearch 5.2 冲突
【发布时间】:2017-06-28 09:38:39
【问题描述】:

我们在使用 Storm 1.0.1 和 elasticsearch 5.2 的 gradle 中面临 sl4j 版本冲突。

我们发现 ElasticSearch 需要桥接 log4j-to-slf4j 以便我们可以使用所需的记录器。 这里我们尝试将 logback-classic 与 slf4j 一起使用。

依赖定义如下:

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    compile 'org.apache.logging.log4j:log4j-to-slf4j:2.6.2'
    compile 'ch.qos.logback:logback-classic:1.1.10'
    provided ('org.apache.storm:storm-core:1.0.1') {
        exclude(group: 'org.slf4j', module: 'slf4j-api')
    }
    compile 'org.elasticsearch:elasticsearch:5.2.0'
    compile 'org.elasticsearch.client:x-pack-transport:5.2.0'
}

为了解决这个问题,我尝试从 Storm-core 中排除 slf4j 并在稍后添加如下内容:

configurations.all {
    resolutionStrategy {
        eachDependency { DependencyResolveDetails dependencyResolveDetails ->
            final requestedDependency = dependencyResolveDetails.requested
            if (requestedDependency.group == 'org.slf4j' && requestedDependency.name == 'slf4j-api') {
                requestedDependency.setVersion "1.7.7"
            }
         }
    }
}

但是当拓扑提交时我们得到错误: SLF4J:类路径包含多个 SLF4J 绑定。 SLF4J:在 [jar:file:/Users/gauthamr05/Documents/Apps/Storm/apache-storm-1.0.1/lib/log4j-slf4j-impl-2.1.jar!/org/slf4j/impl/StaticLoggerBinder 中找到绑定。班级] SLF4J:在 [jar:file:/Users/gauthamr05/Documents/workspace/xyz_app/build/libs/FullIndexing.jar!/org/slf4j/impl/StaticLoggerBinder.class] 中找到绑定 SLF4J:请参阅http://www.slf4j.org/codes.html#multiple_bindings 了解说明。 SLF4J:实际绑定的类型为 [org.apache.logging.slf4j.Log4jLoggerFactory] 线程“主”java.lang.StackOverflowError 中的异常 在 org.apache.logging.log4j.spi.LoggerRegistry.getOrCreateInnerMap(LoggerRegistry.java:140) 在 org.apache.logging.log4j.spi.LoggerRegistry.hasLogger(LoggerRegistry.java:154) 在 org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:38) 在 org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37) 在 org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29) 在 org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:47) 在 org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29) 在 org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:277)

【问题讨论】:

    标签: gradle apache-storm slf4j elasticsearch-5


    【解决方案1】:

    如异常所述,有两个 jar 包含 StaticLoggerBinder.class

    • /Users/gauthamr05/Documents/Apps/Storm/apache-storm-1.0.1/lib/log4j-slf4j-impl-2.1.jar
    • /Users/gauthamr05/Documents/workspace/xyz_app/build/libs/FullIndexing.jar

    问题:xyz_app/build/libs/FullIndexing.jar 是您自己的项目之一吗?

    如果它是你自己的 jar,我在这里猜测一下,但我猜 org/slf4j/impl/StaticLoggerBinder.java 被打包在你的类路径中的一个 jar 中。 javac 有一个奇怪的默认值,它将编译在类路径的 jar 中找到的任何 .java 文件。这可以通过

    关闭
    compileJava.options.compilerArgs << '-implicit:none'
    

    herehere

    【讨论】:

      【解决方案2】:

      找到了一种在最终 jar 中隐藏 log4j 类的方法。

      以下是配置:

      apply plugin: 'com.github.johnrengelman.shadow'
      
      subprojects {
          shadowJar
      }
      
      dependencies {
          compile 'org.slf4j:slf4j-api:1.7.22'
          compile 'ch.qos.logback:logback-classic:1.1.10'
      
          compileOnly("org.apache.storm:storm-core:1.0.1") {
              exclude module: "log4j-slf4j-impl"
              exclude module: "slf4j-api"
              exclude module: "log4j-to-slf4j"
          }
      
          // ElasticSearch and X-Pack
          compile 'org.elasticsearch:elasticsearch:5.2.0'
          compile 'org.elasticsearch.client:x-pack-transport:5.2.0'
      
          compile 'org.apache.logging.log4j:log4j-api:2.7'
          compile 'org.apache.logging.log4j:log4j-core:2.7'
      }
      
      shadowJar {
          relocate 'org.apache.logging.log4j', 'gautham.elasticsearch.org.apache.logging.log4j'
      
          zip64 true
          transform(ServiceFileTransformer) {
              path = 'META-INF/vesta*'
          }
      
          manifest {
              attributes 'Implementation-Title': 'Storm Topology',
                  'Implementation-Version': 1.0,
                  'Main-Class': 'com.gautham.topology.StormTopology'
          }
      
          baseName = 'StormTopology'
      
          mergeServiceFiles()
      
          exclude "META-INF/*.SF"
          exclude 'META-INF/*.DSA'
          exclude 'META-INF/*.RSA'
          exclude "LICENSE*"
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-03
        • 1970-01-01
        • 2020-11-13
        • 2018-05-14
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        相关资源
        最近更新 更多