玩 2.3.8 sbt 不包括 logback

Posted

技术标签:

【中文标题】玩 2.3.8 sbt 不包括 logback【英文标题】:play 2.3.8 sbt excluding logback 【发布时间】:2015-06-14 14:01:41 【问题描述】:

我很难将 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]

我已经束手无策了。帮助。

【问题讨论】:

【参考方案1】:

我发现将排除链接到添加 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。您可以排除 plugin.sbt 中的库,但如果您使用的是 enablePlugins(PlayScala)(https://www.playframework.com/documentation/2.5.x/NewApplication) 的 PlayScala 插件,则无法排除,因此您必须单独添加排除项。

【讨论】:

感谢您的回答。我暂时没有时间验证正确性..【参考方案2】:

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

将此添加到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
  )
)

【讨论】:

我已经将插件从0.7.4升级到0.7.5,仍然得到[error] Not a valid command: whatDependsOn @kumetix 像这样使用命令:sbt 'whatDependsOn ch.qos.logback logback-classic 1.1.1' 它没有很好的错误消息,但它确实有效 @flavian 这仍然不排除 logback-classic

以上是关于玩 2.3.8 sbt 不包括 logback的主要内容,如果未能解决你的问题,请参考以下文章

SBT/Play2 多项目设置在运行/测试的类路径中不包括依赖项目

SBT包括程序中的版本号

关于scala工程结构(使用sbt)

如何告诉 sbt-proguard 包含 java *.jars?

如何使用 SBT 为库构建分层 JAR 文件?

在 Play Framework 2.3.8 中使用 SORM