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"
@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