SBT插件——编译前执行自定义任务 [英] SBT plugin -- execute custom task before compilation
问题描述
我刚刚编写了我的第一个 SBT Autoplugin,它有一个生成设置文件的自定义任务(如果该文件尚不存在).当显式调用任务时,一切都按预期工作,但我希望在使用插件编译项目之前自动调用它(无需项目修改它的 build.sbt 文件).有没有办法做到这一点,或者我是否需要覆盖 compile
命令?如果是这样,有人能指出我这样做的例子吗?任何帮助将不胜感激!(如果我遗漏了一些简单的东西,我很抱歉!)谢谢!
I've just written my first SBT Autoplugin which has a custom task that generates a settings file (if the file is not already present). Everything works as expected when the task is explicitly invoked, but I'd like to have it automatically invoked prior to compilation of the project using the plugin (without having the project modify it's build.sbt file). Is there a way of accomplishing this, or do I somehow need to override the compile
command? If so, could anyone point me to examples of doing so? Any help would be extremely appreciated! (My apologies if I'm missing something simple!) Thanks!
推荐答案
您可以使用 dependsOn
定义任务之间的依赖关系并覆盖作用域任务的行为(如 compile in Compile
code>) 通过重新分配.
You can define dependencies between tasks with dependsOn
and override the behavior of a scoped task (like compile in Compile
) by reassigning it.
添加到 build.sbt
文件的以下几行可以作为示例:
The following lines added to a build.sbt
file could serve as an example:
lazy val hello = taskKey[Unit]("says hello to everybody :)")
hello := { println("hello, world") }
(compile in Compile) := ((compile in Compile) dependsOn hello).value
现在,每次运行compile
,都会打印hello, world
:
Now, every time you run compile
, hello, world
will be printed:
[IJ]sbt:foo> compile
hello, world
[success] Total time: 0 s, completed May 18, 2018 6:53:05 PM
这个例子已经用 SBT 1.1.5 和 Scala 2.12.6 测试过.
This example has been tested with SBT 1.1.5 and Scala 2.12.6.
这篇关于SBT插件——编译前执行自定义任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!