There may be times when you have to run multiple versions of Grails in your local development environment. It may be because you supporting multiple Grails applications each running different version, or you would like to test your prime Grails app in different version before shipping to customer, or you just grabbed some Grails application from internet( ‘into wild’) and like try out. For whatever reason, in this post i describe one setup that auto detects version of the application and runs it in appropriate Grails version.
Pre-requisite: Unix based machine
We set up so that when ‘grails’ command is called, it runs a script. This script will read the version from Grails application(application.properties) you want to run and forwards all of the commands to the appropriate grails version to run.
All of the multiple Grails framework versions are installed in ‘/DevTools/grails’ folder in a format grails-x.x.x.
From the multiple Grails versions, we have chosen one version as default one and defined env variable GRAILS_HOME that points to it.
Step 1 – Create Script
Create file ‘grails’ in some folder you choose. Make this file executable by running ‘chmod u+x filename’
Next, copy past the following:
#!/bin/bash grails GRAILS_CMD=$1 shift GRAILS_DEFAULT=$GRAILS_HOME if [ -f application.properties ];then export GRAILS_VERSION=`grep app.grails.version application.properties | sed -E 's/.*=(.*)/\1/'` export GRAILS_HOME="$DEV_TOOLS/grails/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 if [ ! $GRAILS_HOME = $GRAILS_DEFAULT ] && [ -d $GRAILS_DEFAULT ]; then echo "Unable to find $GRAILS_HOME. Switching back to default $GRAILS_DEFAULT" export GRAILS_HOME=$GRAILS_DEFAULT else echo "ERROR: Unable to find \$GRAILS_HOME directory at $GRAILS_HOME" exit 1 fi fi echo $GRAILS_HOME/bin/$GRAILS_CMD $* $GRAILS_HOME/bin/$GRAILS_CMD $*
Make sure to adjust the highlighted line 8 to point to your folder where all of the different Grails versions are located for you.
Step 2 – Setup ‘grails’ Command to Run Script
Update your PATH variable to include the folder containing the script – grails you created in Step 1.
Note: make sure you restart command line terminal to ensure your new updated env variable PATH is loaded
Running Different Versions
Afterwards, when command ‘grails something’ is called, then it goes to your ‘grails’ script. The scripts searches through the application.properties file for the version. Once found, it checks if this version is stored in your machine. If it is not, it runs the default version you have set in ‘GRAILS_HOME’ variable.
There may be times you need to run ‘grails-debug’ command. If that is the case you then change your setup as follows:
1. Rename ‘grails’ script to ‘grails-version’
2. Create new file named ‘grails’ and add the following:
grails-version grails $*
3. Create new file named ‘grails-debug’ and add the following:
grails-version grails-debug $*
Note:make sure you make new scripts executable like step 2
So this will forward ‘grails’ and ‘grails-debug’ commands to versioning script before running either of the commands