如果任务失败,则Gradle任务doLast

Dar*_*key 5 groovy build gradle

我想要一个干净的构建,在那里你可以看到究竟发生了什么,但所有信息都被保留 - 所以基本上对于每个任务,我希望它将输出写入文件,并且只有在任务失败时才显示它.

我试图在gradle中实现这一点 - 但是我被击败了,因为如果任务失败,doLast不会运行.这是我的"差不多"工作模式:

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有替代doLast可以随时运行?这样做的其他任何方式? - 特别是因为我想为我的每一项任务做这件事?

Dar*_*key 6

这是我的最终解决方案:

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 )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


RaG*_*aGe 5

添加ignoreExitValue true到任务定义以在退出值非零时禁止抛出异常.