sbt如何获得sbt任务中的编译结果(成功/失败)

时间:2018-11-09 13:13:57

标签: scala build sbt sbt-0.13

我试图编写一个sbt任务,该任务检查代码编译是成功还是失败,并根据该信息执行某些操作。到目前为止,我有这个:

https://github.com/JohnReedLOL/WeirdSbtBug/blob/894f497567477619b4150de92c6bb2c146a1b615/build.sbt#L46

编译失败时,它会打印出以下内容:

[warn] Compile: Inc(Incomplete(node=Some(Task((taskDefinitionKey: ScopedKey(Scope(Select(ProjectRef(file:/Users/john-michaelreed/Downloads/NewDownloads/sbt-0.13/lesson/HelloScala1/,helloscala1)),Select(ConfigKey(compile)),Global,Global),compile)))), tpe=Error, msg=None, causes=List(Incomplete(node=Some(Task((tags: Map(Tag(compile) -> 1, Tag(cpu) -> 1), taskDefinitionKey: ScopedKey(Scope(Select(ProjectRef(file:/Users/john-michaelreed/Downloads/NewDownloads/sbt-0.13/lesson/HelloScala1/,helloscala1)),Select(ConfigKey(compile)),Global,Global),compileIncremental)))), tpe=Error, msg=None, causes=List(), directCause=Some(Compilation failed))), directCause=None)) !

,其中包含字符串“编译失败”。我可以检查一下String是否存在,并根据结果执行一些操作。

示例:

val monitorTask = taskKey[Unit]("A task that gets the result of compile.")
monitorTask in Scope.GlobalScope := {
  // monitorTask dependencies:
  val log = streams.value.log // streams task happens-before monitorTask
  val compileResult = (compile.in(Compile)).result.value // compile task happens-before monitorTask

  // ---- monitorTask begins here ----
  if(compileResult.toString.contains("Compilation failed")) {
    log.warn("Compilation failed!")
    // Do stuff
  } else {
    log.info("Compilation succeeded!")
    // Do other stuff
  }
}

但这看起来有些脆弱。有更好的方法吗?

p.s。在测试monitorTask的过程中,我遇到了以下错误:https://github.com/sbt/sbt/issues/4444

1 个答案:

答案 0 :(得分:0)

如果您想要二进制成功或失败信息,为什么不致电toEither上的.result.value?然后,您可以检查它是否为isRight

相关问题