Gradle 颜色输出 [英] Gradle color output

查看:14
本文介绍了Gradle 颜色输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Google 上查过这个,但 Gradle 网站上似乎没有任何文档,甚至没有人在论坛上讨论这个.

I've looked this up on Google, but there doesn't seem to be any documentation on the Gradle site, or even people discussing this in forums.

我在 Mac(10.8.2,ML)上安装了 Gradle,并且正在构建自定义 build.gradle 脚本.当我调用 println() 时,我想让输出着色(如红色错误、绿色信息等).如何在我的 gradle 构建脚本中执行此操作?

I have Gradle installed on my Mac (10.8.2, ML) and am building a custom build.gradle script. When I call println(), I would like to make the output colored (like errors in red, info in green, etc). How do I do this in my gradle build script?

这是我迄今为止的代码示例:

Here's an example of code I have so far:

def proc = "echo `DATE`".execute()
proc.in.eachLine {line -> println line}
proc.err.eachLine {line -> println 'ERROR: ' + line}

这个 gradle 论坛上,他们谈论作为 StyledTextOutput 类的一部分,各种样式,如正常、标题、用户输入、标识符、描述、进度状态、失败、信息和错误.看起来这是一个内部类.有没有一种简单的方法可以在不导入大量包的情况下利用 Gradle/Groovy 的彩色打印功能?

On this gradle forum, they talk about various styles like normal, header, userinput, identifier, description, progressstatus, failure, info, and error, as part of the StyledTextOutput class. It looks like this is an internal class. Is there a simple way to tap into the color printing powers of Gradle/Groovy without importing a lot of packages?

推荐答案

找到答案了!根据 这个 gradle 论坛帖子,没有用于为记录器的输出着色的公共界面.您可以自由使用内部类,但这些可能会在未来版本中发生变化.在gradle脚本中,放在最上面:

Found the answer! According to this gradle forum post, there's no public interface for coloring the output of the logger. You are free to use the internal classes, but those may change in future versions. In the gradle script, put at the top:

旧 Gradle:

import org.gradle.logging.StyledTextOutput;
import org.gradle.logging.StyledTextOutputFactory;
import static org.gradle.logging.StyledTextOutput.Style;

Gradle 3.3+:

import org.gradle.internal.logging.text.StyledTextOutput;
import org.gradle.internal.logging.text.StyledTextOutputFactory;
import static org.gradle.internal.logging.text.StyledTextOutput.Style;

(我还没有想出如何将它移动到 init.gradle 文件中.)然后不久之后,定义

(I still haven't figured out how to move this to the init.gradle file.) Then shortly after, define

def out = services.get(StyledTextOutputFactory).create("blah")

我仍然不确定在 create 方法的字符串中需要什么(它似乎还没有影响任何东西).然后在你的任务中,

I'm still not sure what needs to be in the create method's string (it doesn't seem to affect anything yet). Then later in your task,

out.withStyle(Style.Info).println('colored text')

这应该适用于所有类别:正常、标题、用户输入、标识符、描述、进度状态、失败、信息和错误.如果您想自定义每个类别的颜色,请将以下内容添加到 ~/.gradle 目录(或 其他选项):

This should work with all the categories: normal, header, userinput, identifier, description, progressstatus, failure, info, and error. An additional step if you want to customize the color of each category, add the following to an init.gradle file in your ~/.gradle directory (or other options):

System.setProperty('org.gradle.color.error', 'RED')

然后用上面列表中的任何一个替换错误"类别.

and then replace the "error" category with any from the above list.

这篇关于Gradle 颜色输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆