Calling the correct Grails version automatically from command line

| Comments

UPDATE: For latest version and instructions, see

Now that I decided to organize and publish some of my code/hacks here, I thought it would be a good thing to republish here my Grails caller script.

I work on and maintain various Grails projects at the same time, and some of them uses versions of Grails as old as 1.0.3! So the question is: How to call the right version of grails command for a given project, the version that the project was created with?

First I tried changing the GRAILS_HOME environment variable every time I was going to work with a project that uses a different version than the default. But it’s just too much work for a thing that should be transparent. So I decided to create a shell script to solve this problem. The script should detect which Grails version to call when it’s executed. Here’s the script I came up with:

# Author: Deluan (

# Check if GRAILS_HOME is set
if [ -z "$GRAILS_HOME" -o ! -d "$GRAILS_HOME" ]; then
    echo "Error: GRAILS_HOME not set"
    exit 2

# Extract the base path from GRAILS_HOME
BASE_GRAILS_PATH=`echo $GRAILS_HOME | sed -e "s/[^\/\\]*$//" -e "s/^$/./"`

# Try to get the version from the command line
if [ -d "${BASE_GRAILS_PATH}/grails-${TRY_VERSION}" ]; then

# Or else get the version from the in the current directory
[ -z "$VERSION" -a -f "$APP_PROP" ] &&
    VERSION=`awk -F'=' '/app.grails.version/ { print $2 }' $APP_PROP | tr -d '\r\n'`

# Or else use the default version
if [ -z "$VERSION" ]; then  
    VERSION=`basename $GRAILS_HOME | cut -f 2 -d "-"`


if [ ! -x "$GRAILS_CMD" ]; then
    echo "Error: grails command not found at '$GRAILS_CMD'!"
    exit 3

exec $GRAILS_CMD $*

How it Works?

The script first checks if you specified a version in the command line, like: grails 1.3.5-SNAPSHOT create-app. If not, it looks for an file in the current folder. This file keeps some metadata for Grails projects, including the Grails version the project was created with. If it does not find any in the current folder, it then just calls the default Grails installed in your system, the one that GRAILS_HOME points to.


  • All your Grails versions must be installed under the same base directory. Ex:
  • GRAILS_HOME environment variable must be set and point to your “default” Grails installation

  • This script was tested on Mac OS X (Snow Leopard), Linux (Ubuntu) and Windows (with cygwin)



Using the script is as transparent as possible:

  • If you invoke it from a project folder, it will detect the version used by the project and call the correct grails (if it is installed in your system)
  • If you invoke it from any other folder that does not contain a Grails project, it will call the “default” Grails installation
  • If you want to call a specific Grails version (i.e. when doing an upgrade) you can specify the version you want in the first parameter. Ex:
$ grails 1.3.3 upgrade