我们在 Gradle 构建定义中使用 Exec 任务来运行 Webpack。
此任务有时会失败,但构建任务完成,我们的 Jenkins 服务器认为一切顺利,并发布了包含损坏客户端代码的 war 文件。
在 Exec 任务的输出中,您可以看到 Webpack 出现错误并停止。
我现在想知道是否可以让我的 Exec 任务消耗输出并在退出之前对其使用react,以便我可以使任务失败。
我玩过 setStandardOutput() 和 standardOutput,但我似乎无法让它工作。
下面是我的任务定义
task webpack(type: Exec) {
def mainCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'webpack.cmd' : 'webpack'
if (project.environment == 'prod')
commandLine mainCommand, '-p'
else
commandLine mainCommand
}
最佳答案
这对我来说似乎很好。它解析输出,如果输出包含以“错误”开头的行,则任务失败。
task webpack(type: Exec) {
def mainCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'webpack.cmd' : 'webpack'
standardOutput = new ByteArrayOutputStream()
if (project.environment == 'prod')
commandLine mainCommand, '-p'
else
commandLine mainCommand
doLast {
String output = standardOutput.toString()
if(output.readLines().any{line->line.trim().toLowerCase().startsWith("error")}) {
throw new GradleException("Error in WebPack: \n${output}")
} else {
println "Great success! Output is: ${output}"
}
}
}
关于jenkins - 根据输出使 Gradle 在 Exec 任务失败时构建暂停/失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35298943/