运行 运行 之后的任务

Run a task after run

在执行 run 开始我的播放框架项目之前,我启动了一些 docker 个容器:

run in Compile := (run in Compile dependsOn(dockerComposeUp)).evaluated

现在我想在播放停止时使用 dockerComposeDown 拆除所有 docker 个容器。关于如何实现这一点有什么想法吗?

我已经完成了 Doing something after an input task,但这会启动容器并立即再次停止它们。 (事实上​​ ,它甚至在启动容器之前就停止了它们。)这是我尝试过的:

run in Compile := {
  (run in Compile dependsOn(dockerComposeUp)).evaluated
  dockerComposeDown.value
}

另一种方法是按顺序调用 docker 任务到 运行 任务。您可以如下所述实现此目的:

lazy val testPrint =  taskKey[Unit]("showTime")
testPrint := {
  println("Test print.")
}

lazy val testRun =  taskKey[Unit]("test build")
testRun := {

  Def.sequential((runMain in Compile).toTask(" com.mycompany.MainClass "), testPrint).value
}

首先定义 testPrint 任务,在您的情况下可能是 dockerTask,然后定义 testRun,它将 运行 这两个任务顺序执行。要 运行 这只是做 sbt testRun。执行后它应该打印出 "Test print."