Skip to content

Instantly share code, notes, and snippets.

@katta
Created June 15, 2011 18:50
Show Gist options
  • Star 74 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save katta/1027800 to your computer and use it in GitHub Desktop.
Save katta/1027800 to your computer and use it in GitHub Desktop.
Script to add colors to maven output
#!/usr/bin/env bash
# Formatting constants
export BOLD=`tput bold`
export UNDERLINE_ON=`tput smul`
export UNDERLINE_OFF=`tput rmul`
export TEXT_BLACK=`tput setaf 0`
export TEXT_RED=`tput setaf 1`
export TEXT_GREEN=`tput setaf 2`
export TEXT_YELLOW=`tput setaf 3`
export TEXT_BLUE=`tput setaf 4`
export TEXT_MAGENTA=`tput setaf 5`
export TEXT_CYAN=`tput setaf 6`
export TEXT_WHITE=`tput setaf 7`
export BACKGROUND_BLACK=`tput setab 0`
export BACKGROUND_RED=`tput setab 1`
export BACKGROUND_GREEN=`tput setab 2`
export BACKGROUND_YELLOW=`tput setab 3`
export BACKGROUND_BLUE=`tput setab 4`
export BACKGROUND_MAGENTA=`tput setab 5`
export BACKGROUND_CYAN=`tput setab 6`
export BACKGROUND_WHITE=`tput setab 7`
export RESET_FORMATTING=`tput sgr0`
# Wrapper function for Maven's mvn command.
mvn-color()
{
# Filter mvn output using sed
mvn $@ | sed -e "s/\(\[INFO\]\ \-.*\)/${TEXT_BLUE}${BOLD}\1/g" \
-e "s/\(\[INFO\]\ \[.*\)/${RESET_FORMATTING}${BOLD}\1${RESET_FORMATTING}/g" \
-e "s/\(\[INFO\]\ BUILD SUCCESSFUL\)/${BOLD}${TEXT_GREEN}\1${RESET_FORMATTING}/g" \
-e "s/\(\[WARNING\].*\)/${BOLD}${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
-e "s/\(\[ERROR\].*\)/${BOLD}${TEXT_RED}\1${RESET_FORMATTING}/g" \
-e "s/Tests run: \([^,]*\), Failures: \([^,]*\), Errors: \([^,]*\), Skipped: \([^,]*\)/${BOLD}${TEXT_GREEN}Tests run: \1${RESET_FORMATTING}, Failures: ${BOLD}${TEXT_RED}\2${RESET_FORMATTING}, Errors: ${BOLD}${TEXT_RED}\3${RESET_FORMATTING}, Skipped: ${BOLD}${TEXT_YELLOW}\4${RESET_FORMATTING}/g"
# Make sure formatting is reset
echo -ne ${RESET_FORMATTING}
}
# Override the mvn command with the colorized one.
alias mvn="mvn-color"
@dant3
Copy link

dant3 commented Nov 13, 2013

You can take a look at my fork, I made some fixes:

  • don't have color definitions in global scope (always spoiled my output on mvn -X and env)
  • removed blue for every INFO line, made only headers with --- be white instead
  • added highlighting for [debug] lines to spot them easier

@k3vinw
Copy link

k3vinw commented Mar 13, 2014

If you need mvn's exit status, you can use Bash's PIPESTATUS array to capture and return the last exit code for any command from a pipeline.

# example wrapper that returns mvn's exit status
function mvn_wrapper() {
  mvn $@ | sed ...
  MVN_EXIT=${PIPESTATUS[0]}
  do_something_else
  return $MVN_EXIT
}

Hope this helps somebody - saved my bacon when I was chaining some long running maven profiles and scratching my head at why the first maven build would fail and then happily continue to the next. Oops!

@lou-k
Copy link

lou-k commented Apr 17, 2014

Thanks @k3vinw; totally helped me.

@bkreda
Copy link

bkreda commented Aug 14, 2014

This will not pass parameters to mvn correctly.
In this command,

mvn release:prepare -Darguments='-Denv=reda -DskipTests'

Test will run even if I passed -DskipTests.

When I remove the mvncolor scripts, tests will not run.

@hoto
Copy link

hoto commented Sep 18, 2015

I love this colorized output ;)
Many many thanks.

But this takes about 3 sec to load, so instead of calling it everytime I open git bash on windows i have done it like this:
It will be executed only when i run mvn

mvn-color()
{
    export BOLD=`tput bold`
    export UNDERLINE_ON=`tput smul`
    export UNDERLINE_OFF=`tput rmul`
    export TEXT_BLACK=`tput setaf 0`
    export TEXT_RED=`tput setaf 1`
    export TEXT_GREEN=`tput setaf 2`
    export TEXT_YELLOW=`tput setaf 3`
    export TEXT_BLUE=`tput setaf 4`
    export TEXT_MAGENTA=`tput setaf 5`
    export TEXT_CYAN=`tput setaf 6`
    export TEXT_WHITE=`tput setaf 7`
    export BACKGROUND_BLACK=`tput setab 0`
    export BACKGROUND_RED=`tput setab 1`
    export BACKGROUND_GREEN=`tput setab 2`
    export BACKGROUND_YELLOW=`tput setab 3`
    export BACKGROUND_BLUE=`tput setab 4`
    export BACKGROUND_MAGENTA=`tput setab 5`
    export BACKGROUND_CYAN=`tput setab 6`
    export BACKGROUND_WHITE=`tput setab 7`
    export RESET_FORMATTING=`tput sgr0`
    # Filter mvn output using sed
    mvn $@ | sed -e "s/\(\[INFO\]\ \-.*\)/${TEXT_BLUE}${BOLD}\1/g" \
        -e "s/\(\[INFO\]\ \[.*\)/${RESET_FORMATTING}${BOLD}\1${RESET_FORMATTING}/g" \
        -e "s/\(\[INFO\]\ BUILD SUCCESSFUL\)/${BOLD}${TEXT_GREEN}\1${RESET_FORMATTING}/g" \
        -e "s/\(\[WARNING\].*\)/${BOLD}${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
        -e "s/\(\[ERROR\].*\)/${BOLD}${TEXT_RED}\1${RESET_FORMATTING}/g" \
        -e "s/Tests run: \([^,]*\), Failures: \([^,]*\), Errors: \([^,]*\), Skipped: \([^,]*\)/${BOLD}${TEXT_GREEN}Tests run: \1${RESET_FORMATTING}, Failures: ${BOLD}${TEXT_RED}\2${RESET_FORMATTING}, Errors: ${BOLD}${TEXT_RED}\3${RESET_FORMATTING}, Skipped: ${BOLD}${TEXT_YELLOW}\4${RESET_FORMATTING}/g"

    # Make sure formatting is reset
    echo -ne ${RESET_FORMATTING}
}
alias mvn-color="mvn-color"
alias mvc=mvn-color
alias mvn=mvn-color

@mehrdadsilver
Copy link

How can I use this script with maven?

@jeffkreska
Copy link

Anyone know how to make it show output that is not prefixed with [INFO]|[DEBUG]etc? I have a plugin I run that prompts me to enter some info and the prompt is always hidden when using this.

@HimanshuRanka
Copy link

HimanshuRanka commented Feb 13, 2024

Throwing this in here for anyone trying to setup mvnColor on gitbash on windows. Windows does not always recognize the escape characters of the variables and the sed chaining causing this not to work.

` mvnColor() {
# export BOLD='\033[1m'
# export UNDERLINE_ON='\033[4m'
# export UNDERLINE_OFF='\033[24m'
# export TEXT_BLACK='\033[30m'
# export TEXT_RED='\033[31m'
# export TEXT_GREEN='\033[32m'
# export TEXT_YELLOW='\033[33m'
# export TEXT_BLUE='\033[34m'
# export TEXT_MAGENTA='\033[35m'
# export TEXT_CYAN='\033[36m'
# export TEXT_WHITE='\033[37m'
# export RESET_FORMATTING='\033[0m'

mvn "$@" | sed -e "s/\(^\[INFO\]\ \)\(Building \)\(.*\ \)\(.*\)/$(printf '\033[0m')$(printf '\033[1m')$(printf '\033[34m')\1$(printf '\033[0m')\2$(printf '\033[1m')$(printf '\033[34m')\3$(printf '\033[0m')\4/g; s/\(^\[INFO\]\ \)\(BUILD SUCCESS\)\(.*\)/$(printf '\033[1m')$(printf '\033[34m')\1$(printf '\033[1m')$(printf '\033[32m')\2$(printf '\033[0m')/g; s/\(^\[INFO\]\ \)\(BUILD FAILURE\)\(.*\)/$(printf '\033[1m')$(printf '\033[34m')\1$(printf '\033[1m')$(printf '\033[31m')\2$(printf '\033[0m')/g; s/\(^\[INFO\]\)\(\ .*\)/$(printf '\033[0m')$(printf '\033[1m')$(printf '\033[34m')\1$(printf '\033[0m')\2/g; s/\(^\[WARNING\]\)\(.*\)/$(printf '\033[1m')$(printf '\033[33m')\1$(printf '\033[0m')\2/g; s/\(^\[ERROR\]\)\(.*\)/$(printf '\033[1m')$(printf '\033[31m')\1$(printf '\033[0m')\2/g; s/^Tests run: \([^,]*\), Failures: \([^,]*\), Errors: \([^,]*\), Skipped: \([^,]*\)/$(printf '\033[1m')$(printf '\033[32m')Tests run: \1$(printf '\033[0m'), Failures: $(printf '\033[1m')$(printf '\033[31m')\2$(printf '\033[0m'), Errors: $(printf '\033[1m')$(printf '\033[31m')\3$(printf '\033[0m'), Skipped: $(printf '\033[1m')$(printf '\033[33m')\4$(printf '\033[0m')/g; s/downloading/$(printf '\033[33m')&$(printf '\033[0m')/ig; s/\(.*downloading\)\(.*\)/\1$(printf '\033[37m')\2$(printf '\033[0m')/ig; s/downloaded/$(printf '\033[32m')&$(printf '\033[0m')/ig; s/\(.*downloaded\)\(.*\)/\1$(printf '\033[37m')\2$(printf '\033[0m')/ig"

# reset formatting
echo -ne $(printf '\033[0m')   

} `

Although significantly uglier as code there are 2 key changes.

  1. All the variables ${VAR_NAME} are replace with hard coded values $(printf <color_code>) which works the exact same without the issue with escape characters not being recognized correctly.
  2. instead of chaining it using \ -e "new s/old/new/g statement" we chain it using the semicolon inside a single string - "s/old1/new1/g; s/old2/new2/g" and this also does the same thing

Not sure if there is a better way to do things but this worked really well to add color to the mvn output on gitbash windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment