Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"
@shairontoledo
Copy link

Neat, thx

@katta
Copy link
Author

katta commented Feb 24, 2012

Glad it helped you, but its not my script. I was looking for this and found it in some blog and thought would share it here for others who are in need for the same.

@kell05
Copy link

kell05 commented May 21, 2012

Should probably give credit for this.

@caarlos0
Copy link

caarlos0 commented Nov 5, 2012

used it in my fork of oh-my-zsh :)

@dirtyhenry
Copy link

Thanks for sharing.

@dirtyhenry
Copy link

Worth noticing that some unit tests output might break mvn with the following error:
sed: RE error: illegal byte sequence

@marc-guenther
Copy link

@dirtyhenry: Can you give an example? Is this something I should be concerned about?

@mtpettyp
Copy link

@dirtyhenry @marc-guenther: Changing to "mvn $@ | LANG=C sed ..." should work

@cfclrk
Copy link

cfclrk commented Oct 9, 2013

You can use sed's --unbuffered option to get less choppy output. I'm still having a problem with "mvn archetype:generate" though: sed's buffer doesn't seem to flush before mvn asks for user input.

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

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