45

When I run a maven goal in Jenkins (e.g. "mvn test"), the logs are very hard to read because of the color control characters that appear (see example below). Is there anyway to remove the color formatting?

[[1;34mINFO[m] [1m-------------------------------------------------------------------[m
[[1;34mINFO[m] [1;32mBUILD SUCCESS[m
[[1;34mINFO[m] [1m-------------------------------------------------------------------[m
[[1;34mINFO[m] Total time: 16.097 s
[[1;34mINFO[m] Finished at: 2017-04-26T11:23:06-04:00
[[1;34mINFO[m] Final Memory: 41M/100M
[[1;34mINFO[m] [1m-------------------------------------------------------------------[m
simbro
  • 3,372
  • 7
  • 34
  • 46
  • 1
    What kind of job do you use? Maven Job or Freestyle job ? Apart from that you can simply define `-B` for --batch-mode which will turn off using of colors... – khmarbaise Apr 26 '17 at 16:52

5 Answers5

56

Thanks to khmarbaise for his/her answer in the comment to my question:

you can simply define -B for --batch-mode which will turn off using of colors..

This fixed the issue, my output now looks like this (much nicer):

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.507 s
[INFO] Finished at: 2017-04-27T05:30:07-04:00
[INFO] Final Memory: 41M/100M
[INFO] ------------------------------------------------------------------------
simbro
  • 3,372
  • 7
  • 34
  • 46
  • 12
    Just as a note for future people, Maven 3.5.1 will be [adding an explicit flag to turn off colors](https://issues.apache.org/jira/browse/MNG-6220) – romeara Sep 14 '17 at 19:39
  • 6
    Note that the actual flag is `-Dstyle.color=(auto|always|never)`. The page linked to by @romeara discusses multiple syntaxes for the option, and it is easy to miss the one that was eventually implemented. – toolforger Jul 17 '19 at 07:06
  • Even the answer matches the question, a better solution for shells that support color might be to fix the issue by UPGRADE MAVEN ! I had the problem under maven 3.5.2, and it was fixed with maven 3.6.1. – olir Sep 04 '19 at 12:27
  • Thanks Olir - since this seems to be a popular questions it's good that someone pointed that - definitely the simplest solution. – simbro Sep 04 '19 at 15:47
  • On a related note: if you see the ANSI codes under cygwin 3.1, it may be because maven doesn't recognise the TERM environment variable value xterm-256color. Try running: TERM=cygwin mvn – tanderson May 30 '20 at 03:54
  • How about giving some votes to @khmarbaise, who gave the original answer? – spekdrum Nov 16 '22 at 12:04
14

--batch-mode, mentioned in other answers, is usually a better option for non-interactive execution, like in a CI server, but if you want just to turn off the color, it is possible since maven 3.5.1 using the following option:

mvn -Dstyle.color=never

other options for this parameter are always or auto.

neves
  • 33,186
  • 27
  • 159
  • 192
1

maven --batch-mode will do. However, if you have to inspect some already-generated output that contains those colour codes, you can use sed to strip the codes away, as it's explained here.

zakmck
  • 2,715
  • 1
  • 37
  • 53
0

Jenkins Maven plugin example of batch mode -B (remove color tags) and silent mode -q inside pipeline script:

rtMaven = Artifactory.newMavenBuild()
...
rtMaven.run pom: 'pom.xml',
            goals: "-q -B clean compiler:compile" 
MichalSv
  • 567
  • 5
  • 10
0
set env

MAVEN_OPTS="$MAVEN_OPTS -Dmaven.color=false"
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103