scala 向内置 SBT 任务添加新的任务依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7344477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding new task dependencies to built-in SBT tasks?
提问by Eemeli Kantola
Is it possible to override or modify built-in SBT tasks (like compile) to depend on custom tasks in my own Build.scala? Overriding e.g. "compile" directly is not possible since it has been defined with lazy val and thus referring to super.compile emits a compiler error "super may be not be used on lazy value".
是否可以覆盖或修改内置 SBT 任务(如编译)以依赖于我自己的 Build.scala 中的自定义任务?直接覆盖例如“编译”是不可能的,因为它已经用惰性 val 定义,因此引用 super.compile 会发出编译器错误“super 可能不能用于惰性值”。
采纳答案by arussell84
Since this question appears when Googling how to add a dependency in SBT, and the current answers are deprecated as of 0.13.xand removed in 1.0, here's the updated answer, assuming that printActionis the task that compileshould depend on:
由于这个问题是在谷歌搜索如何在 SBT 中添加依赖项时出现的,并且当前的答案从 0.13.x 开始被弃用并在 1.0 中被删除,这里是更新的答案,假设这printAction是compile应该依赖的任务:
(Compile / compile) := ((Compile / compile) dependsOn printAction).value
(Compile / compile) := ((Compile / compile) dependsOn printAction).value
回答by James Moore
Update: See arussell84's answer for a modern way to do this
更新:请参阅 arussell84 的答案以了解执行此操作的现代方法
You should be able to do it like this:
你应该可以这样做:
in a .sbt file:
在 .sbt 文件中:
compile <<= (compile in Compile) dependsOn jruby
Where jruby is a task key that you've defined in a project/something.scala file:
其中 jruby 是您在 project/something.scala 文件中定义的任务键:
val jruby = TaskKey[Unit]("jruby", "run a jruby file")
Also, this isn't part of your question but you can just call regular Scala code:
此外,这不是您问题的一部分,但您可以调用常规 Scala 代码:
compile <<= (compile in Compile) map { result =>
println("in compile, something")
result
}
回答by Eemeli Kantola
Reply to self: http://code.google.com/p/simple-build-tool/wiki/ProjectDefinitionExamples#Insert_Task_Dependencytells the answer:
回复自我:http: //code.google.com/p/simple-build-tool/wiki/ProjectDefinitionExamples#Insert_Task_Dependency告诉答案:
If you are using older 0.7.x SBT versions you can do this:
如果您使用的是较旧的 0.7.x SBT 版本,您可以这样做:
import sbt._
class SampleProject(info: ProjectInfo) extends DefaultProject(info) {
lazy val printAction = task { print("Testing...") }
override def compileAction = super.compileAction dependsOn(printAction)
}
回答by Kevin Cao
In the base_dir/project/folder create a file build.sbtand put libraryDependencies += ...there.
在base_dir/project/文件夹中创建一个文件build.sbt并放在libraryDependencies += ...那里。
That's the idiomatic SBT way to build your "build project", also known as "Meta Build".
这是构建“构建项目”(也称为“元构建”)的惯用 SBT 方式。

