Bash/Zsh aliases to switch Groovy and Grails version

2009/03/8

I work on a couple of different grails projects that use a variety of versions of groovy and grails. I’ve thrown together a quick shell script that makes it easy to create a new alias to switch between different versions depending on what project you’re working with.

function switchGrails() {
    echo "Switching to groovy version: $1"
    echo "Switching to grails version: $2"
    sudo rm /usr/local/{groovy,grails}
 
    sudo ln -s /usr/local/$1 /usr/local/groovy
    sudo ln -s /usr/local/$2 /usr/local/grails
    echo "Done!"
    ls -latr /usr/local/{groovy,grails}
}
 
alias g104='switchGrails "groovy-1.5.7" "grails-1.0.4"'
alias g11rc2='switchGrails "groovy-1.6.0" "grails-1.1-RC2"'

You can create your own aliases like the ones above to switch to the groovy/grails combinations that you happen to be working with.

Just stick the code above in your .profile/.bashrc/.zshrc file and restart your shell to make the aliases available.

This function assumes that you’ve got you’ve got groovy and grails installed in your /usr/local directory and that you use a symlink at /usr/local/groovy that $GROOVY_HOME is pointed to and /usr/local/grails that $GRAILS_HOME is pointed to. If those assumptions aren’t correct for you, you’ll have to tweak the script.

It also uses “sudo” as it assumes that /usr/local is owned by root and not by your logged in user. If you’ve chowned the directory to yourself, you can remove the sudo (and the need to enter your password).

Now it’s easy for me to switch between different projects by just typing the appropriate alias:

pollux% g104
Switching to groovy version: groovy-1.5.7
Switching to grails version: grails-1.0.4
Done!
lrwxr-xr-x  1 root  wheel  23 Mar  8 19:55 /usr/local/groovy -> /usr/local/groovy-1.5.7
lrwxr-xr-x  1 root  wheel  23 Mar  8 19:55 /usr/local/grails -> /usr/local/grails-1.0.4
 
 
pollux% g11rc2 
Switching to groovy version: groovy-1.6.0
Switching to grails version: grails-1.1-RC2
Done!
lrwxr-xr-x  1 root  wheel  23 Mar  8 19:56 /usr/local/groovy -> /usr/local/groovy-1.6.0
lrwxr-xr-x  1 root  wheel  25 Mar  8 19:56 /usr/local/grails -> /usr/local/grails-1.1-RC2

There are 8 comments in this article:

  1. 2009/03/9andhapp say:

    Nice… I have been thinking about upgrading the groovy and grails and this script but be so much helpful in switching back and forth in case the upgrade does not go well.

  2. 2009/03/10Colin Harrington say:

    Ted,

    Thank you for sharing your command-line kung fu.
    I love how easy it is to add Grails 1.1:

    alias g11=’switchGrails “groovy-1.6.0″ “grails-1.1″‘

    Just out of curiosity what other grails command line tricks do you have up your sleeve?

    Here are some of my aliases:
    alias g=”grails”
    alias gra=”grails run-app”
    alias grad=”grails-debug run-app”
    alias gta=”grails test-app”
    alias gtad=”grails-debug test-app”

  3. 2009/04/5Alexander Chernyakevich say:

    Thank you for helpful hint for Linux OS. I have found that somebody have to switch often between different versions of Grails as we. ;)

    But we generally works under Windows OS. So I created some tricky batch script for similar purposes. See http://achernyakevich.blogspot.com/2009/04/how-to-switch-grails-environment-under.html in my blog.

  4. 2010/02/13Vladimir Grichina say:

    Maybe you would be interested, I had the similar problem, but other approach (more automated, based on application.properties), described it in my blog:

    http://www.componentix.com/blog/10

  5. 2010/02/13tednaleid say:

    That’s interesting Vladimir, thanks for the link. I like the ability to figure out the grails version using the application.properties.

    One potential issue I can think of with it is that you have to be at the root of an app for it to work (so ‘grails run-app’ wouldn’t work, or would be hard coded to some default value). You also wouldn’t be able to do an “upgrade” to the latest version on an old app as it’d always pick the old version.

  6. 2010/04/10Vladimir Grichina say:

    Yep, Ted, there are issues you mention. I think I’ll make updated script to take them into account.

    In the meanwhile, there is a post of another developer from my team which describes the script for Windows which takes these things into account. May be useful for someone – http://www.componentix.com/blog/12

  7. 2010/07/31Vladimir Grichina say:

    Provided updated version of script with ability to do “create-app”, “upgrade”, etc, given you specify version to use explicitly.

  8. 2010/07/31Vladimir Grichina say:

    Forgot to include link into previous comment – http://www.componentix.com/blog/16

Write a comment: