gpt4 book ai didi

sbt - 如何将自定义资源目录添加到测试类路径而不复制其中的文件?

转载 作者:行者123 更新时间:2023-12-04 07:00:56 25 4
gpt4 key购买 nike

当我运行测试时,我的特殊资源目录 special-resources 的内容被复制到 target/classes目录。我有这样的事情

unmanagedResourceDirectories in Compile += baseDirectory.value / "special-resources",

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

我试过使用
unmanagedClasspath in Compile += baseDirectory.value / "special-resources",

但是资源不可用。

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

最佳答案

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

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

unmanagedClasspath in (Compile, runMain) += baseDirectory.value / "special-resources"

使用 show 检查设置是否正确:
> show test:unmanagedClasspath
[info] List(Attributed(C:\dev\sandbox\runtime-assembly\special-resources))

通过以下 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)
}
}
}
hello.txtspecial-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

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

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

fork in Test := true

关于sbt - 如何将自定义资源目录添加到测试类路径而不复制其中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25158689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com