Russel Winder's Website

Grails - taking a small draught

ACCU is running a mentored developer project on Grails using the Grails in Action book by Glen Smith and Peter Ledbrook. The place to start is Chapter 1 which is a rush through the main concepts of a Grails application. I did it once using Emacs and command line. Thought to try the whole thing in Eclipse using STS, but never got to that before moving on to Chapter 2. This is a set of notes based on me redoing the Emacs and command line sequence, to provide notes for someone.

Installed Grails 1.4.0.M1 to a place and ensured an alias to the right place so that grails works from the command line.

"grails create-app qotd" works fine. If this is the first time of running grails it seems very slow as it is downloading stuff -- I wonder what happens if you are not connected to the Internet? - and it seems extraordinarily slow in this case if the filestore in NFS mounted rather than local disc. Fortunately this delay all seems to go away if grails has been used before.

"cd qotd", "grails run-app" work as expected. Very seriously slow starting up on NFS mounted filestore compared to local disc. Finally decides to load resources, hibernate, tomcat, and jquery.

Am seeing lots of Gant output :-) (wander around and the source code to get the point).

Finally Grails is ready and . . . after a long delay . . . the expected Grails page shows on http://localhost:8080. Doesn't look anything like the image in the book of course as this is Grails 1.4 not 1.1, which is what was used for the book.

Grails and Groovy community seem to favour Git over any other version control system, so rather than fight the community by using Mercurial or Bazaar, decided to use Git. Followed the instructions at g+Projects+into+SVN#Adding%20a%20new%20project%20to%20Git Methinks this should be a separate page, and there should be stuff for Mercurial and Bazaar as well - Git is not mandated by Grails. Then found out about so used that instead. It seems that Grails deals with IntelliJ IDEA as well as Eclipse.

OK, ready to move on, so create a controller: "grails create-controller quote" seems to do the right thing but slow on NFS mounted filestore. This comment about NFS mounted filestores comes up at all stages, so I shall now assume you appreciate this.

Added the home closure (Groovy calls them closures, but technically they are not, they are lambda functions -- the Groovy community knows that they are abusing the jargon term but it's all too late now :-( Grails notices the file change and compiles the file but there is an error of some sort. "Failed to reload file". This is reasonable as the file didn't exist when Grails was started, so a restart needed.

Did I mention how slow Grails is to start on NFS mounted filestore?

Anyway success. Added the index redirect. Updated file compiled and seemingly reloaded. Redirect works. Excellent. Commit.

Added Emacs backup files to the Git ignore list.

Added a view and it worked!

Added the style template. Begin to wonder if SiteMesh is really worth it. Now discover that snazzy.css and logo.png are not found. Reasonable really, the default Grails project shouldn't contain them! The book really ought to say something about putting these things in the web-apps directory. I didn't think I'd have to do a Grails restart but I did. Then discovered snazzy.css refers to three other image files background.png, logo_background.png, and menu_background.png. Found versions, put them in . . . failure. Restart Grails again. I get the feeling that adding any new file requires a restart.

Onto domain models. "grails create-domain-class quote" works fine. Listing 1.10 is slightly wrong, don't use what is printed there ("jdbc:hsqldb:file:devDB;shutdown=true") instead use "jdbc:h2:file:devDB".

Short-circuited the loading of the database by writing a Groovy script and then executing it in the Grails Console. Noted that qotd.Quote has to be explicitly imported, which the book doesn't mention.

Did I mention things are extraordinarily slow over NFS mounted filestore?

Apparently Grails not finding jquery at this point is not a problem.

Amended the controller to present a random message. Worked fine. Can't say I like the coding style of Listing 1.12, far to imperative and not nearly declarative enough.

def random = {
def allQuotes = Quote.list ( )
[ quote :
( ( allQuotes.size ( ) > 0 )
? allQuotes[ new Random ( ).nextInt ( allQuotes.size ( ) ) ]
new Quote ( author : 'Anonymous' , content : 'Real Programmers don\'t eat much quiche.' ) ) ] }

Scaffolding gives a CRUD interface by adding a single line to the controller. Magnificent.

Noted that all entries created via the scaffolding are created at 00:00 -- how to add a time to the date of creation?

Grails gets confused by the .#Quote.groovy file created by Emacs.

Added constraints, but the screen doesn't quite look like the one in the book, no time, just a date.

Created the service, spotted error in book, path shown is grails- app/services/QuoteService.groovy but should be grails- app/services/qotd/QuoteService.groovy

The field transactional is not in the default service as in the book, presumably a 1.1. -> 1.4 change. Listing 1.15 still hideously imperative and insufficiently declarative.

class QuoteService {
boolean transactional = false
def getStaticQuote ( )
def getRandomQuote ( ) {
def allQuotes = Quote.list ( )
( ( allQuotes.size ( ) > 0 )
? allQuotes[ new Random ( ).nextInt ( allQuotes.size ( ) ) ]
getStaticQuote ( ) ) } }

Started adding the tests. Spotted that the code listings had the wrong class names QuoteService Tests should have been QuoteServiceIntegrationTests. I hate * includes, not sure why but I do. the Grails template integration tests is full of them. Also of course the book is using Grails 1.1 and hence JUnit3 whilst Grails 1.4 uses JUnit4 so the test files are very different looking. But this is good. Using TestNG would of course be better than using JUnit4. But then using Spock would be even better. Must find out how to use Spock for these Grails tests.

Test reports go in a different place that stated in the book, I found them at target/test-reports/html/index.html. We are green :-)

Funny I always though AJAX was a scouring powder like VIM, apparently though it works well with Grails. I use the word apparently as it seems means you have to hack a lot to get it working. Something to do with JavaScript -- or should that be ECMAScript, is there actually a difference?

I think at this point I am going to quit, I'll leave AJAX hacks until I need them. Maybe actually try to do this with Eclipse/STS.

Final note: Grails is going to be great fun, and very efficatious at creating the Web applications I am seeking to build.

Copyright © 2017 Russel Winder -