如何在sbt中的测试任务之前附加要执行的自定义任务? [英] How to attach custom task to execute before the test task in sbt?
问题描述
我正在将SBT与Play Framework一起使用.
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)
}
到目前为止一切都很好.
So far so good.
我想总是在有人执行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屋!