Hide Exec output for non-failing tasks

One of the (many) things I love about Gradle is that its output is terse if everything is working, but verbose if something goes wrong. However, this breaks down when I have to run an Exec task as part of my build. In my case, one of my subprojects is calling make, which is always verbose. Is there a way I can get Gradle to swallow all of make’s output unless make fails?

It’s possible with some scripting. You can set the output stream of the ‘Exec’ task to a stream that stores the output in memory, configure the ‘Exec’ task not to fail in case of a non-zero exit code, and add a task action that checks the exit value and if necessary, prints the previously captured output and fails the task. For details, see ‘Exec’ in the Gradle Build Language Reference.

Thanks for the pointers. This is what I’m using now, inside my Exec task:

// Only show the output if something went wrong.
     standardOutput = new ByteArrayOutputStream()
     errorOutput = standardOutput
     ignoreExitValue = true
     doLast {
         if (execResult.exitValue != 0) {
             throw new GradleException("exec failed; see output above")