Fun with Groovy and Grails

Brian LeGros | July 16th, 2007 | programming  

Ok, I’m a ColdFusion/Java programmer. I like ColdFusion for the web-tier, but sometimes I feel its way to verbose. I like Java for the application tier but I’m not a fan of static typing and some of the things that Java brings with it (i.e. – checked exceptions). I’ve been hearing/reading a lot of cool stuff about dynamic types in .NET, all of the cool stuff that Ruby can do, and how Smalltalk has had similar features for years now. With all of the press things like JSR-223 and the DLR have been getting, I figured it was time for me to start learning some new things (I will somehow figure out how this will lead to less responsibility later).

So I looked at JRuby for a little while and got too intimidated to learn a new syntax and new language constructs. This may seem silly, but I’m afraid of everything. So in the spirit of trying to learn too little at once, I started to look into Groovy. Dick Wolf from the Java Posse is always talking about it, so I figured, maybe its an easier transition for my Java syntax mind. After doing a few tutorials and playing with the console that ships with it (cool little Java app), I got pretty comfortable, pretty fast. The syntax is based on Java with, what my Ruby friends (Tan and Dyler) would call, syntactical sugar available to make things like file IO, regular expressions, database connectivity, and collections much simpler. The availability of closures alone just made it that much easier to get into Groovy. Groovy also has the notion of formal properties so that was nice too. Oh, and Groovy is written entirely in Java and supports JSR-223 so bi-directional Java/Groovy integration is really straight forward.

In any case, after a few walkthroughs, I wanted to get some instant gratification, so I tried out Grails. Now one of the first things I noticed about Grails which was a disappointment, but not a big deal, was how slow the generation tools were. Even though you are running them a discrete number of times for an application and its just to get started … slow. Now that being said, wow this is a cool framework. I may later find that’s its not all that great, but what I thought was cool about it was I understood the technologies used to build it! The build tools and generator tools are coordinated from Ant using Ivy for dependency management. The web server that comes bundled with each app is Jetty. The framework uses Spring WebFlow as the foundation for “wire framing” the site’s paths and routing. Spring is available for use with your Java classes (and I think Groovy classes as well). The GORM project is a wrapper around Hibernate 3 that Grails uses for its scaffolding (instead of ActiveRecord as in Rails). I thought it was also pretty cool that Grails uses closures as the foundation for its framework, rather than the extension of a set of base classes. Additionally, there is a notion of services which they advise using to implement business rules, so extending an existing Java SOA would get a cinch.

Some more cool stuff about Groovy and Grails, an Eclipse plugin exists that has some helpful IDE features, the generator tools generate IDE metadata so any Grails project can be imported into Eclipse, IntelliJ, or Netbeans, and Groovy and Grails both have lots of documentation to read through. I was able to get a scaffolded site up and running and start messing with the Spring and Hibernate mapping files (for fun) in under 5 minutes.

Groovy is 1.0 and Grails is at 0.5.6, so I’m not sure if its something we could begin using at work, but it feels much more natural to bridge the Java gap than JRuby, learning curve and all. As I mess with Grails and Groovy some more I’ll try to post some code samples. Now, like a good programmer, I’m going to learn a framework before I have any real clue on how the underlying language works.

;)



Tags: , , , , ,

Discussion

  1. Maxim Porges Says:

    Frameworks are for chumps. Pull your skirt up and write some spaghetti code, already. :)

    - max

  2. Graeme Rocher Says:

    Thanks for the comments. Regarding the generation tools, the speed of these is fixed (or massively improved) in the upcoming 0.6 release

  3. Brian LeGros Says:

    Wow, thanks for stopping by Graeme! I’m definitely new to Groovy/Grails in general but I’m excited to hear that there is a speed up for the tools in the next release.

  4. mkleen Says:

    >I like Java for the application tier but I’m not a fan of >static typing ….

    So why don’t you lust work with plain “java.lang.objects” everywhere, and make in this way no type declaration ? I don’t think this works much better. Just because the typesystem of java is a bit messy, means that no typesystem is a better solution !

    mkleen

  5. Brian LeGros Says:

    @mkleen

    Unfortunately Java prohibits you from using approaches like “duck typing”. If everything was treated as an instance of Object, the Java compiler wouldn’t allow me to make calls to methods associated with custom behaviors because the type associated with the class Object doesn’t have those custom behaviors(the code wouldn’t compile). In languages like ColdFusion, Ruby, and Groovy “duck typing” is possible however, so what you’re suggesting is feasible.

  6. Mike Jeppesen Says:

    Venkat Subramaniam gave a presentation on Groovy and Grails to SeaJUG last month. One of his suggestions was that until you get comfortable enough to use it for your regular work tasks, you might just start out using it to unit test your Java rather than JUnit. It’s got all of the unit test stuff, including mock objects, included.

  7. Brian LeGros Says:

    @Mike – That is a great point. I’m currently looking into GAnt and I like what I’m seeing thus far (I’ve had plenty of woes working with plain old Ant). I saw a post on mock object in Groovy but haven’t looked into it too much yet. Thanks for the heads up on the presentation, I’m gonna go searching for it tonight.

  8. Dan Vega Says:

    I am a full time ColdFusion developer and I am in the same boat as you. The second line of this article is the same problem I have sometimes.

    “I like ColdFusion for the web-tier, but sometimes I feel its way to verbose.”

  9. Brian LeGros Says:

    @Dan – Yeah I can totally understand your sentiment. The more I work with languages like ActionScript, Groovy, and JavaScript, working with CF has now been escalated to being cumbersome.

  10. David Woodbury Says:

    Hi Brian:

    Will, its been a couple of years since this post, what do you think now? Esp in relation to CF? Thanks!

  11. Brian LeGros Says:

    @David – Thanks for stopping by. I’ve written another blog post about my experiences with Grails recently @ http://www.brianlegros.com/blog/2009/08/15/experiences-from-using-grails-to-migrate-legacy-web-services/.

    In relation to Groovy/Grails vs CFML, I’d say I’m pretty much a convert, although I think Scala and Lift deserve a good look as well which I hope to get to. CFML, even with better cfscript support in 9, still is very verbose and working with Groovy as a language is exponentially better, IMO. Adobe’s CF implementation is still chocked full of features however, I think that ceiling has finally been hit where, it’s just not enough to keep my interested as a developer.

Add A Comment