Chris Kessel
Chris Kessel

Reputation: 5875

Gradle: custom message on task failure?

I'd like to hook into the compileJava target and spit out an additional custom message on failure. We've got a really common case setup case that a lot of folks overlook and it'd be useful, only on failure, to be able to do something like:

compileJava.onFailure { 
   println "Did you configure your wampfrankle to talk to the slackometer?" 
}

My Google skills have not yet led to an answer.

Upvotes: 7

Views: 2873

Answers (2)

Noel Yap
Noel Yap

Reputation: 19778

Use finalizedBy on a task that executes onlyIf the first task fails. For example:

tasks.compileJava.finalizedBy('compileJavaOnFailure')

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

  onlyIf {
    tasks.compileJava.state.failure != null
  }
}

Upvotes: 5

ditkin
ditkin

Reputation: 7044

The error is a dependency error and as Rene points out that needs to be detected after the build has executed, not after the project has been evaluated.

Here I've added a call to buildFinished with a closure that detects if a failure has happened and prints out an error message.

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

To test this I force a dependency resolution failure with this bogus dependency:

dependencies {
  compile 'foo:bar:baz'
}

Upvotes: 6

Related Questions