Skip to content

Instantly share code, notes, and snippets.

@deluan
Created September 28, 2010 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deluan/601039 to your computer and use it in GitHub Desktop.
Save deluan/601039 to your computer and use it in GitHub Desktop.
Script for selecting and calling the correct Grails version when you have more than one version installed
#!/bin/sh
# Author: Deluan (http://techbeats.deluan.com)
# Check if GRAILS_HOME is set
if [ -z "$GRAILS_HOME" -o ! -d "$GRAILS_HOME" ]; then
echo "Error: GRAILS_HOME not set"
exit 2
fi
# Extract the base path from GRAILS_HOME
BASE_GRAILS_PATH=`echo $GRAILS_HOME | sed -e "s/[^\/\\]*$//" -e "s/^$/./"`
APP_PROP="application.properties"
# Try to get the version from the command line
TRY_VERSION=$1
if [ -d "${BASE_GRAILS_PATH}/grails-${TRY_VERSION}" ]; then
VERSION=$TRY_VERSION
shift
fi
# Or else get the version from the application.properties 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 "-"`
fi
export GRAILS_HOME=${BASE_GRAILS_PATH}/grails-${VERSION}
GRAILS_CMD=${GRAILS_HOME}/bin/grails
if [ ! -x "$GRAILS_CMD" ]; then
echo "Error: grails command not found at '$GRAILS_CMD'!"
exit 3
fi
exec $GRAILS_CMD $*
@deluan
Copy link
Author

deluan commented Sep 28, 2010

Prerequisites

  • All your Grails versions must be installed under the same base directory. Ex:

    /opt/grails-1.0.3
    /opt/grails-1.1.1
    /opt/grails-1.3.3
    /opt/grails-1.3.5-SNAPSHOT
    
  • 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)

Installation

  • Download the script: http://gist.github.com/601039
  • Include the folder where it is installed in your PATH.
  • Exclude $GRAILS_HOME/bin from your PATH

Usage

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
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment