gradle - 当出现任何标准错误时,Gradle 构建失败
问题描述
如果任何任务或插件在标准错误输出上打印任何内容,我如何将 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')
}
}
后半部分的例子只是为了表明它按预期工作。尝试使用各种命令行参数/选项进行构建:
# 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
推荐阅读
- windows - 如何编译 mingw-w64-crt
- javascript - 与 android api 交互的 googlemap javascript api
- python - Cassandra 连接问题
- javascript - 减少从页面顶部滚动时的固定标题
- sql - 如何在 postgreSQL 中创建 generate_series 函数?
- json - Java中Json结构动态变化
- javascript - 电子:如何隐藏/显示
和
按按钮? - ios - Appcode 不会自动完成第三方库
- spring - 带有表达式的 JPA 规范
- gdb - MacOS Catalina 10.15.5 - OMNeT++ 5.6.2 卡在“配置 GDB”