玩2.3.8 sbt不包括logb​​ack

时间:2015-04-09 09:20:32

标签: sbt playframework-2.3 phantom-dsl

我很难将logback从我的游戏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]

我的智慧结束了。帮助

2 个答案:

答案 0 :(得分:0)

我发现链接添加库依赖项的排除不起作用,即

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。

请注意,您可以链接排除和排除所有方法调用,以避免重复调用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"))
}

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

答案 1 :(得分:-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
  )
)