SBT 编译后应用任务 [英] SBT apply Task after Compile

查看:27
本文介绍了SBT 编译后应用任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在编译前自动执行任务:

I am able to automatically execute a task before compilation with:

compile in Compile <<= (compile in Compile).dependsOn(myTask)

编译之后我该怎么做?

我知道我能做到:

compile in Compile <<= (compile in Compile) map{x=>
  // post-compile work
  doFoo()
  x
}

执行任意Scala代码,但我需要在编译事件发生时自动执行目标任务本身

to execute arbitrary Scala code, but I need to automatically execute the target task itself when a compile event occurs

做类似的事情:

val foo = TaskKey[Unit]("foo", "...")
val fooTask = foo <<= scalaInstance map {si =>
...
} dependsOn(compile in Compile)

如果我在 sbt> 提示符下输入foo"就可以工作;即编译后执行任务,但目标是挂钩到编译任务本身,所以每当编译发生时,编译完成后会自动调用 foo 任务.

works if I type "foo" from sbt> prompt; i.e. task is executed after compile, but the goal is to hook into compile task itself, so anytime a compilation occurs, the foo task is automatically called after compilation completes.

这可能吗,还是我以错误的方式处理内置编译任务?

Is this possible, or am I going about things in the wrong way to hook into the built-in compile task?

推荐答案

一般来说,有一些任务取决于你的任务.

In general, there is some task that depends on your task.

如果 compile 被用来表示为 Grunt 编译和设置",那么创建一个依赖于 compileprepareGrunt 任务和 myTask 并运行它.

If compile is being used to mean "compile and set things up for Grunt", then create a prepareGrunt task that depends on compile and myTask and run that instead.

如果 myTask 应该在项目的类和资源被其他东西使用之前运行,则使其成为 exportedProducts 的依赖项.runtest 等任务以及依赖项目中的任务将从该任务中获取导出的类路径条目.

If myTask should run before the project's classes and resources are used by something else, then make it a dependency of exportedProducts. Tasks like run and test and tasks in dependent projects will get the exported classpath entries from that task.

在编译后运行"的危险在于 myTask 不会在实际需要它的任务之前运行.Initialize[Task[T]] 上有 triggeredBy 方法,但是很容易被滥用,应该只在已知任务的输出只能使用时使用在所有任务执行之后.

The danger in "run sometime after compile" is that myTask won't be run before the task that actually needs it. There is the triggeredBy method on Initialize[Task[T]], but it is easily abused and should only be used when the output of the task is known to be used only after all tasks execute.

这篇关于SBT 编译后应用任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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