Smart Bash/Zsh Aliases to Run Appropriate Grails Version
2011/09/26I’m currently on a project that has a couple of different apps that are using different versions of grails that need to be run concurrently. Switching a symlink no longer fit the way I needed to work so I came up with a couple bash/zsh aliases that are smart about the version of grails for the current directory.
These aliases work for both the grails as well as the grails-debug commands (for attaching a remote debugger).
If there is an application.properties file in the current directory, we can find the current version of grails for the app.
If there isn’t an application.properties file in the current directory, the script just defaults to whatever version of grails you’ve already set up as your default through the standard $GRAILS_HOME environment variable. You can use the grails symlink switching aliases that I created previously to easily move this between versions.
alias grails="execute_grails_version grails" alias grails-debug="execute_grails_version grails-debug" function execute_grails_version() { GRAILS_CMD=$1 shift if [ -f application.properties ]; then export GRAILS_VERSION=`grep app.grails.version application.properties | sed -E 's/.*=(.*)/\1/'` export GRAILS_HOME="/usr/local/grails-$GRAILS_VERSION" echo "application.properties found, using \$GRAILS_HOME of $GRAILS_HOME" else echo "application.properties NOT found, leaving \$GRAILS_HOME as $GRAILS_HOME" fi if [ ! -d $GRAILS_HOME ]; then echo "ERROR: Unable to find \$GRAILS_HOME directory at $GRAILS_HOME" exit 1 fi echo $GRAILS_HOME/bin/$GRAILS_CMD $* $GRAILS_HOME/bin/$GRAILS_CMD $* }
UPDATE: There are a few situations where aliases aren’t available (or are a pain to get available) such as when code is being executed as part of another application rather than from the command line. To get around this, these scripts (created by a co-worker of mine, @sjurgemeyer) could be put in your PATH, ahead of your $GRAILS_HOME/bin and used instead of the aliases above:
grails:
grails-version grails $*grails-debug:
grails-version grails-debug $*grails-version:
GRAILS_CMD=$1 shift if [ -f application.properties ]; then export GRAILS_VERSION=`grep app.grails.version application.properties | sed -E 's/.*=(.*)/\1/'` export GRAILS_HOME="/usr/local/grails-$GRAILS_VERSION" echo "application.properties found, using \$GRAILS_HOME of $GRAILS_HOME" else echo "application.properties NOT found, leaving \$GRAILS_HOME as $GRAILS_HOME" fi if [ ! -d $GRAILS_HOME ]; then echo "ERROR: Unable to find \$GRAILS_HOME directory at $GRAILS_HOME" exit 1 fi echo $GRAILS_HOME/bin/$GRAILS_CMD $* $GRAILS_HOME/bin/$GRAILS_CMD $*
There are 4 comments in this article: