Skip to content

Instantly share code, notes, and snippets.

@JAffleck
Forked from katta/mvncolor.sh
Created June 3, 2016 18:45
Show Gist options
  • Save JAffleck/bab50dbc21ff66faf80ae8b7262135ef to your computer and use it in GitHub Desktop.
Save JAffleck/bab50dbc21ff66faf80ae8b7262135ef 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"
@JAffleck
Copy link
Author

JAffleck commented Jun 3, 2016

Here's my version based on Koto's.
I only wanted to highlight the beginning of the line e.g. [INFO]/[WARNING]/[ERROR] with a few exceptions.
Should work in normal bash consoles
(git bash doesn't like it, but commander for http://cmder.net/ for Windows, and should work in OS X though untested.
Here's the sed string for the OS X version I used to use)

#  LC_CTYPE=C mvn $@ | sed \
#     -e "s/\(^\[INFO\]\ Building\)\(\ jar:\)/${RESET_FORMATTING}\1${RESET_FORMATTING}\2/g" \
#     -e "s/\(^\[INFO\]\ Building\)\(.* \)\(.*\)/${RESET_FORMATTING}\1${BOLD}${TEXT_BLUE}\2${RESET_FORMATTING}\3/g" \
#     -e "s/\(^\[INFO\]\ BUILD SUCCESS\)\(.*\)/${BOLD}${TEXT_GREEN}\1${RESET_FORMATTING}\2/g" \
#     -e "s/\(^\[INFO\]\ BUILD FAILURE\)\(.*\)/${BOLD}${TEXT_RED}\1${RESET_FORMATTING}\2/g" \
#   -e "s/\(^\[WARNING\]\)\(.*\)/${BOLD}${TEXT_YELLOW}\1${RESET_FORMATTING}\2/g" \
#   -e "s/\(^\[ERROR\]\)\(.*\)/${BOLD}${TEXT_RED}\1${RESET_FORMATTING}\2/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"
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
    # custom [info] cases are first, then default [info] case (could be customized for [Warning]/[Error] as well)
    mvn $@ | sed \
      -e "s/\(^\[INFO\]\ \)\(Building \)\(.*\ \)\(.*\)/${RESET_FORMATTING}${BOLD}${TEXT_BLUE}\1${RESET_FORMATTING}\2${BOLD}${TEXT_BLUE}\3${RESET_FORMATTING}\4/g" \
      -e "s/\(^\[INFO\]\ \)\(BUILD SUCCESS\)\(.*\)/${BOLD}${TEXT_BLUE}\1${BOLD}${TEXT_GREEN}\2${RESET_FORMATTING}/g" \
      -e "s/\(^\[INFO\]\ \)\(BUILD FAILURE\)\(.*\)/${BOLD}${TEXT_BLUE}\1${BOLD}${TEXT_RED}\2${RESET_FORMATTING}/g" \
      -e "s/\(^\[INFO\]\)\(\ .*\)/${RESET_FORMATTING}${BOLD}${TEXT_BLUE}\1${RESET_FORMATTING}\2/g" \
    -e "s/\(^\[WARNING\]\)\(.*\)/${BOLD}${TEXT_YELLOW}\1${RESET_FORMATTING}\2/g" \
    -e "s/\(^\[ERROR\]\)\(.*\)/${BOLD}${TEXT_RED}\1${RESET_FORMATTING}\2/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

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

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"

Although significantly uglier as code there are 2 key changes.

All the variables
(printf <color_code>) which works the exact same without the issue with escape characters not being recognized correctly.
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