2

如果任何任务或插件在标准错误输出上打印任何内容,我如何将 Gradle 配置为最终失败构建(不会快速失败)?

我还没有在官方 API 中找到方法。

4

1 回答 1

4

这是一个示例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
于 2018-12-08T19:49:26.987 回答