Gradle exec task returns non-zero exit value and fails the build but I want to not fail and instead perform another task

TL;DR - Use ignoreExitValue = true

When I read the documentation for about the fiftieth time, I finally saw that there is a property, ignoreExitValue, which defaults to false. But if you set it to true, you can then perform your own tasks in the doLast block.

task myTask(type:Exec) {

    workingDir '.'

    commandLine './myscript.sh'

    ignoreExitValue true

    doLast {
        if(execResult.getExitValue() == 0) {
           //one thing
        } else {
           //another thing
        }
    }
}

If the ignoreExitValue answer is not working, another solution is to surround commandLine with a try { ... } catch (Exception e() { ... }.

Tags:

Gradle