Skip to content

Grails Startup Pain

by Jeff

Grails is a composed of a stack I like, a combination of technologies I’ve enjoyed using over the last few years- Groovy, HSQLDB, Spring, Hibernate, etc. And the whole seems greater than the sum of its parts, so while I’m complaining about it today, I do so knowing that with future releases Grails could really deliver a great solution.

One thing that many new frameworks are striving to offer these days is a quick startup- you can push this button, pull this lever, and voila, you have a hello world and can build from there. Of course this sounds better than writing a domain class, a dao, a service class, then enter some BS in your web.xml, and wire your beans, and add a *-servlet.xml, configure log4j, etc etc etc. And it is. Except when it doesn’t work.

My initial attempt at creating a Grails project went like this:

  • grails create-app created my application.
  • import project imported my project into Eclipse. I ended up with 25 or so classes in my base directory.
  • grails run-app runs Jetty. I pull up a browser and can see my new site- everything appears to be going great.
  • grails create-domain-class Create a domain class. Edit the domain class file, add a few properties. Easy.
  • grails create-controller Creates a controller class. Remove the index() method and add def scaffold = domain class.
  • grails run-app start my server again. This time I see my controller, click on it.

NullPointerException

The crazy thing is, this is after following directions from the book written by the lead developer of the Grails project! Ouch!

Here’s another example:

The getting started with Grails documentation highlights using Bootstrap.groovy in grails-app/conf as a place to inject test data.

So for example, using a domain class Bookmark, in the Bootstrap.groovy file you could write:

new Bookmark(property1: "value1", property2: "value2").save()

and have the bookmark show up in the Grails scaffolding.

With this approach I ended up with a “NoMethodException” saying that save() was not a valid method for this domain class.

Nice.

Another approach mentioned using the groovy console to add data. I could create an object, such as the Bookmark, but again I could not save it. This time it seemed like after creating it, it could not find the object in the stack.

I will continue to post about this as I find out the source of my problems.

Update 5/28/08: After rereading the extensive “how to set up a Grails project in Eclipse” documentation, it became clear that without disabling the Groovy plugin auto-compilation feature, I wouldn’t have much success. In fact, after last night’s effort I’ve made some nice progress and am beginning to see some similarities between Grails and Seam, at least in terms of work-cycle.

I’m frustrated though, by the fact that grails shell is not working for me. I believe the issue is that I’m using cygwin. It may be time to just switch over the linux entirely.

Update: The “grails console” option appears to work.
Grails Console

Update 5/29/08: One thing I’ve noticed is that given a domain object like this:

class Person {
	static optionals = ['middleInitial']
	String firstName
	String lastName
	String middleInitial
}

In the grails console, you MUST specify the optional property middleInitial in order to successfully create an object. Unfortunately, there’s no warning or explanation in the console. It just doesn’t work without all properties being passed in the constructor.

So this will work:

def p = new Person(firstName: "test", lastName: "test", middleInitial: "m")
p.save()

But this will not:

def p = new Person(firstName: "test", lastName: "test")
p.save()

And neither will this:

def p = new Person()
p.firstName = "test"
p.lastName = "test"
p.save()

But this will:

def p = new Person()
p.firstName = "test"
p.lastName = "test"
p.middleInitial  = "m"
p.save()
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

8 Comments

  1. Mike wrote:

    Just curious – do you still get the problem if you skip the step: “import project imported my project into Eclipse.”?

    Also, when you try to do the save through the console – did you use “groovyconsole” or “grails console”

    Tuesday, May 27, 2008 at 1:08 pm | Permalink
  2. jeff wrote:

    Yep, the problem occurs with the import. When I created a new project and tried the above mentioned steps without the import step, it works fine.

    Also, using the Bootstrap.groovy to add test data works fine as well.

    By console, I meant “grails shell”. When I try to persist an object like this:
    def t = new Person(firstName: “Jeff”, lastName:”Smith”).save(), the shell never responds, it just hangs.

    I’ve had the same luck with “grails console”. But yes, I’m talking about the grails console/shell, not the groovy console.

    Thanks for your questions.

    Tuesday, May 27, 2008 at 9:07 pm | Permalink
  3. mike wrote:

    There have been some issues noted with the eclipse plugin on the Grails mailing list. I don’t use Eclipse myself, but I think it had something to do with the way the Eclipse plugin compiled the groovy files…anyway, glad to hear it’s working without the import step.

    In the shell, try saving the object like this:
    def t = new Person(firstName: “Jeff”, lastName:”Smith”)
    t.save()

    (two lines instead of one)

    Wednesday, May 28, 2008 at 5:21 am | Permalink
  4. jeff wrote:

    The grails shell hangs the same way as before:

    groovy:000> def t = new Employer(name:”test”, displayName:”test”)

    Jeff@RTC ~/work/test
    $ def t = new Employer(name:”test”, displayName:”test”)
    bash: syntax error near unexpected token `(‘

    Wednesday, May 28, 2008 at 12:40 pm | Permalink
  5. Mike wrote:

    Bummer – the docs for Grails shell (http://grails.org/doc/1.0.x/ref/Command%20Line/shell.html) show that you should be able to type several commands and then type ‘go’, but I just tried that and it isn’t working. Glad to hear that you were able to get up and running with Grails console though.

    Wednesday, May 28, 2008 at 2:21 pm | Permalink
  6. Yeh Eclipse is a problem area for us right now, we are looking for ways to improve our Eclipse support. Btw you should add your blog to http://www.groovyblogs.org

    Cheers
    Graeme

    Friday, June 6, 2008 at 3:33 am | Permalink
  7. Tomas wrote:

    While I agree that Grails can have a little bit of a learning curve, I find that it is easier to learn than Django and Rails if you are already coding and writing code in Java. We’re using IntelliJ in our development and it is much much nicer and has very good Grails integration ( except for maybe auto-completion ). Best of all, adding the project simply involves opening the .project file.

    Saturday, June 7, 2008 at 12:10 am | Permalink
  8. tip wrote:

    U’ve got good pics, the site could use a tiny bit of work (no offense) its still awesome

    Sunday, January 25, 2009 at 10:48 pm | Permalink

One Trackback/Pingback

  1. Applications on Monday, July 28, 2008 at 2:00 pm

    hey…

    agree…

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*