出现标准错误时失败 Gradle 构建
Fail Gradle build when anything on standard error
如果任何任务或插件在标准错误输出上打印任何内容,我如何配置 Gradle 以在最后失败构建(不是快速失败)?
官方没找到方法API。
这里有一个示例 build.gradle
展示了它是如何工作的:
// create a listener which collects stderr output:
def errMsgs = []
StandardOutputListener errListener = { errMsgs << it }
// add the listener to both the project *and* all tasks:
project.logging.addStandardErrorListener errListener
project.tasks.all { it.logging.addStandardErrorListener errListener }
// evaluate the collected stderr output at the end of the build:
gradle.buildFinished {
if (errMsgs) {
// (or fail in whatever other way makes sense for you)
throw new RuntimeException(errMsgs.toString())
}
}
// example showing that the project-level capturing of stderr logs works:
if (project.hasProperty('projErr'))
System.err.print('proj stderr msg')
// example showing that the task-level capturing of stderr logs works:
task foo {
doLast {
System.err.print('task stderr msg')
}
}
// example showing that stdout logs are not captured:
task bar {
doLast {
System.out.print('task stdout msg')
}
}
后半部分的示例只是为了说明它按预期工作。尝试使用各种命令行构建 args/options:
# doesn’t fail:
./gradlew bar
# fails due to project error:
./gradlew -PprojErr bar
# fails due to task error:
./gradlew foo
# fails due to both task and project error:
./gradlew -PprojErr foo
如果任何任务或插件在标准错误输出上打印任何内容,我如何配置 Gradle 以在最后失败构建(不是快速失败)?
官方没找到方法API。
这里有一个示例 build.gradle
展示了它是如何工作的:
// create a listener which collects stderr output:
def errMsgs = []
StandardOutputListener errListener = { errMsgs << it }
// add the listener to both the project *and* all tasks:
project.logging.addStandardErrorListener errListener
project.tasks.all { it.logging.addStandardErrorListener errListener }
// evaluate the collected stderr output at the end of the build:
gradle.buildFinished {
if (errMsgs) {
// (or fail in whatever other way makes sense for you)
throw new RuntimeException(errMsgs.toString())
}
}
// example showing that the project-level capturing of stderr logs works:
if (project.hasProperty('projErr'))
System.err.print('proj stderr msg')
// example showing that the task-level capturing of stderr logs works:
task foo {
doLast {
System.err.print('task stderr msg')
}
}
// example showing that stdout logs are not captured:
task bar {
doLast {
System.out.print('task stdout msg')
}
}
后半部分的示例只是为了说明它按预期工作。尝试使用各种命令行构建 args/options:
# doesn’t fail:
./gradlew bar
# fails due to project error:
./gradlew -PprojErr bar
# fails due to task error:
./gradlew foo
# fails due to both task and project error:
./gradlew -PprojErr foo