我正在尝试编写一个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)) !它包含字符串"Compilation“。我可以检查该字符串是否存在,并根据结果执行一些操作。
示例:
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
}
}但这看起来有点脆弱。有没有更好的方法呢?
附注:在测试monitorTask的过程中,我遇到了这个错误:https://github.com/sbt/sbt/issues/4444
发布于 2018-12-15 04:03:32
如果您想要二进制成功或失败信息,为什么不在.result.value上调用toEither?然后,您可以检查它是否是isRight。
https://stackoverflow.com/questions/53226392
复制相似问题