如何将自定义资源目录添加到测试类路径而不复制其中的文件? [英] How to add custom resource directory to test classpath without copying the files inside?

查看:26
本文介绍了如何将自定义资源目录添加到测试类路径而不复制其中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I run my tests, the contents of my special resources directory special-resources are copied to the target/classes directory. I have something like this

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

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

But I do not want to copy these resources into the target directory, but I want them to be on the classpath of forked java processes (e.g. for testing).

我试过使用

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

但资源不可用.

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

How can I add a resource directory to the classpath without having to copy the files? Or, alternatively, how can I setup sbt to not copy resources to the target directory?

推荐答案

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

To have the contents of the special-resources directory included in the classpath for tests and runMain task, do the following:

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

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

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

Check that the setting is set properly with show:

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

通过以下 Specs2 测试,我确信设置运行良好:

With the following Specs2 tests I'm convinced the setup worked fine:

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.txt 位于 special-resources 目录中,其中包含一个 hello world 字符串.

hello.txt is in the special-resources directory with a hello world string inside.

> ; 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

这篇关于如何将自定义资源目录添加到测试类路径而不复制其中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆