【问题标题】:How to include test dependencies in sbt-assembly jar?如何在 sbt-assembly jar 中包含测试依赖项?
【发布时间】:2016-10-22 05:44:00
【问题描述】:

我无法将我的测试依赖项打包到我的测试程序集 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 组装我的 fat jar 时,会生成 fat jar project-test.jar,但 play-json 依赖项没有被打包:

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

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

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

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

【问题讨论】:

    标签: scala sbt sbt-assembly


    【解决方案1】:

    我在上面发布的原始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 部门来解决此问题:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 2013-07-25
      • 2015-12-27
      • 2016-05-18
      • 2011-10-23
      • 2015-10-04
      • 1970-01-01
      • 2015-03-05
      • 2017-05-30
      相关资源
      最近更新 更多