如果发生标准错误,则无法进行Gradle构建

时间:2018-12-07 12:34:30

标签: gradle errorprone

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

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

1 个答案:

答案 0 :(得分:2)

这里有一个示例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