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?
clean will delete a build directory, all your classes will be removed for a fresh compile. assemble will create an archive (JAR) file from your Java project and will run all other tasks that require compile, test and so on.
Gradle does not redirect its logs in a separate file in Android Studio. Therefore if you want to view them in a file, you need to build gradle using a command in the terminal and redirect gradle input to a file. This command will redirect all standard output and error messages from gradle build to a file called myLogs.
To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … e.g. gradlew clean allTests.
You can use the command line switches shown in Log level command-line options to choose different log levels. You can also configure the log level using gradle. properties , see Gradle properties. In Stacktrace command-line options you find the command line switches which affect stacktrace logging.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With