在标准任务之前/之后自动运行自定义任务 [英] Run custom task automatically before/after standard task

查看:51
本文介绍了在标准任务之前/之后自动运行自定义任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常想在运行其中一项标准任务之前进行一些自定义.我意识到我可以按照我想要的顺序创建执行现有任务的新任务,但我发现这很麻烦,而且开发人员错过他应该运行 my-compile 而不是 compile 的机会很大,并且导致难以修复错误.

I often want to do some customization before one of the standard tasks are run. I realize I can make new tasks that executes existing tasks in the order I want, but I find that cumbersome and the chance that a developer misses that he is supposed to run my-compile instead of compile is big and leads to hard to fix errors.

所以我想定义一个自定义任务(比如prepare-app)并将其注入现有任务的依赖树(比如package-bin),以便每次有人调用 package-bin 时,我的自定义任务都会在它之前运行.

So I want to define a custom task (say prepare-app) and inject it into the dependency tree of the existing tasks (say package-bin) so that every time someone invokes package-bin my custom tasks is run right before it.

我试过这样做

  def mySettings = {
    inConfig(Compile)(Seq(prepareAppTask <<= packageBin in Compile map { (pkg: File) =>
      // fiddle with the /target folder before package-bin makes it into a jar
    })) ++
    Seq(name := "my project", version := "1.0")
  }

  lazy val prepareAppTask = TaskKey[Unit]("prepare-app")

但它不会在将编译输出打包到 jar 之前由 package-bin 自动执行.那么如何更改上面的代码以在正确的时间运行?

but it's not executed automatically by package-bin right before it packages the compile output into a jar. So how do I alter the above code to be run at the right time ?

更一般地说,我在哪里可以找到有关挂钩到其他任务(如编译)的信息,以及是否有一种通用方法可以确保您自己的任务在调用标准任务之前和之后运行?.

More generally where do I find info about hooking into other tasks like compile and is there a general way to ensure that your own tasks are run before and after a standard tasks are invoked ?.

推荐答案

扩展现有任务记录在 任务(查看部分 修改现有任务).

Extending an existing task is documented the SBT documentation for Tasks (look at the section Modifying an Existing Task).

像这样:

compile in Compile <<= (compile in Compile) map { _ => 
  // what you want to happen after compile goes here 
}

其实还有另外一种方式——定义你的任务依赖于编译

Actually, there is another way - define your task to depend on compile

prepareAppTask := (whatever you want to do) dependsOn compile

然后修改 packageBin 以依赖它:

and then modify packageBin to depend on that:

packageBin <<= packageBin dependsOn prepareAppTask

(以上所有未经测试,但我希望一般推力应该有效).

(all of the above non-tested, but the general thrust should work, I hope).

这篇关于在标准任务之前/之后自动运行自定义任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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