Scala SBT Assembly cannot merge due to de-duplication error in StaticLoggerBinder.class

后端 未结 2 544
再見小時候
再見小時候 2021-02-08 12:48

My problem is I can no longer use the sbt-assembly plugin because some kind of dependency merge problem creeped in, between a couple people working on this project.

The

相关标签:
2条回答
  • 2021-02-08 13:18

    The issue is that more than one dependency brings in slf4j transitive dependency which causes a conflict. The best way is to exclude that transitive dependency instead of using merging strategy of SBT assembly to resolve the conflict.

    First you need to visualize where slf4j comes from (for all top level dependencies). You can use a tool like this sbt-dependency-graph to get a visual diagram of all dependencies. Alternatively you can explore SBT settings from SBT command prompt or use a maven site like this one or this one.

    Once you locate duplicate dependencies use exclude the same way as you use in your build file. Leave only single slf4j transitive dependency. You can also exclude all slf4j transitive dependencies (see here how) and manually add that slf4j dependency at top level.

    Note that if your dependencies require multiple different versions of slf4j it might be problematic to pick a single version that works for all cases. Depends on binary compatibility.

    0 讨论(0)
  • 2021-02-08 13:34

    Previous answer gives all the required feedback, but does not actually give any solution or steps to try out.

    My error:

    [error] (project/*:assembly) deduplicate: different file contents found in the following:
    [error] /home/user/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.2.3.jar:org/slf4j/impl/StaticLoggerBinder.class
    [error] /home/user/.ivy2/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.7.10.jar:org/slf4j/impl/StaticLoggerBinder.class
    [error] deduplicate: different file contents found in the following:
    [error] /home/user/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.2.3.jar:org/slf4j/impl/StaticMDCBinder.class
    [error] /home/user/.ivy2/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.7.10.jar:org/slf4j/impl/StaticMDCBinder.class
    [error] deduplicate: different file contents found in the following:
    [error] /home/user/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.2.3.jar:org/slf4j/impl/StaticMarkerBinder.class
    [error] /home/user/.ivy2/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.7.10.jar:org/slf4j/impl/StaticMarkerBinder.class
    

    Recommended way

    track down conflicting dependency. For example, I do not want those classes:

    org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.7.10.jar:org/slf4j/impl/StaticLoggerBinder.class
    org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.7.10.jar:org/slf4j/impl/StaticMDCBinder.class
    org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.7.10.jar:org/slf4j/impl/StaticMarkerBinder.class
    

    Call sbt (and make sure to use sbt-dependency-graph plugin):

    whatDependsOn org.slf4j slf4j-log4j12 1.7.10
    

    Returns a list:

    [info] org.slf4j:slf4j-log4j12:1.7.10
    [info]   +-org.apache.hadoop:hadoop-auth:2.8.0
    [info]   | +-org.apache.hadoop:hadoop-common:2.8.0
    [info]   |   +-com.github.atais:test_2.11:0.0.3 [S]
    [info]   |   
    [info]   +-org.apache.hadoop:hadoop-common:2.8.0
    [info]   | +com.github.atais:test_2.11:0.0.3 [S]
    [info]   | 
    [info]   +-org.apache.zookeeper:zookeeper:3.4.6
    [info]     +-org.apache.curator:curator-client:2.7.1
    [info]     | +-org.apache.curator:curator-framework:2.7.1
    [info]     | | +-org.apache.curator:curator-recipes:2.7.1
    [info]     | | | +-org.apache.hadoop:hadoop-common:2.8.0
    [info]     | | |   +-com.github.atais:test_2.11:0.0.3 [S]
    [info]     | | |   
    [info]     | | +-org.apache.hadoop:hadoop-auth:2.8.0
    [info]     | |   +-org.apache.hadoop:hadoop-common:2.8.0
    [info]     | |     +-com.github.atais:test_2.11:0.0.3 [S]
    [info]     | |     
    [info]     | +-org.apache.hadoop:hadoop-common:2.8.0
    [info]     |   +-com.github.atais:test_2.11:0.0.3 [S]
    [info]     |   
    [info]     +-org.apache.curator:curator-framework:2.7.1
    [info]     | +-org.apache.curator:curator-recipes:2.7.1
    [info]     | | +-org.apache.hadoop:hadoop-common:2.8.0
    [info]     | |   +-com.github.atais:test_2.11:0.0.3 [S]
    [info]     | |   
    [info]     | +-org.apache.hadoop:hadoop-auth:2.8.0
    [info]     |   +-org.apache.hadoop:hadoop-common:2.8.0
    [info]     |     +-com.github.atais:test_2.11:0.0.3 [S]
    [info]     |     
    [info]     +-org.apache.curator:curator-recipes:2.7.1
    [info]     | +-org.apache.hadoop:hadoop-common:2.8.0
    [info]     |   +-com.github.atais:test_2.11:0.0.3 [S]
    [info]     |   
    [info]     +-org.apache.hadoop:hadoop-auth:2.8.0
    [info]     | +-org.apache.hadoop:hadoop-common:2.8.0
    [info]     |   +-com.github.atais:test_2.11:0.0.3 [S]
    [info]     |   
    [info]     +-org.apache.hadoop:hadoop-common:2.8.0
    [info]       +-com.github.atais:test_2.11:0.0.3 [S]
    

    I need to track down hadoop-common dependency since it is the one that connects com.github.atais:test_2.11:0.0.3 to the unwanted org.slf4j:slf4j-log4j12:1.7.10

    And modify it:

    libraryDependencies += "org.apache.hadoop" % "hadoop-common" % "2.8.1" exclude("org.slf4j", "slf4j-log4j12")
    

    Easy way

    simply add the merging strategy flag for conflicting paths:

    assemblyMergeStrategy in assembly := {
      ...
      case PathList("org", "slf4j", xs@_*) => MergeStrategy.first
      case x => (assemblyMergeStrategy in assembly).value(x)
    }
    
    0 讨论(0)
提交回复
热议问题