摇篮彩色输出 [英] Gradle color output

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

问题描述

我看了这在谷歌,但似乎没有要对现场摇篮任何文件,甚至人的论坛讨论这个。

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)安装摇篮和我建立一个自定义的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?

下面是code的一个例子,我到目前为止有:

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这个论坛,他们谈论各种风格像正常,头,userinput,标识符,描述,progressstatus,失效,信息和错误,作为StyledTextOutput类的一部分。看起来这是一个内部类。有没有一种简单的方法来打入摇篮/ 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?

推荐答案

找到了答案!据<一个href=\"http://forums.gradle.org/gradle/topics/the_correct_way_for_a_custom_plugin_to_output_colored_styled_text_to_the_gradle_logging_system\">this 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:

import org.gradle.logging.StyledTextOutput;
import org.gradle.logging.StyledTextOutputFactory;
import static org.gradle.logging.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")

我仍然不知道需要在创建方法的字符串(它似乎并没有影响到任何东西)。再后来在你的任务,

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

正常,头,userinput,标识符,描述,progressstatus,失败,信息和错误:

这应与所有类别的工作。一个额外的步骤,如果你想自定义每个类别的颜色,添加以下到你的〜/ .gradle目录中的init.gradle文件(或的other选项):

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.

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

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