Gradle color output

gradle verbose output
gradle run
gradle commandline
gradle version command
gradle suppress output
gradle capture standard output
gradle print
gradle warn

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.

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}

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?


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:

Older 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;

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

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.

Command-Line Interface, https://docs.gradle.org › org › gradle › api › logging › configuration › Co With GRADLE_OPTS unset I can do this. C:\depot\CoreLibs\main>set GRADLE_OPTS= C:\depot\CoreLibs\main>gradle --no-color show sourceSets.all main sourceSets.all test:show VARS version: 4.1.2.3. BUILD SUCCESSFUL. Total time: 5.462 secs. using C:\depot\CoreLibs\main>gradle -version-----Gradle 1.4-----Gradle build time: Monday, January 28, 2013 3:42:46 AM UTC Groovy: 1.8.6


Just additional informations completing the accepted answer. Here is the default styles with gradle 4.10

StyledTextOutput.Style.values().each {
    out.style(it).println("This line has the style $it")
}

Moreoever, you can create multicolor line, like a StringBuilder

out.style(Style.ProgressStatus).text('This is ').style(Style.Failure).text('a multicolor ').style(Style.Identifier).println('line')

edit : here is a working example :

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

def out = services.get(StyledTextOutputFactory).create("an-ouput")

out.style(Style.ProgressStatus).text('This is ').style(Style.Failure).text('a multicolor ').style(Style.Identifier).println('line')

ConsoleOutput (Gradle API 6.4.1), Disable all color and rich output. Generate plain text only. Auto. public static final ConsoleOutput Auto. Enable color and rich output when the current process is  Collection of Gradle plugins that usually come in handy when bootstrapping new projects - biasedbit/gradle-plugins


Is there a simple way to tap into the color printing powers of Gradle/Groovy without importing a lot of packages?

In the interest of exploring more options, without importing packages, you could just use straight ANSI escape codes (not a strictly Gradle/Groovy technology), to format your output. The following is a working example:

task myTask {
    def styler = 'black red green yellow blue magenta cyan white'
        .split().toList().withIndex(30)
        .collectEntries { key, val -> [(key) : { "\033[${val}m${it}\033[0m" }] }

    doLast {
        println "Message: ${styler['red']('Hello')} ${styler['blue']('World')}"
    }
}

Complete code on GitHub

Colorful Gradle, Did you ever whished to change the color of your gradle log output? Maybe to highlight something?AFAIK, Gradle has no support for colors out  Gradle Travis Colored Output (for tests) When running Gradle tests on Travis CI, the terminal is usually set to dumb mode, so you get very plain looking output. However, Travis does allow for colors in their logs. This Gradle script plugin formats the Gradle test output in a slightly colorful way (made for Travis CI but works in terminal).


Similar solution, suggested by ToYonos written on Kotlin DSL

val printStyles by tasks.registering {
    doLast {
       val out = project.serviceOf<org.gradle.internal.logging.text.StyledTextOutputFactory>().create("an-output")
        org.gradle.internal.logging.text.StyledTextOutput.Style.values().forEach {
            out.style(it).println("This line has the style $it")
        }
    }
}

gradle-plugins/colored-test-output.gradle at master · biasedbit , Gradle Travis Colored Output (for tests). When running Gradle tests on Travis CI, the terminal is usually set to dumb mode, so you get very plain looking output. To raise new issues or bugs against Gradle, Gradle; GRADLE-2319; colored output even if stdout is not connected to a terminal. Log In. Export. XML Word Printable.


mendhak/Gradle-Travis-Colored-Output: Gradle script , 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  Gradle redirects their logging output into the Gradle logging system. There is a 1:1 mapping from the Ant/Ivy log levels to the Gradle log levels, except the Ant/Ivy TRACE log level, which is mapped to Gradle DEBUG log level.


Gradle color output - logging - html, Set to auto (the default) to enable color and other rich output in the console output when the build process is attached to a console, or to  This option disables all color and other rich output in the console output. This is the default when Gradle is not attached to a terminal. Set to auto (the default) to enable color and other rich output in the console output when the build process is attached to a console, or to generate plain text only when not attached to a console.


gradle(1) — gradle — Debian testing, When running Gradle tests on Travis CI, the terminal is usually set to dumb mode, so you get very plain looking output. However, Travis does  Enable color and rich output when the current process is attached to a console, disable when not attached to a console. Plain Disable all color and rich output.