2007-06-20

Groovy and Grails: Introduction



Groovy and Grails very much excite me.

Groovy is an alternative programming language for the Java Virtual Machine (JVM). Java Specification Request 241 (JSR 241) states that "Groovy is an agile, dynamic programming language for the Java Virtual Machine". This alternative language for the JVM has been around for a few years; see the blog of James Strachan. A programmer can put into a few lines of Groovy code the same task that would take several more lines of Java.

According to the Grails official web site, Grails is "an open-source web application framework that leverages the Groovy language and complements Java Web development"; in other words, a programmer can use Grails to build web applications in a simple, organised manner using the Groovy and Java languages. The idea behind Grails is to build upon some of the desirable functionalities available in the web application framework Rails, but with functionality more familiar to Java developers. The official Grails web site does a better job of fully explaining Grails than what I care to do here.

So, I'd like to share my journey exploring Groovy and Grails....

Quick note: I recently purchased a new laptop (or "notebook")--a Lenovo Z61m running Microsoft Windows Vista Home Premium operating system. Some software do not install or run properly on Vista, but this should be a temporary state of affairs with most major Windows software manufacturers. Thankfully, Java appears to run well on Vista; so, once again, Java has proven itself a reliable platform.

To get started, I wanted to use a good IDE for Java development, so I downloaded my favourite Java IDE: JDeveloper (aka "JDev"). Right now, I have Studio Edition Version 10.1.3.2.0.4066. When I downloaded JDev, I selected the Base Install (does not include JDK) rather than the Full Install (includes JDK). I'd also downloaded and installed the latest JDK (JDK 6u1) from Sun. I decided to do this so as to have JDev running with the latest JDK.

Next, I downloaded a free Groovy plug-in/add-on/extension for JDev through the Help-->Check for Updates dialog in JDev (this ability to easily download plug-ins for JDev is fantastic).

Then, I downloaded Grails from the official Grails web site. At first, I thought that since the Groovy plug-in for JDev included the necessary Groovy library, that I would not need to download Groovy for Grails to run; i.e., I thought that I'd be able to tell Grails to use the Groovy library in the JDev extensions directory. Apparently, this did not work; so I downloaded Groovy from the official Groovy web site. After installing both Grails and Groovy, and setting Windows environment variables that include JAVA_HOME, GRAILS_HOME, and GROOVY_HOME, as well as updating the Path variable to include references to bin directories of Groovy and Grails, I was able to create a new Grails project.

Before attempting to work with Grails, it helps to test if it exists by typing the following request for Grails Help at the command line:

grails /?

Next, at the command line, try to create a new Grails application using the create-app command:

grails create-app my_grails_example

You should see output from the command line that looks something like the following:



The resulting directory structure will appear like this:
C:\Users\mikequentel\my_grails_example
├───grails-app
│   ├───conf
│   ├───controllers
│   ├───domain
│   ├───i18n
│   ├───services
│   ├───taglib
│   ├───utils
│   └───views
│       └───layouts
├───hibernate
├───lib
├───plugins
│   └───core
│       └───grails-app
│           ├───taglib
│           └───utils
├───scripts
├───spring
├───src
│   ├───groovy
│   ├───java
│   └───test
├───test
│   ├───integration
│   └───unit
└───web-app
  ├───css
  │   └───tree
  │       ├───check
  │       ├───default
  │       ├───folders
  │       └───menu
  ├───images
  │   └───tree
  │       ├───check
  │       ├───default
  │       ├───folders
  │       └───menu
  ├───js
  │   ├───prototype
  │   └───yahoo
  │       └───assets
  ├───META-INF
  └───WEB-INF
      ├───classes
      ├───grails-app
      │   └───i18n
      ├───lib
      ├───spring
      └───tld

Because Grails includes a Jetty container, you can immediately test the web application (though you will probably want to configure Grails to run on another Servlet Container or J2EE platform). Navigate to the base directory of your Grails application (in this example, navigate to my_grails_example). Then, use the run-app command:

grails run-app my_grails_example

You should see something like this at the command line:



Once this runs, you can then launch a browser window and enter the URL provided in the end of the output at the command line (in this example, http://localhost:8080/my_grails_example). You should see something like this:



Because this is just a skeleton of a Grails application, there is no list of controllers as might be suggested in the welcome message.

After I create an example with some more functionality, I shall show you how Groovy and Grails can provide a powerful tool set for developing web applications.

2 comments:

Mike Quentel said...

Author's note: Back when I originally wrote this post, I enjoyed using JDev, but since then, I have been using Eclipse, and have grown to like it about as much (perhaps even more).

Mike Quentel said...

Also, thankfully as of 2008, I have ditched Windows and have instead used OpenSUSE as well as Ubuntu on my Lenovo z61m laptop.