我正在尝试编写一个 sbt 任务来检查代码编译是成功还是失败,并根据该信息执行某些操作。到目前为止,我有这个:
当编译失败时,它打印出:
[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)) !
其中包含字符串“编译失败”。我可以检查该字符串是否存在,并根据结果执行某些操作。
例子:
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
}
}
但这看起来有点脆弱。有没有更好的方法来做到这一点?
ps 在测试monitorTask的过程中,遇到了这个bug:https ://github.com/sbt/sbt/issues/4444