Home » Programs » Ant Colorizor
An easy way to add color to Apache Ant build output. Unlike many other Ant color utilities, this one does not automatically color your output based on rules, but allows explicit control of colors and formatting from within the Ant build file itself. Requires Ant version 1.8 or above and color support from your terminal. For Windows you might have to use something like ANSICON.
Note that this library is not doing any real magic and is just a convenient wrapper around ANSI color codes. Details on ANSI color codes.
Current version is 0.9, released on January 7, 2017.
Download the binary jar file (built with Java 8)
- or -
Browse the source code on GitHub. Note that I am neither a Java nor Ant expert, so the code might not be the prettiest or the best, but it mostly works. Feedback and contributions are welcome.
Place the downloaded jar in a location readable by Ant and add the following lines to your build file:
<componentdef classname="org.dogsplayingpoker.ant.PropertyHelpers.ColorPropertyEvaluator" name="colorpropertyevaluator" classpath="path/to/ant-colorizor.jar" /> <propertyhelper> <colorpropertyevaluator /> </propertyhelper>
After imported into your build file, simply use the ${color:expr}
notation to add colored text to any task that supports property expansion. It is currently tested with the echo
and fail
tasks, but should work with others.
<echo>${color:blue}Hello World! in blue${color:reset}</echo> <echo>${color:bold}This text is bold${color:reset}</echo> <echo>${color:bg-green}This is text with a green background${color:reset}</echo> <echo message="${color:purple}Purple text from a message attribute${color:reset}" />
Output:
Multiple attributes can be applied at once, separated by commas (,
).
<echo>${color:red,bold}This text is green and bold${color:reset}</echo>
<echo>${color:underline,bg-cyan}Underlined text with a cyan background${color:reset}</echo>
<echo>${color:red,bg-green,bold}Red, bold text on a green background. Try not to puke!${color:reset}</echo>
Output:
Attributes can occur in any order.
<echo>${color:red,bold}This text is red and bold${color:reset}</echo>
<echo>${color:bold,red}This text however, is bold and red!${color:reset}</echo>
Output:
Raw numeric ANSI color values can also be used.
<echo>${color:31}31 is the number of red${color:reset}</echo>
Output:
Note that colors will continued to be applied even after the end of text unless specifically disabled.
<echo>${color:purple}Purple text that is unterminated...</echo>
<echo>...and continues until a reset happens${color:reset}</echo>
Output:
So each string should normally be ended with a ${color:reset}
<echo>${color:bg-cyan}Background cyan text${color:reset}</echo>
<echo>${color:blue,bold}This bold blue text${color:reset}</echo>
<echo>${color:green, bold}Spaces are allowed in the color expr list${color:reset}</echo>
<echo>Text without color</echo>
<echo>${color:yellow}This yellow text${color:reset} followed by normal text</echo>
<echo>${color:red,dim}Red dim text${color:reset-dim} followed by plain red text${color:reset}</echo>
<echo>${color:purple}Purple text, ${color:underline}add underline${color:reset-underline}, remove underline but still purple${color:reset}</echo>
<echo>${color:green,underline}Green, underlined text${color:reset}</echo>
<fail>:${color:red,bold}Major Error!${color:reset}</fail>
Output:
Valid colors and formatting which can be placed inside an expr
expression. Note that reset
can only be used by itself, and not combined with any other colors/formats.
Foreground Color | Background Color | Formatting | Reset Formatting | Global Reset |
---|---|---|---|---|
black | bg-black | normal | reset-normal | reset |
red | bg-red | dim | reset-dim | |
green | bg-green | bold | reset-bold | |
brown | bg-brown | underline | reset-underline | |
yellow1 | bg-yellow | blink2 | reset-blink | |
blue | bg-blue | reverse | reset-reverse | |
purple | bg-purple | hidden | reset-hidden | |
cyan | bg-cyan |
build.xml
files<?xml version="1.0"?> <project name="Color Test" default="color-test"> <componentdef classname="org.dogsplayingpoker.ant.PropertyHelpers.ColorPropertyEvaluator" name="colorpropertyevaluator" classpath="ant-colorizor.jar" /> <propertyhelper> <colorpropertyevaluator /> </propertyhelper> <target name="color-test" description="Tests to see if colors work"> <echo>${color:red}Red Text${color:reset}</echo> </target> </project>
Output:
Shows all possible color combinations for your terminal.
<?xml version="1.0"?> <project name="Color Demo" default="color-demo"> <!-- ant-contib will need to be in your classpath for this example to work see http://ant-contrib.sourceforge.net/ --> <taskdef resource="net/sf/antcontrib/antlib.xml" classpath="ant-contrib.jar" /> <componentdef classname="org.dogsplayingpoker.ant.PropertyHelpers.ColorPropertyEvaluator" name="colorpropertyevaluator" classpath="ant-colorizor.jar" /> <propertyhelper> <colorpropertyevaluator /> </propertyhelper> <target name="color-demo" description="Shows all possible color/formatting combinations for your terminal"> <for list="0,1,2,4,5,7,8" param="format"> <sequential> <for list="30,31,32,33,34,35,36,37" param="fg-color"> <sequential> <var name="line.str" value="" /> <for list="40,41,42,43,44,45,46,47" param="bg-color"> <sequential> <var name="line.str" value="${line.str}${color:@{format},@{fg-color},@{bg-color}}@{format};@{fg-color};@{bg-color}${color:reset} " /> </sequential> </for> <echo>${line.str}</echo> </sequential> </for> </sequential> </for> </target> </project>
Output:
To disable colors globally without removing all the notations from your build file (for example, on a terminal which does not support color output), use the org.dogsplayingpoker.ant.disable-color
property:
<!-- Globally disable coloring, perhaps for terminals which do not support colors --> <property name="org.dogsplayingpoker.ant.disable-color" value="true" /> ... <echo>${color:red}This text will not be in color :(${color:reset}</echo>
Output:
As of at least Ant 1.9.7, a color cannot be the first non-white space character of a message in a fail
task.
<fail>${color:red,bold}Epic fail${color:reset}</fail>
Output:
As a workaround, add leading character that will not be colorized.
<fail>:${color:red}Less epic fail${color:reset}</fail>
Output:
Yellow
has the same numeric value as brown
, but is provided for convenience as many terminals display this in yellowBlink
is not supported by a lot of terminals