7

我很难logback从我的 play 2.3.8 测试运行中排除。我尝试了许多排除规则,但似乎没有任何效果。我也无法在我的依赖树中找到它。我的 sbt 文件中的片段:

[...]
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"))
}

由于某种原因,它不在依赖关系树中:

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

然而,当我运行测试时,它出现了!

$ 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]

我无计可施。帮助。

4

2 回答 2

0

我发现将排除链接到添加 libraryDependencies 不起作用,即

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"))
}

相反,在添加新依赖项后,使用未记录的SettingKey.~=函数 ( http://www.scala-sbt.org/0.13.12/api/index.html#sbt.SettingKey ) 在以下行添加排除项,即

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"))
}

我不知道为什么这会产生不同的行为,但是使用 Play Framework 2.4.3 和 SBT 0.13.8 这成功地排除了 logback-classic 和 slf4j。

请注意,您可以链接 exclude 和 excludeAll 方法调用以避免重复调用map,因此您的代码可以简化为:

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"))
}

编辑:经过进一步调查,我认为这是必需的,因为 libraryDependencies 在解析 build.sbt 文件之前已经包含来自 Play 插件的 logback-classic。enablePlugins(PlayScala)您可以排除 plugin.sbt 中的库,但如果您通过( https://www.playframework.com/documentation/2.5.x/NewApplication )使用 PlayScala 插件,则无法排除此内容,因此您必须单独添加排除项.

于 2017-01-13T02:59:54.057 回答
-1

有更好的方法来做到这一点。首先,您应该首先确定导致问题的依赖项。

将此添加到plugins.sbt

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

现在您在 sbt 中有一个可用的新命令,即whatDependsOn. 使用它,您可以试用:

whatDependsOn ch.qos.logback logback-classic 1.1.1

然后我建议从确切的位置删除依赖项,而不是映射整个配置:

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
  )
)
于 2015-09-03T21:36:22.510 回答