如何在sbt-assembly jar中包含测试依赖项?

时间:2016-10-21 20:02:28

标签: scala sbt sbt-assembly

我无法在测试程序集jar中打包测试依赖项。以下是我build.sbt的摘录:

...

name := "project"

scalaVersion := "2.10.6"

assemblyOption in (Compile, assembly) := (assemblyOption in (Compile, assembly)).value.copy(includeScala = false)

fork in Test := true

parallelExecution in IntegrationTest := false

lazy val root = project.in(file(".")).configs(IntegrationTest.extend(Test)).settings(Defaults.itSettings: _ *)

Project.inConfig(Test)(baseAssemblySettings)

test in (Test, assembly) := {}

assemblyOption in (Test, assembly) := (assemblyOption in (Test, assembly)).value.copy(includeScala = false, includeDependency = true)

assemblyJarName in (Test, assembly) := s"${name.value}-test.jar"

fullClasspath in (Test, assembly) := {
  val cp = (fullClasspath in Test).value
  cp.filter{ file => (file.data.name contains "classes") || (file.data.name contains "test-classes")}  ++  (fullClasspath in Runtime).value
}

libraryDependencies ++= Seq(
  ...
  "com.typesafe.play" %% "play-json" % "2.3.10" % "test" excludeAll ExclusionRule(organization = "joda-time"),
  ...
)

...

当我使用sbt test:assembly组装我的胖罐时,生成胖罐project-test.jar,但play-json依赖关系未打包在:

$ jar tf /path/to/project-test.jar | grep play
$

但是,如果我从"test" dep(即play-json)删除"com.typesafe.play" %% "play-json" % "2.3.10" excludeAll ExclusionRule(organization = "joda-time")配置,我可以看到它被包含在内:

$ jar tf /path/to/project-test.jar | grep play
...
play/libs/Json.class
...
$

我做错了什么和/或遗失了什么?我的目标是将play-json库仅包含在test:assembly jar中而不包含assembly jar

1 个答案:

答案 0 :(得分:0)

我在上面发布的原始build.sbt摘录中遗漏了一个关键部分,结果证明是问题的原因:

fullClasspath in (Test, assembly) := {
  val cp = (fullClasspath in Test).value
  cp.filter{ file => (file.data.name contains "classes") || (file.data.name contains "test-classes")}  ++  (fullClasspath in Runtime).value
}

此代码块实际上是从测试类路径中过滤掉deps。我们将此包括在内以避免痛苦的合并冲突。我通过添加逻辑来修复此问题,以包含所需的play-json dep:

fullClasspath in (Test, assembly) := {
  val cp = (fullClasspath in Test).value
  cp.filter{ file =>
    (file.data.name contains "classes") ||
    (file.data.name contains "test-classes") ||
    // sorta hacky
    (file.data.name contains "play")
  }  ++  (fullClasspath in Runtime).value
}