Gradle:有关任务失败的自定义消息?

Chr*_*sel 5 gradle

我想了解compileJava目标并在失败时吐出一条额外的自定义消息。我们有一个非常普通的案例设置案例,很多人都忽略了它,并且只有在失败的情况下,它才能执行以下操作:

compileJava.onFailure { 
   println "Did you configure your wampfrankle to talk to the slackometer?" 
}
Run Code Online (Sandbox Code Playgroud)

我的Google技能尚未找到答案。

dit*_*kin 5

该错误是从属错误,正如Rene指出的那样,需要在执行构建后而不是在评估项目后才进行检测。

在这里,我添加了一个对buildFinished的调用,该调用带有一个闭包,用于检测是否发生了故障并输出错误消息。

project.gradle.buildFinished { buildResult ->
  if (buildResult.getFailure() != null) {
    println "Did you configure your wampfrankle to talk to the slackometer?" 
  }
}
Run Code Online (Sandbox Code Playgroud)

为了测试这一点,我用这种虚假的依赖项强制了依赖项解析失败:

dependencies {
  compile 'foo:bar:baz'
}
Run Code Online (Sandbox Code Playgroud)


Noe*_*Yap 5

在执行第一个任务的finalizedBy任务上使用会失败。onlyIf例如:

tasks.compileJava.finalizedBy('compileJavaOnFailure')

task compileJavaOnFailure {
  doLast {
    println 'Did you configure your wampfrankle to talk to the slackometer?'
  }

  onlyIf {
    tasks.compileJava.state.failure != null
  }
}
Run Code Online (Sandbox Code Playgroud)