所有子项目之后的 SBT 运行任务 [英] SBT run task after all subprojects

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

问题描述

我想写一个在所有子项目任务完成后运行的任务.

I want to write a task that is run after all subproject tasks are complete.

例如,如果我这样做

sbt a b 然后在所有子项目上完成任务 a 后,我想执行任务 b.我不想在每个项目上都做 (a b).

sbt a b then after task a is complete on ALL subprojects I want to execute task b. I don't want to do (a b) on each project.

这可能吗?

其实我拿来直接修改build.sbt.我不一定要在命令行中指定它.

In fact, I'll take it so that I modify the build.sbt directly. I don't necessarily have to specify it at the command line.

推荐答案

我写了一篇关于这个主题的博文:使用 sbt-sequential 对任务进行排序.

I wrote a blog post on the subject: sequencing tasks with sbt-sequential.

这是一个例子.我们将在 sub1 和 sub2 中定义一个自定义任务 a,在 root 中定义一个 b.实现顺序执行的最简单方法是使用 addCommandAlias,所以我们就这样做.

Here's an example. We'll define a custom task a in sub1 and sub2 and b in root. The simplest way to achieve sequential execution is using addCommandAlias, so we'll just do that.

lazy val a = taskKey[Unit]("a")
lazy val b = taskKey[Unit]("b")

lazy val root = (project in file(".")).
  aggregate(sub1, sub2).
  settings(addCommandAlias("ab", ";a;b"): _*).
  settings(
    b := {
      println("b")
    }
  )

lazy val sub1 = (project in file("sub1")).
  settings(a := println("a - sub1"))

lazy val sub2 = (project in file("sub2")).
  settings(a := println("a - sub2"))

您可以在 shell 中以 sbt ab 的形式运行它.

You can run this from shell as sbt ab.

$ sbt ab
[info] Loading global plugins from ...
[info] Loading project definition from ...
[info] Set current project to root (in build ...)
a - sub2
a - sub1
[success] Total time: 0 s, completed Nov 22, 2014 8:36:18 PM
b
[success] Total time: 0 s, completed Nov 22, 2014 8:36:18 PM

Def.taskDyn

这是另一个例子.这次使用 Def.taskDyn,它也在博客文章中有所介绍.我正在从 aggregate 构建一个 ScopeFilter,然后我将任务 a 分派给它们.

Def.taskDyn

Here's another example. This time using Def.taskDyn, which is also featured in the blog post. I'm constructing a ScopeFilter from the aggregate and then I'm dispatching task a to them.

lazy val a = taskKey[File]("a")
lazy val b = taskKey[Seq[File]]("b")

lazy val root = (project in file(".")).
  aggregate(sub1, sub2).
  settings(
    b := (Def.taskDyn {
      val proj = thisProject.value
      val filter = ScopeFilter(inProjects(proj.aggregate: _*))
      Def.task {
        val allValues = a.all(filter).value
        println(allValues.mkString(","))
        allValues
      }
    }).value
  )

lazy val sub1 = (project in file("sub1")).
  settings(a := new File("a"))

lazy val sub2 = (project in file("sub2")).
  settings(a := new File("b"))

您可以在 shell 中以 sbt b 的形式运行它.

You can run this from shell as sbt b.

$ sbt b
[info] Loading global plugins from ...
[info] Loading project definition from ...
[info] Set current project to root (in build ...)
a,b
[success] Total time: 0 s, completed Nov 23, 2014 9:42:16 PM

这篇关于所有子项目之后的 SBT 运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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