如何添加自定义资源目录来测试类路径而不复制里面的文件?

fom*_*mil 8 sbt

当我运行我的测试时,我的特殊资源目录的内容special-resources被复制到该target/classes目录.我有类似的东西

unmanagedResourceDirectories in Compile += baseDirectory.value / "special-resources",
Run Code Online (Sandbox Code Playgroud)

但我不想将这些资源复制到target目录中,但我希望它们位于分叉java进程的类路径上(例如用于测试).

我试过用

unmanagedClasspath in Compile += baseDirectory.value / "special-resources",
Run Code Online (Sandbox Code Playgroud)

但资源不可用.

如何在不必复制文件的情况下将资源目录添加到类路径?或者,或者,如何设置sbt以不将资源复制到目标目录?

Jac*_*ski 10

要使special-resources测试和runMain任务的类路径中包含目录的内容,请执行以下操作:

unmanagedClasspath in Test += baseDirectory.value / "special-resources"

unmanagedClasspath in (Compile, runMain) += baseDirectory.value / "special-resources"
Run Code Online (Sandbox Code Playgroud)

检查设置是否正确设置show:

> show test:unmanagedClasspath
[info] List(Attributed(C:\dev\sandbox\runtime-assembly\special-resources))
Run Code Online (Sandbox Code Playgroud)

通过以下Specs2测试,我确信设置工作正常:

import org.specs2.mutable._

class HelloWorldSpec extends Specification {

  "Hello world" should {
    "find the file on classpath" in {
      val text = io.Source.fromInputStream(getClass.getResourceAsStream("hello.txt")).mkString
      text must have size(11)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

hello.txt在里面special-resources有一个hello world字符串的目录中.

> ; clean ; test
[success] Total time: 0 s, completed 2014-08-06 20:00:02
[info] Updating {file:/C:/dev/sandbox/runtime-assembly/}runtime-assembly...
[info] Resolving org.jacoco#org.jacoco.agent;0.7.1.201405082137 ...
[info] Done updating.
[info] Compiling 1 Scala source to C:\dev\sandbox\runtime-assembly\target\scala-2.10\test-classes...
[info] HelloWorldSpec
[info]
[info] Hello world should
[info] + find the file on classpath
[info]
[info] Total for specification HelloWorldSpec
[info] Finished in 17 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
Run Code Online (Sandbox Code Playgroud)

而且build.sbt:

unmanagedClasspath in Test += baseDirectory.value / "special-resources"

libraryDependencies += "org.specs2" %% "specs2" % "2.4" % "test"

fork in Test := true
Run Code Online (Sandbox Code Playgroud)

  • 是的,但这仍然无法解释为什么配置中的设置有时会起作用,有时它需要在任务上.我想也许任务并不总是从其范围继承设置(或者在范围上没有定义设置).但是,由于在文档中没有特别明确,所以必须解决这些问题是非常烦人的. (2认同)