Bristle Software Groovy/Grails Tips

This page is offered as a service of Bristle Software, Inc.  New tips are sent to an associated mailing list when they are posted here.  Please send comments, corrections, any tips you'd like to contribute, or requests to be added to the mailing list, to

Table of Contents:
  1. Groovy
    1. Groovy Intro
  2. Grails
    1. Grails Intro
    2. Grails 2.0 Features
Details of Tips:
  1. Groovy
    1. Groovy Intro

      Last Updated: 4/16/2012
      Applies to: Groovy 1.0+

      All Java programmers should be paying attention to the new languages that run on the JVM, especially Groovy, Scala and Clojure.  They generate the same format of byte codes as Java, so they run on the same JVM, which means you can call back/forth between them and Java.   So, you can continue using all of your existing custom and 3rd party Java libraries.  But, they add features that Java is missing.

      Groovy is an especially easy transition from Java because most of your existing Java code is already valid Groovy code. Groovy adds:

      1. Optional syntactic looseness
      2. Optional dynamic typing
      3. The Groovy JDK -- additional methods on the normal Java classes (for example, it adds a ton of additional methods to the Java String class)
      4. The Groovy API -- additional classes not found in Java
      5. Additional features, like closures, native maps and lists, additional operators, etc.
      6. etc.

      More detail soon...  For now, I just wanted a placeholder to set the stage for other tips.


  2. Grails
    1. Grails Intro

      Original Version: 4/16/2012
      Last Updated: 4/17/2012
      Applies to: Groovy 1.0+, Grails 1.0+

      Grails is a Rails-like MVC framework built on Groovy, it adds:

      1. An MVC Framework
      2. Easy way to use Spring and Hibernate (w/o XML config files)
      3. Ant/Ivy builds with Maven-style dependency management using Maven repos
      4. GSP (Groovy Server Pages) are better than JSP (JavaServer Pages)
      5. Lots of plugins
      6. Backed by SpringSource, which is now owned by VMWare
      7. etc.

      More detail soon...  For now, I just wanted a placeholder to set the stage for other tips.

      Thanks to Bob Rodini for keeping me honest about the level of Maven integration!


    2. Grails 2.0 Features

      Last Updated: 4/12/2012
      Applies to: Grails 2.0+

      Here are notes from Jeff Brown's "What's New In Grails 2.0?" talk at Philly ETE 2012:

      Jeff Brown, Core Member, Grails team, Sr. Engineer, SpringSource.
      - Abstract:
        In this session, Grails core developer Jeff Brown will deliver
        an update on the latest and greatest features of the Grails
        framework -- a dynamic, web application framework based on the
        Groovy language and designed for Spring. Jeff will cover all the
        new features of Grails 2.0 including agent-based reloading, unit
        testing mixins, really powerful new ways to express GORM queries
        and more. Attendees will gain key knowledge of the latest
        techniques for building Grails applications on the Spring
        platform.This talk will be very interesting to new users, and
        even more so for Grails 1.x developers looking to upgrade.
      - New Features of Grails 2.0.3
        - Command line vs new Grails console
          - Was: grails create-app, grails create-domain-class, etc.
          - Now: grails command launches console with JVM and Groovy runtime
                 loaded.  Faster for subsequent commands.
          - Autocompletion for commands and for artifact names.
        - Better display of assertion failures
        - Better Unit Test Template
        - Better Documntation Template so docs for your plugins look like
          standard Grail docs
        - Enhanced Error Reporting (runtime errors) in browser
          - Source code lines in context
        - H2 Console
          - H2 replaces HSQLDB as the in-memory DB
          - Console lets you browse the schema, etc.
          - Provided by H2, not SpringSource
        - New Automatic Reloading
          - Was: ClassLoader approach, had problems
          - Now: Reloading agent at runtime
            - More robust, some things could not be reloaded and now can:
              - Type service references
              - Domain classes
              - src/groovy, src/java, etc...
              - Config.groovy?  Yes, he thinks so.  File a JIRA if not true.
        - PERMGEN problems reduced (question from audience)
        - Binary Plugins
          - Can distribute plugins w/o source
          - Makes commercial plugins more viable
          - [Anti-FOSS step, it seems to me]
        - Methods as Actions and Binding Arguments
          - No need for closure, as:
                def save = { }
          - Can do standard method as:
                def save(String name, int age) { }
          - Form params bound to method arguments
        - HTML5 Scaffolding
        - New APIs
          - renderer.render()
            - Can render the view into a string, not necessarily back to the 
            - Create a link
        - jQuery is default, not Prototype
          - Prototype is now a plugin
          - BuildConfig.groovy
            - plugins { runtime: ":jquery:version.number" ... }
        - Easy Date Parsing
          -'myDate', 'dd-MM-yyyy')
        - Customizable URL formats
          - HumanResources does not have to map to "human-resources"
          - Can do CamelCase also
        - Filter exclusions
        - Persistence stuff
          - GORM is less tied to Hibernate
            - Can use MongoDB, Cassandra, Hibernate, redis, Amazon SimpleDB,
              riak, etc.
        - "Where Queries"
          - Special where() method added to all domain classes:
                  def query = Person.where {
                      firstName == "Bart"
                  Person bart = query.find()
          - Supports all native Groovy operators (==, !== <, <=, etc.)
          - Supports all aggregate functions (avg, sum, max, min, etc.)
        - Multiple Data Sources
             class ZipCode {
                 String code
                 static mapping = {
                     datasource 'auditing'
        - GORM
          - Supports abstract base classes
          - findOrCreateWhere()
            - Create and return an object
          - findOrSaveWhere()
            - Create, save, and return an object
        - Unit testing
          - 1.0 was inheritance-based 
          - 2.0 uses mix-ins, not inheritance
          - @TestFor (MyController)
          - @Mock (Person)
          - class MyControllerUnitTests [extends Specification]
            - Since Grails parent GrailsUnitTest not required, can inherit
              from Spock's Specification if you want to use Spock.
          - Says we should all use Spock
        - In-memory GORM for testing.  Real GORM, not fake any more.
        - GitHub, not SVN, so making contributions is possible via pull
          requests, with a patch-like mechanism tracking who really wrote 
          it.  Now more people make contributions.
          - grails-core
          - grails-docs
          - GitHub was a huge win!  Git by itself was an improvement over 
            SVN, but is even better.