Maven build notifications

Setting up new environment is usually a great time to reflect on all the great things and all the bad things you had encountered in the past.
Now I had just experienced this very moment twice in a row, thus resulting in a full scale tools revision. One of the things I have always overlooked were command line aliases.

Once I decided to use aliases, another question was in order: Why to stop there, isn't there a way to put them to even better use? And after a while of googling I found a great article, it describes how to get system notifications from command line.

While until now all the project I encountered took less than a minute to build, my current project's tests take more than ten minutes to execute. This is why I searched for this feature. Now I can easily switch to another window to do something somewhat productive and yet to be notified as soon as the build finishes. And all that without the need to periodically check the terminal window or guessing according to processor's fan noise.


The first thing you need is obviously Maven itself. You can install it from repository, or manually from maven home. This post's main script assumes that Maven is present on PATH, allowing you, the script or anyone else to invoke Maven by mvn command.

Second requirement is the notification library libnotify-bin. This can be easily tested by notify-send "test" command. This is a quite interesting library and I suggest you to read its notify-send --help for further customization possibilities.


This is my version of the script based on Eric's script. Detail explanation is located after the script.

    $CMD $@
    BUILD_CMD=`basename $CMD`
    if [ $RETCODE -eq 0 ]
        notify-send -c $BUILD_CMD -i emblem-default -t 3600000 "$BUILD_DIR: $BUILD_CMD successful" "$(date)"
    elif [ $RETCODE -eq 130 ]
        notify-send -c $BUILD_CMD -i emblem-ohno -t 3600000 "$BUILD_DIR: $BUILD_CMD canceled" "$(date)"
        notify-send -c $BUILD_CMD -i emblem-important -t 3600000 "$BUILD_DIR: $BUILD_CMD failed" "$(date)"
    return $RETCODE

alias mvn="cmdstatus mvn"
alias mci="mvn clean install"

The script defines function cmdstatus, which wraps around any given command and sends notification depending on command's return code right after a command finishes.

There are three different notifications. Return code zero indicates Success, code 130 indicates user termination (usually ctrl + c), this is the main change as cancelled builds being reported as failed really bordered me. Any other return code is declared a failure.

Second last line of the script is the key system change. This alias actively overrides maven's binary from PATH and replaces it with cmdstatus wrapped version.

The last line is merely a demonstration, one of my many expanding aliases. It shows you, that you can safely build upon the notification wrap. Now executing the command mci results in command cmdstatus mvn clean install, which would be quite a pain to write several times a day.


The code snippet needs to be placed in the right spot in order to work. That depends on your shell.
If use bash, you should place it at the end of the .bashrc file in your home directory. This is the most likely possibility.
If use zsh (or oh-my-zsh), than .zshrc is the file you want to edit.
After adding the script, you only have to restart the terminal and you will be ready to action.


This what you can expect to see as notifications.
On success.
On cancelation.
On failure.


That was it. You should now be notified on your build results as soon as the building ends. On a related note, if you are using linux or mac and you are avoiding aliases, I strongly suggest you to give them a try as they can really make your life a little easier. If you are using window, tough luck, frankly cmd is one of the main things that drove me away.

Hope you enjoyed the post, let me know if you feel that something is missing, or that it could be done easier/better.


Popular posts from this blog

Ldap security for Jenkins CI

Automatic jsp recompile on Jboss AS 7

Simple EJB Arquillian test based on JUnit running on managed JBoss AS 7