我正在运行一个非常简单的Gradle任务。
哪个运行Yarn命令-yarn_test哪个运行Selenium测试。
我正在使用com.moowork.gradle:gradle-node-plugin:1.2.0插件来运行Yarn命令
我的任务看起来像-
task run_tests(type: YarnTask) {
try {
args = ['test']
}catch (all){
println('Tests failed!')
}}
在我的packge.json中,我有:
“test”:“mocha --timeout 25000 ./automation/test --reporter xunit-file”
即使我将其更改为以exec运行
executable "sh"
args "-c", "yarn test"
我遇到了同样的错误,所以插件不是问题,而是Gradle try-catch
当连一项测试都没有失败时,为什么我要捕获异常。
但由于某种原因,它没有被捕获。
当我使用--stacktrace运行时,这就是我得到的-
error Command failed with exit code 2. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Task :run_tests FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':run_tests'. Process 'command '/DATA/build/workspace/build_build/.gradle/yarn/yarn-v1.3.2/bin/yarn'' finished with non-zero exit value 1 * Try: Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':run_tests'.
我想念什么?
我只想打印一些错误,然后完成构建..但没有异常(exception)吗?
Gradle 4.7,在具有Gradle包装器的Ubuntu上运行。
谢谢
编辑
即使我只跑
sh: exit 1
我得到了相同的结果,所以它一定是带有gradle try-catch的东西,而不是与 yarn ormocha等
最佳答案
问题是您的try
/ catch
涵盖了配置 gradle构建阶段,而不是执行阶段。整个gradle构建生命周期包含在here中,但在此答案的结尾,我将包括每个阶段的基本描述。
looks like YarnTask
支持ignoreExitValue
属性,因此您至少应该能够执行以下操作:
task run_tests(type: YarnTask) {
args = ['test']
ignoreExitValue = true
}
只是忽略结果;我认为您可以使其更加复杂,例如:
task run_tests(type: YarnTask) {
args = ['test']
ignoreExitValue = true
doLast {
if(result.getExitValue() != 0){
println "Tests failed!"
}
}
}
doLast
操作将在任务的所有已定义操作完成后执行(例如,在运行测试之后)。Gradle Build Lifecycle的阶段,非常简短的版本:
关于gradle - Gradle没有捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51704691/