8

I'd like to use AnsiColorLogger to get color ouput from Ant. I'm using Git Bash on Windows.

I tried:

$ ant -logger org.apache.tools.ant.listener.AnsiColorLogger

but my output looks like:

Buildfile: c:\foo\build.xml
←[2;36m    [junit] Testsuite: org.foo.BarTest←[m
←[2;36m    [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.188 sec←[m
←[2;36m    [junit] ←[m
←[2;36m    [junit] Testcase: testInherits took 0.175 sec←[m
←[2;36m    [junit]      FAILED←[m
←[2;36m    [junit] subdir not child←[m
←[2;36m    [junit] junit.framework.AssertionFailedError: subdir not child←[m
←[2;36m    [junit]      at org.foo.BarTest.testInherits(BarTest.java:61)←[m
←[2;36m    [junit] ←[m
←[2;31m    [junit] Test org.foo.BarTest FAILED←[m

I know ANSI colors work, at least partially, in Git Bash because commands like ls -ACF --color=auto produce nicely colored output.

What's the trick for Ant?

jwfearn
  • 28,781
  • 28
  • 95
  • 122

2 Answers2

8

After much Googling and experimentation, I combined several variations into a solution that works well for the particular combination of Git Bash, Ant and Windows.

Relaunch Git Bash after adding the following lines to your .bashrc file:

alias ant='cant'
function cant {
  "ant" -logger org.apache.tools.ant.listener.AnsiColorLogger "$@" \
      2>&1 | perl -pe 's/(?<=\e\[)2;//g'
}

Git Bash Noob Tip: .bashrc file, if it exists, lives in your home directory. Here's how to create/edit it:

$ cd
$ notepad .bashrc &
jwfearn
  • 28,781
  • 28
  • 95
  • 122
  • For a general solution try creating a new file cant.bat beside your ant.bat within your ants bin folder with the following contents: @echo off call ant -logger org.apache.tools.ant.listener.AnsiColorLogger %* 2>&1 | perl -pe "s/(?<=\e\[)2;//g" – jek Jun 17 '13 at 14:59
  • No need to relaunch bash, after modifying `.bashrc`; just source it, in the running shell, like this: `. ~/.bashrc`, (or `source ~/.bashrc`). – Keith Marshall May 05 '15 at 11:47
  • notepad really is an abysmal choice of editor for creating or modifying `.bashrc`, (or indeed *any* shell script intended for consumption by either `bash.exe` or `sh.exe`), because it doesn't comprehend the LF only line ending style, which is the norm for such scripts. – Keith Marshall May 05 '15 at 11:51
7

it's probably even easier to just set the environment variable ANT_ARGS. e.g. just put this in your .bashrc:

export ANT_ARGS='-logger org.apache.tools.ant.listener.AnsiColorLogger'
cavbertel
  • 79
  • 1