Suppressing interactive output in Gradle exec tasks

ぃ、小莉子 提交于 2020-06-16 04:06:16

问题


Some Gradle Exec tasks produce excessive output when the command updates text in place.

When run from a terminal, these commands produce a couple of lines of output that are updated in place.

When run from within Gradle, they produce a new line of output each time something is updated, e.g.:

docker build:

task dockerBuild(type: Exec) {
    commandLine 'docker', 'build', '-t', 'foo', '.'
}

when run produces:

Sending build context to Docker daemon 557.1 kB
Sending build context to Docker daemon 1.114 MB
Sending build context to Docker daemon 1.646 MB
Sending build context to Docker daemon  2.17 MB
Sending build context to Docker daemon  2.72 MB
Sending build context to Docker daemon 3.277 MB
Sending build context to Docker daemon 3.834 MB
Sending build context to Docker daemon 4.391 MB
... hundreds more lines ...

wget:

task fetchData(type: Exec) {
    commandLine 'wget', 'http://example.org/some/large/file'
}

when run outputs:

HTTP request sent, awaiting response... 200 OK
Length: 209715200 (200M) [application/zip]
Saving to: '200MB.zip'

  0K .......... .......... .......... .......... ..........  0% 5.02M 40s
 50K .......... .......... .......... .......... ..........  0% 6.34M 36s
100K .......... .......... .......... .......... ..........  0% 6.68M 34s
150K .......... .......... .......... .......... ..........  0% 6.42M 33s
200K .......... .......... .......... .......... ..........  0% 6.41M 33s
250K .......... .......... .......... .......... ..........  0% 7.12M 32s
... thousands more lines ...

Is this anything I can fix from within Gradle (e.g. ask it to execute commands in non-interactive mode), or is it down to individual applications to provide flags for disabling this sort of output?


回答1:


From Exec DSL documentation, you can redirect the output of the executable to your own stream.

//store the output instead of printing to the console:
standardOutput = new ByteArrayOutputStream()

You could optionally also redirect error stream to keep it completely quiet, but by leaving it intact, you'll see any exit errors in gradle log.



来源:https://stackoverflow.com/questions/34766301/suppressing-interactive-output-in-gradle-exec-tasks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!