如何通过Gradle任务执行命令后修改错误消息

rqqzpn5f  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(105)

我正在实现一个Gradle Java插件,它在任务下面注册。

TaskProvider<Exec> taskProvider = project.getTasks().register("spectralTask", Exec.class);
taskProvider.configure(exec -> {
    exec.executable("spectral");
    exec.args("lint");
});

字符串
当执行的命令的输出是错误时,这就是我得到的。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:spectralTask'.
> Process 'command 'spectral'' finished with non-zero exit value 1


此错误消息不是很有用。我想弥补下面有办法吗?
1.打印自定义错误消息。
1.打印由执行的命令给出的确切错误消息。

vsdwdz23

vsdwdz231#

要实现这一点,一个选择是用ByteArrayOutputStream覆盖exec的standardOutput和/或errorOutput,并使用修改后的消息重新抛出异常。
举例来说:

def execOutput = new ByteArrayOutputStream()
try {
    return this.project.exec({
        it.errorOutput = execOutput
        it.standardOutput = execOutput       
        // rest of exec's config
    })
} catch (exception) {
    // Will print task's output as part of the error message
    throw new ExecException(exception.message + LineSeparator.SYSTEM + execOutput.toString())
} finally {
    // Preserve the logs in the task output
    print execOutput.toString()
}

字符串

相关问题