Friday, December 23, 2011

Getting started with Grails (again)

Well, it's been a while since I had looked at Grails. In addition to being busy and not having a need for it, I was worried about Grails' future given Spring Roo's existence. However, I am no longer worried (for now). Grails 2.0.0 RC3 is now available and it seems that VMWare is committing itself to both Grails and Roo with the acknowledgement that they provide solutions for different audiences.

I have been updating this post to keep the information in one place so it has grown substantially. It should be a great reference for anyone who is new to Grails and looking to create a site from scratch.

Installation

  1. Download SpringSource Tool Suite 2.8.1 if you haven't already done so. It's free! I've found that the embedded vFabric tc Server works better on Linux than on Mac OS. Not sure why...
  2. Install Groovy, Grails, and make sure you have JDK 1.6

Getting Started

  1. Follow this beginners' tutorial. It's easy to follow and is relevant. In particular, I like how he introduces BootStrap.groovy, which is a way to pre-populate your data between recompiles
  2. This is another beginners' tutorial but it's wordier and harder to follow if you have little patience
  3. When learning how to do unit testing, simply remember that the tests automatically generated for you will fail. You need to edit populateValidParams() with at least one valid record. Also, this page is a concise tutorial on unit and integration testing
  4. If you are interested in using HAML, you need to install the plugin for each Grails project. Simply type, "grails install-plugin haml". Here is some more information. If your page is normally called "list.gsp", your HAML file should be named "list.haml".
  5. Here is an HTML to HAML converter. It works well but there may be a few bugs. I found it doesn't handle the "page import" statement or ternary operators well.
  6. All things Groovy... Ever seen a closure? Did you know that Groovy has a full API doc? Also, there are a few differences between Groovy and Java but not many.

Rich Internet Application

  1. A great way to incorporate user security (login/logout/authentication) is by using the Grails plugin for Spring Security. I chose to create my own User class by extending the SecUser class and Static URL rules.
  2. When you want to extend your controller output to provide XML or JSON (say you're building an API...) then this is a great tutorial. To test your controller, use curl:
    curl -v -L http://localhost:8080/Grails1/user/show/1.json
  3. As of Grails 2.0.0, the default JavaScript library is jQuery, not Prototype. This is a good tutorial on using AJAX with Grails and this post suggests the correct way to configure jQuery for Grails 2.0.0

Servers

  1. Heroku is a great way to setup a development server, and if your needs are limited, then it's free! To get started with Heroku, follow these setup directions. To deploy a Grails application, use the Grails Heroku plugin. I discovered that, for these instructions to work, you should initialize your Grails app using the command line, not by creating a "New" > "Grails Project" in STS. There is a supplemental instruction here to configure your Grails app to use a Postgres database.
  2. To publish your app to Heroku, you will need to use Git. I highly recommend Git in any case as a cheaper alternative to Perforce, SVN, and CVS. There is a learning curve but it is available out of the box if you have installed SpringSource ToolSuite (STS) on Ubuntu and there is a lot of good documentation. Here is a tutorial and a quick reference. An added bonus is that STS comes pre-installed with EGit -- the Git plugin for Eclipse.
  3. Heroku's signature database service is PostgreSQL. If you want to test your Grails application locally using the same database, this is a great beginner's tutorial to install and set up a Postgres server on your Ubuntu machine.
  4. If you want to use MySQL, install a local server using
    mysql-client-core-5.1
    Be certain to provide a root password because MySQL Workbench does not permit blank passwords. This post describes how to set up MySQL with your Grails app and this post describes how to use the ClearDB addon. Follow these instructions for your DataSource.groovy