title: Gradle: Execute an action only if a task fails
body: <p>I would like to open the report file of several of the "check" and "test" plugins I use when a check fails. I know I <a href="https://stackoverflow.com/questions/20973365/gradle-execute-task-after-test-phase-even-if-test-has-failed">can use "finalizedBy</a> to execute another task no matter whether the original task was executed. Using that knowledge I tried the following to open the report only if the corresponding task (in this example <code>checkstyle</code>) fails:</p>
  
  <pre class="lang-groovy prettyprint-override"><code>task showCheckStyleResultsInBrowser(type: Exec) {
      ext.htmlFileName = "main.html"
      executable 'open'
      args 'file:///' + checkstyleMain.reports.xml.destination.parent + "/" + ext.htmlFileName
  }
  
  task showCheckStyleResultsIfFailed {
      ext.aCheckFailed = true
      doLast {
          if (ext.aCheckFailed) {
              showCheckStyleResultsInBrowser.execute()
          }
      }
  }
  
  checkstyleMain {
      finalizedBy 'showCheckStyleResultsIfFailed'
      doLast {
          // convert the xml output to html via https://stackoverflow.com/questions/20361942/generate-checkstyle-html-report-with-gradle
          ant.xslt(in: reports.xml.destination,
                   style: new File('config/checkstyle/checkstyle-noframes-sorted.xsl'),
                   out: new File(reports.xml.destination.parent, showCheckStyleResultsInBrowser.htmlFileName))
  
          showCheckStyleResultsIfFailed.aCheckFailed = false
      }
  }
  </code></pre>
  
  <p>Explanation (as far as I understand it): </p>
  
  <ul>
  <li><code>showCheckStyleResultsInBrowser</code> ist the task that actually does the opening of the report. You can ignore what it actually does, but it's the one that should be executed if the check-task fails</li>
  <li>The <code>showCheckStyleResultsIfFailed</code> task declares a property <code>aCheckFailed</code> and initializes it to true. When it is executed it checks whether it is still true (which means, the check did not complete successfully) and if so, opens the report using <code>showCheckStyleResultsInBrowser</code>.</li>
  <li><code>checkstyleMain</code> is the task that does the actual checking. I'm interested in its result. However I don't know how to get to it. So instead, in end of the <code>checkStyleMain</code> task, I set the <code>aCheckFailed</code> property to <code>false</code> relying on the fact that the last step will only be executed if none of the previous checks failed.</li>
  <li><code>showCheckStyleResultsIfFailed</code> is set to execute after <code>checkstyleMain</code> no matter what by <code>finalizedBy</code>. This way it will execute even if <code>checkstyleMain</code> fails. It uses the <code>aCheckFailed</code> property to determine whether <code>checkstyleMain</code> was completed successfully.</li>
  </ul>
  
  <p>This works ok if I do a complete build. But if I just do a partial rebuild and the checkstyleMain task does not run, because all its result are already up-to-date, I end up with <code>aCheckFailed</code> being true, because <code>checkstyleMain</code> did not run, which makes it look as if something actually went wrong. </p>
  
  <p>So how can I execute my <code>showCheckStyleResultsInBrowser</code> task if and only if the checkstyleMain task fails? Also, my solution feels rather cumbersome and hacky even for what it does achieve. Is there an easier way?</p>