如何在另一个内部命令(不是外部命令)的sbt中获取返回代码?

时间:2018-04-26 12:56:14

标签: scala sbt

我的build.sbt文件如下所示:

import sys.process._
def makeProject = Command.args("make", "<args>") {
    (state, args) => {
        val exec = Exec("run", None);
        val makeCommand = ("make " + args.mkString(" "));
        val newState = MainLoop.processCommand(exec, state);
        // run is an internal command of sbt which can be executed from sbt terminal
        // what I need is the return code of the `run` INTERNAL command
        // if(exec's execution failed) do not execute the following makeCommand
        makeCommand!;
        newState;
    }
}

我需要获得Exec("run", None)的返回码。如果它成功运行,则应该执行后面的makeCommand - 否则,不要执行makeCommand。

1 个答案:

答案 0 :(得分:0)

您可以定义一个依赖于内部任务的任务,如下所示:

myTask := {
  val x = run.value
  val args = Seq("my", "args")
  val makeCommand = ("make " + args.mkString(" "))
  makeCommand!
}

现在如何让args更容易设置?我建议最常用的方法是在build.sbt中将它们定义为设置,并在任务中依赖它们。