play 2.3.8 sbt excluding logback

时光毁灭记忆、已成空白 提交于 2020-01-14 08:18:10

问题


I'm having a really hard time excluding logback from my play 2.3.8 test run. I've tried many exclude rules, but nothing seems to work. I also can't find it in my dependency tree. Snippet from my sbt file:

[...]
resolvers ++= Seq(
  "Typesafe repository snapshots" at "http://repo.typesafe.com/typesafe/snapshots/",
  "Typesafe repository releases" at "http://repo.typesafe.com/typesafe/releases/",
  "Sonatype repo"                    at "https://oss.sonatype.org/content/groups/scala-tools/",
  "Sonatype releases"                at "https://oss.sonatype.org/content/repositories/releases",
  "Sonatype snapshots"               at "https://oss.sonatype.org/content/repositories/snapshots",
  "Sonatype staging"                 at "http://oss.sonatype.org/content/repositories/staging",
  "Java.net Maven2 Repository"       at "http://download.java.net/maven/2/",
  "Twitter Repository"               at "http://maven.twttr.com",
  "Websudos releases"                at "http://maven.websudos.co.uk/ext-release-local"
)

libraryDependencies ++= {
  val phantomVersion = "1.5.0"
  Seq(
    "net.jpountz.lz4" % "lz4" % "1.3.0",
    "org.xerial.snappy" % "snappy-java" % "1.1.1.6",
    "com.websudos" %% "phantom-dsl" % phantomVersion,
    "com.websudos" %% "phantom-testing" % phantomVersion % Test,
    "org.scalatestplus" %% "play" % "1.2.0" % Test,
    "org.cassandraunit" % "cassandra-unit" % "2.1.3.1" % Test
  ).map(_.exclude("org.slf4j", "slf4j-jdk14"))
   .map(_.excludeAll(ExclusionRule(organization = "ch.qos.logback")))
   .map(_.excludeAll(ExclusionRule(organization = "QOS.ch")))
   .map(_.excludeAll(ExclusionRule(artifact = "logback*")))
   .map(_.excludeAll(ExclusionRule(artifact = "logback-classic")))
   .map(_.exclude("ch.qos.logback", "logback-parent"))
   .map(_.exclude("ch.qos.logback", "logback-core"))
   .map(_.exclude("QOS.ch", "logback-parent"))
   .map(_.exclude("", "logback-classic"))
}

It's not in the dependency tree for some reason:

$ activator "inspect tree test" |grep -i qos |wc -l
   0
$ activator "inspect tree test" |grep -i logback |wc -l
   0

Yet, when I run the test, it shows up!

$ activator test
[...]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/X/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.1.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/X/.ivy2/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.6.1.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]

I'm at my wits' end. Help.


回答1:


I have found that chaining the exclusions to the addition of libraryDependencies does not work, i.e.

libraryDependencies ++= {
  val phantomVersion = "1.5.0"
  Seq(
    "net.jpountz.lz4" % "lz4" % "1.3.0",
    "org.xerial.snappy" % "snappy-java" % "1.1.1.6",
    "com.websudos" %% "phantom-dsl" % phantomVersion,
    "com.websudos" %% "phantom-testing" % phantomVersion % Test,
    "org.scalatestplus" %% "play" % "1.2.0" % Test,
    "org.cassandraunit" % "cassandra-unit" % "2.1.3.1" % Test
  ).map(_.exclude("org.slf4j", "slf4j-jdk14"))
   .map(_.excludeAll(ExclusionRule(organization = "ch.qos.logback")))
   .map(_.excludeAll(ExclusionRule(organization = "QOS.ch")))
   .map(_.excludeAll(ExclusionRule(artifact = "logback*")))
   .map(_.excludeAll(ExclusionRule(artifact = "logback-classic")))
   .map(_.exclude("ch.qos.logback", "logback-parent"))
   .map(_.exclude("ch.qos.logback", "logback-core"))
   .map(_.exclude("QOS.ch", "logback-parent"))
   .map(_.exclude("", "logback-classic"))
}

Instead, add the exclusions using the undocumented SettingKey.~= function (http://www.scala-sbt.org/0.13.12/api/index.html#sbt.SettingKey) on the following line after adding new dependencies, i.e.

libraryDependencies ++= {
  val phantomVersion = "1.5.0"
  Seq(
    "net.jpountz.lz4" % "lz4" % "1.3.0",
    "org.xerial.snappy" % "snappy-java" % "1.1.1.6",
    "com.websudos" %% "phantom-dsl" % phantomVersion,
    "com.websudos" %% "phantom-testing" % phantomVersion % Test,
    "org.scalatestplus" %% "play" % "1.2.0" % Test,
    "org.cassandraunit" % "cassandra-unit" % "2.1.3.1" % Test
  )

libraryDependencies ~= { _.map(_.exclude("org.slf4j", "slf4j-jdk14"))
   .map(_.excludeAll(ExclusionRule(organization = "ch.qos.logback")))
   .map(_.excludeAll(ExclusionRule(organization = "QOS.ch")))
   .map(_.excludeAll(ExclusionRule(artifact = "logback*")))
   .map(_.excludeAll(ExclusionRule(artifact = "logback-classic")))
   .map(_.exclude("ch.qos.logback", "logback-parent"))
   .map(_.exclude("ch.qos.logback", "logback-core"))
   .map(_.exclude("QOS.ch", "logback-parent"))
   .map(_.exclude("", "logback-classic"))
}

I don't know why this gives different behaviour, but with Play Framework 2.4.3 and SBT 0.13.8 this successfully excludes logback-classic and slf4j.

Note that you can chain exclude and excludeAll method calls to avoid repeatedly calling map so your code can be simplified to:

libraryDependencies ++= {
  val phantomVersion = "1.5.0"
  Seq(
    "net.jpountz.lz4" % "lz4" % "1.3.0",
    "org.xerial.snappy" % "snappy-java" % "1.1.1.6",
    "com.websudos" %% "phantom-dsl" % phantomVersion,
    "com.websudos" %% "phantom-testing" % phantomVersion % Test,
    "org.scalatestplus" %% "play" % "1.2.0" % Test,
    "org.cassandraunit" % "cassandra-unit" % "2.1.3.1" % Test
  )

libraryDependencies ~= { _.map(_
  .exclude("org.slf4j", "slf4j-jdk14"))
  .exclude("ch.qos.logback", "logback-classic"))
}

Edit: After further investigation, I believe that this is required because the libraryDependencies already contain logback-classic from the Play plugin prior to parsing the build.sbt file. You can exclude the libraries in plugin.sbt, but if you are using the PlayScala plugin by enablePlugins(PlayScala)(https://www.playframework.com/documentation/2.5.x/NewApplication) this cannot be excluded so you have to add the exclusions separately.




回答2:


There is a better way to do this. First you should identify which dependency is causing the problem in the first place.

Add this to plugins.sbt:

addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.7.5")

Now you have a new command available in sbt, namely whatDependsOn. Using this, you can trial:

whatDependsOn ch.qos.logback logback-classic 1.1.1

Then I would suggest removing the dependency from the exact place, not mapping the entire configuration:

libraryDependencies ++= {
  val phantomVersion = "1.5.0"
  Seq(
    "net.jpountz.lz4" % "lz4" % "1.3.0",
    "org.xerial.snappy" % "snappy-java" % "1.1.1.6",
    "com.websudos" %% "phantom-dsl" % phantomVersion excludeAll(
      ExclusionRule(...)
    ),
    "com.websudos" %% "phantom-testing" % phantomVersion % Test,
    "org.scalatestplus" %% "play" % "1.2.0" % Test,
    "org.cassandraunit" % "cassandra-unit" % "2.1.3.1" % Test
  )
)


来源:https://stackoverflow.com/questions/29534528/play-2-3-8-sbt-excluding-logback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!