在Sbt中,如何在任务中执行命令

Qin*_*wei 1 sbt

是否可以在Sbt任务中执行命令?如果是这样,怎么样?由于司令部需要一个国家,我怎么能得到一个?

我试图覆盖默认任务,这是我尝试过的

dist := {
  println("Turning coverage off")
  Command.process("coverageOff")
  dist.value
}
Run Code Online (Sandbox Code Playgroud)

Command.process的签名是 (string, state) => _

我还没弄明白从哪里得到国家

Mir*_*tta 6

是的,您可以在任务中运行命令.以下是我目前正在做的事情.首先,在构建中定义以下方法:

/**
  * Convert the given command string to a release step action, preserving and      invoking remaining commands
  * Note: This was copied from https://github.com/sbt/sbt-release/blob/663cfd426361484228a21a1244b2e6b0f7656bdf/src/main/scala/ReleasePlugin.scala#L99-L115
  */
def runCommandAndRemaining(command: String): State => State = { st: State =>
  import sbt.complete.Parser
  @annotation.tailrec
  def runCommand(command: String, state: State): State = {
    val nextState = Parser.parse(command, state.combinedParser) match {
      case Right(cmd) => cmd()
      case Left(msg) => throw sys.error(s"Invalid programmatic input:\n$msg")
    }
    nextState.remainingCommands.toList match {
      case Nil => nextState
      case head :: tail => runCommand(head, nextState.copy(remainingCommands = tail))
    }
  }
  runCommand(command, st.copy(remainingCommands = Nil)).copy(remainingCommands = st.remainingCommands)
}
Run Code Online (Sandbox Code Playgroud)

然后,使用上面定义的实用程序从任务中调用任何命令,例如runCommandAndRemaining("+myProject/publishLocal")(state.value).

在你的具体情况下,它应该归结为

dist := {
  val log = streams.value.log
  log.debug("Turning coverage off")
  runCommandAndRemaining("coverageOff")(state.value)
  dist.value
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!