Last Updated: February 25, 2016
·
1.203K
· rodhilton

Grails alias that intelligently uses proper version

Working with multiple versions of grails can sometimes be a hassle.

This bash function and alias will try its hardest to find the proper version of grails to use for the current directory. It will use grails wrapper if available first, but if not it will check the version defined in the application.properties file and use the grails installation for it. Should all else fail,it just falls through to regular grails and assumes your GRAILS_HOME is set appropriately.

This is adapted from Ted Naleid's solution, mostly to add support for grailsw and improve the output a bit.

alias grails="execute_grails_version grails"
alias grails-debug="execute_grails_version grails-debug"
#Put your grails installations here, as 'grails-<version_number>'
GRAILS_LOCATIONS=/usr/share

function execute_grails_version() {
    GRAILS_CMD=$1
    shift
    if [ -f grailsw ]; then
        echo "[Found grailsw]"
        ./grailsw $*
    else
        if [ -f application.properties ]; then
            export OLD_GRAILS_HOME=$GRAILS_HOME
            export GRAILS_VERSION=`grep app.grails.version application.properties | sed -E 's/.*=(.*)/\1/'`
            export GRAILS_HOME="$GRAILS_LOCATIONS/grails-$GRAILS_VERSION"                

            if [ ! -d $GRAILS_HOME ]; then
                echo "[Unable to find $GRAILS_HOME, using $OLD_GRAILS_HOME]"
                export GRAILS_HOME=$OLD_GRAILS_HOME
                \grails $*
            else
                echo "[Found application.properties, using $GRAILS_HOME]"
                $GRAILS_HOME/bin/$GRAILS_CMD $*
                # Set it back
                export GRAILS_HOME=$OLD_GRAILS_HOME             
            fi
        else 
            \grails $*
        fi
    fi
}