本文介绍了Gradle没有捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个非常简单的Gradle任务.
哪个运行Yarn命令-yarn_test哪个运行硒测试.
我正在使用com.moowork.gradle:gradle-node-plugin:1.2.0插件来运行Yarn命令我的任务看起来像-

I am running a very simple Gradle task.
which run Yarn command - yarn_test which run Selenium tests.
I am using com.moowork.gradle:gradle-node-plugin:1.2.0 plugin in order to run Yarn commandsmy task look like -

task run_tests(type: YarnTask) {
try {
    args = ['test']
}catch (all){
    println('Tests failed!')
}}

在我的packge.json中,我有:
"test":"mocha --timeout 25000 ./automation/test --reporter xunit-file"

and in my packge.json i have:
"test": "mocha --timeout 25000 ./automation/test --reporter xunit-file"

即使我将其更改为以exec身份运行
可执行的"sh"
args"-c",纱线测试"

even when I changed it to run as exec
executable "sh"
args "-c", "yarn test"

我遇到了同样的错误,因此插件无关,而是Gradle try-catch

I got the same error, so it is not something with the plugin but, with Gradle try-catch

即使一项测试都没有失败,为什么我要捕获异常.但由于某种原因,它没有被抓住.

when even one test fail no meter why I want to catch the exception.but for some reason it doesn't get caught.

当我使用--stacktrace运行时,这就是我得到的-

when I run with --stacktrace this is what I get-

任务:run_tests失败失败:生成失败,发生异常.* 什么地方出了错:任务':run_tests'的执行失败.进程'命令'/DATA/build/workspace/build_build/.gradle/yarn/yarn-yarn-v1.3.2/bin/yarn''完成,退出值非零* 尝试:使用--info或--debug选项运行,以获取更多日志输出.与--scan一起运行以获取完整的见解.*例外是:org.gradle.api.tasks.TaskExecutionException:任务':run_tests'的执行失败.

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'.

我想念什么?
我只想打印一些错误,然后完成构建..但没有例外吗?!
Gradle 4.7,在具有Gradle包装器的Ubuntu上运行.谢谢

What am I missing?
All I want is to print some error, and finish the build.. but not with exception?!
Gradle 4.7, run on Ubuntu with Gradle wrapper.Thanks

编辑

即使我只是跑步

sh: exit 1

我得到了相同的结果,所以必须是使用gradle try-catch而不是使用纱线ormocha等

I got same result so, it has to be something with gradle try-catch and not with yarn ormocha etc

推荐答案

问题是您的 try / catch 涵盖了配置gradle构建阶段,而不是执行阶段.在此处中介绍了整个gradle构建生命周期.答案末尾的每个阶段的说明.

The problem is that your try/catch is covering the configuration gradle build phase, not the execution phase. The full gradle build lifecycle is covered here, but I'll include the basic descriptions of each phase at the end of this answer.

看起来像 YarnTask 支持 ignoreExitValue 属性,因此您至少应该能够执行以下操作:

It looks like YarnTask supports the ignoreExitValue property, so you should at least be able to do something like:

task run_tests(type: YarnTask) {
    args = ['test']
    ignoreExitValue = true
}

只是忽略结果;我认为您可以使其更加复杂,例如:

to just ignore the result; I think you can make it even a bit more sophisticated, like:

task run_tests(type: YarnTask) {
    args = ['test']
    ignoreExitValue = true
    doLast {
        if(result.getExitValue() != 0){
            println "Tests failed!"
        }
    }
}

doLast 操作将在任务的所有已定义操作完成后执行(例如,在运行测试之后).

The doLast action will be executed after completion of all of the task's defined actions (e.g., after the tests have been run).

Gradle Build Lifecycle (很短的版本)的阶段:

Phases of the Gradle Build Lifecycle, the very short version:

  1. 初始化-确定要构建的项目/子项目
  2. 配置-配置所有项目/任务(执行构建脚本,以准备任务)
  3. 执行-自己执行任务
  1. Initialization - Determine which projects/subprojects will be built
  2. Configuration - Configures all the projects/tasks (executes the build scripts, which prepares the tasks)
  3. Execution - Executes the tasks themselves

这篇关于Gradle没有捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:53