Wednesday, June 19, 2013

Maven / Ant Alerts in OS X

The Case of the Forgotten Builds (Mac Edition)

You can see my previous post on Maven / Ant Alerts in Ubuntu for the Linux version

There are a few projects I work with that have long builds. I have a bad habit of alt-tabbing away from the build and promptly forgetting that I even started it only to come back 15 minutes later and not remembering if/when/why I built the project. What I need is a way for these tools to get my attention when they are done.

growlnotify To the Rescue!

Growl, a popular notifications app for OS X has a nice command line utility called growlnotify. Download an install it from that link and then with a short bash function and a few aliases we can have nice notifications for any command line we want!

The OS X Terminal as of 10.7 also responds to the "bel" escape character if it is in the background. For each bel that occurs while the Terminal is in the background a numeric badge appears and the counter it shows increments. The Terminal app icon also bounces a few times.

The combination of Growl and the Terminal bel provide a nice notification system that your command is done.

1. Install Growl and growlnotify

2. Edit ~/.bash_aliases:

cmdstatus()
{
    CMD=$1
    shift
 
    $CMD $@
    RETCODE=$?
    BUILD_DIR=${PWD##*/}
    BUILD_CMD=`basename $CMD`
    if [ $RETCODE -eq 0 ]
    then
        printf '\a'
        growlnotify -s -d $BUILD_CMD -n $BUILD_CMD -m "$BUILD_DIR: $BUILD_CMD successful
$(date)"
    else
        printf '\a\a'
        growlnotify -s -d $BUILD_CMD -n $BUILD_CMD -m "$BUILD_DIR: $BUILD_CMD failed
$(date)"
    fi
    return $RETCODE
}
alias ant="cmdstatus ant"
alias mvn="cmdstatus mvn"

3. Restart Your Terminal

Now you get nice success/failure messages out of your ant and mvn commands. The original commands return code is correctly preserved and this cmdstatus wrapper can be easily added to any other command you want via a simple alias line.





No comments: