Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task doLast if task fails

I want a clean build, where you can see exactly what happened, but all information is preserved - so essentially for every task, I want it to write the output to a file, and only display it if the task fails.

I've tried to achieve this in gradle - but am being defeated because doLast doesn't run if the task fails. Here's my "almost" working pattern:

task fakeTask( type: Exec ) {

    standardOutput = new ByteArrayOutputStream()
    commandLine 'cmd', '/c', 'silly'
    doLast {
        new File("build.log") << standardOutput.toString()
        if (execResult.exitValue != 0) println standardOutput.toString()
    }
}

is there any alternative to doLast that will run any time? Any other way of doing this? - especially as I want to do this for every single task I have?

like image 481
Darren Oakey Avatar asked Feb 10 '16 01:02

Darren Oakey


1 Answers

this is my final solution:

tasks.withType( Exec ) {
    standardOutput = new ByteArrayOutputStream()
    ignoreExitValue = true
    doLast {
        new File("gradle.log") << standardOutput.toString()
        if (execResult.exitValue != 0) 
        {
            println standardOutput.toString()
            throw new GradleException(commandLine.join(" ") + " returned exitcode of " + execResult.exitValue )
        }
    }
}
like image 122
Darren Oakey Avatar answered Sep 30 '22 04:09

Darren Oakey