如何在 sbt 中的测试任务之前附加自定义任务以执行? [英] How to attach custom task to execute before the test task in sbt?
问题描述
我正在将 SBT 与 Play 框架一起使用.
I'm using SBT with Play Framework.
我创建了一个自定义 TaskKey
来在我的项目中运行 JavaScript 测试:
I created a custom TaskKey
to run JavaScript tests in my project:
import sbt._
import sbt.Process._
import PlayProject._
object ApplicationBuild extends Build {
val testJsTask = TaskKey[Unit]("testJs", "Run javascript tests.") := {}
val main = PlayProject("xxx", 1.0, Seq())
.settings(defaultScalaSettings: _*)
.settings(testJsTask)
}
到目前为止一切顺利.
我想总是在有人执行 test
任务时运行这个 testJsTask
.
I want to run this testJsTask
always when someone executes the test
task.
我猜应该是这样的:
test in Test <<= (test in Test).dependsOn(testJsTask)
我不知道应该如何准确定义它.如何将依赖项添加到现有任务,如测试"或构建"?
I've no idea how it should be defined exactly. How to add a dependency to an existing task like 'test' or 'build'?
更新
@Christian 提出更改后,构建定义如下所示:
After changes proposed by @Christian the build definition looks as follows:
object ApplicationBuild extends Build {
val testJsTask = TaskKey[Unit]("testJs", "Run tests for javascript client.")
def testJs = {}
val main = PlayProject("xxx", 1.0, Seq())
.settings(defaultScalaSettings: _*)
.settings(testJsTask := testJs)
(test in Test) <<= (test in Test) dependsOn (testJs)
}
不幸的是,该解决方案也不起作用:
Unfortunately, the solution doesn't work either:
[error] /xxx/project/Build.scala:21: not found: value test
[error] (test in Test) <<= (test in Test) dependsOn (testJs)
[error] ^
[error] one error found
[error] {file:/xxx/project/}default-f468ae/compile:compile: Compilation failed
推荐答案
这是一种方法:
定义任务键:
val testJsTask = TaskKey[Unit]("testJs", "Run javascript tests.")
在您的项目设置中定义任务:
Define the task in your projects settings:
testJsTask <<= testJs
使测试依赖于它:
(test in Test) <<= (test in Test) dependsOn (testJs)
testJs 可以定义如下:
testJs can be defined as follows:
def testJs = (streams) map { (s) => {
s.log.info("Executing task testJs")
// Your implementation
}
您必须在项目设置中定义任务依赖项.为了一个正常"的项目,你可以这样做:
You have to define the task dependencies within the projects settings. For a "normal" project, you would do it the following way:
lazy val testProject = Project(
"testProject",
file("testProject"),
settings = defaultSettings ++ Seq(
testJsTask <<= testJs,
(test in Test) <<= (test in Test) dependsOn (testJsTask)
)
)
这篇关于如何在 sbt 中的测试任务之前附加自定义任务以执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!