Friday 8 April 2011

Grails - Ruby on Rails for the JVM

This post will be about a framework for the JVM called Grails, which stands for (or rather, used to stand for) Groovy on Rails. Grails is a RoR-like framework making use of the Groovy language and the "Convention over Configuration" paradigm which RoR seems to have made so popular.

First a little bit of history as to how I discovered grails. I was introduced to the grails framework in a round about way, when I started looking for the perfect RoR IDE and ended up using Netbeans (funnily enough, Netbeans has since dropped support for RoR). As a way of getting familiar with Netbeans I started working my way through the Netbeans tutorials, trying out the different frameworks and technologies along the way. This eventually led me to Grails. If there's one thing to be taken away from this little story it's to never stop learning new ways to write code and that the Netbeans tutorials are an excellent resource :-)

So, to get on with the show. We will be using a clean install of Ubuntu 10.10 (desktop edition) as our operating system, so bear in mind that you may have to tweak the commands below depending on your enviroment. The first thing we need to do before installing grails is to ensure that we have a Java runtime environment and a Java Developement Kit installed. Ubuntu 10.10 should come with the OpenJDK JRE installed by default. To check this out we open a terminal run java -version and we should see the following as output:

java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.7) (6b20-1.9.7-0ubuntu1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)


To install the JDK, we run:

sudo apt-get install openjdk-6-jdk

and install all of the necessary packages.

In order to run grails from the command line, we'll also need to set the JAVA_HOME and GRAILS_HOME variables as well as adding these to the PATH. We can achieve this by adding the following to the bottom of our ~/.bashrc file:

export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export GRAILS_HOME=$HOME/grails
PATH=$PATH:$JAVA_HOME/bin:$GRAILS_HOME/bin


NOTE: We'll have to open a new console window for these environment variables to become available.

Now that we've setup the environment, we can go to grails.org and download the latest Grails binary (1.3.7 at the time of this article). I usually move it to the home folder so that it's sitting under ~/grails-1.3.7. The observant amongst you will have noticed that in our ~/.bashrc file we've set $GRAILS_HOME to ~/grails previously, so in order to make this work, we create a symbolic link to this folder using ln -s ~/grails-1.3.7 ~/grails. We should now have our grails environment set up, so running grails from the terminal should give the following output:

Welcome to Grails 1.3.7 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /home/srdan/grails

No script name specified. Use 'grails help' for more info or 'grails interactive' to enter interactive mode


Congratulations, we now have a working grails install!

That's it for this post, in the next one we'll be having a look at creating our first grails project.

No comments: