(Quick Reference)

1 Introduction - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari

Version: 3.1.1

1 Introduction

Java web development as it stands today is dramatically more complicated than it needs to be. Most modern web frameworks in the Java space are over complicated and don't embrace the Don't Repeat Yourself (DRY) principles.

Dynamic frameworks like Rails, Django and TurboGears helped pave the way to a more modern way of thinking about web applications. Grails builds on these concepts and dramatically reduces the complexity of building web applications on the Java platform. What makes it different, however, is that it does so by building on already established Java technologies like Spring and Hibernate.

Grails is a full stack framework and attempts to solve as many pieces of the web development puzzle through the core technology and its associated plugins. Included out the box are things like:

  • An easy to use Object Relational Mapping (ORM) layer built on Hibernate
  • An expressive view technology called Groovy Server Pages (GSP)
  • A controller layer built on Spring MVC
  • An interactive command line environment and build system based on Gradle
  • An embedded Tomcat container which is configured for on the fly reloading
  • Dependency injection with the inbuilt Spring container
  • Support for internationalization (i18n) built on Spring's core MessageSource concept
  • A transactional service layer built on Spring's transaction abstraction

All of these are made easy to use through the power of the Groovy language and the extensive use of Domain Specific Languages (DSLs)

This documentation will take you through getting started with Grails and building web applications with the Grails framework.

1.1 What's new in Grails 3.1?

Grails 3.1 includes the following new features.

Spring Boot 1.3 and Spring 4.2

Grails 3.1 has been upgraded to Spring Boot 1.3 and Spring 4.2.

1.1.1 Improvements to Grails 3 Profiles

Profile Publishing and Repositories

The following improvements are available in Grails profiles:

  • Profiles are now published as regular JAR files to any Maven compatible repository (Artifactory, Nexus etc.).
  • Additional profiles can be created easily with the new create-profile command.
  • Profiles can now contribute to the generation of the build
  • Profiles can now have one or many features

For more information see the new section on Profiles in the user guide.

1.1.2 REST API and AngularJS Profiles

REST Profile

A new profile is available designed for the creation of pure REST applications without a UI.

To create a REST application use the rest-api profile as an argument to create-app:

$ grails create-app myapp --profile=rest-api

In earlier milestones this profile was named web-api. The profile has been renamed rest-api which more appropriately describes its purpose.

Then start interactive mode to see the available commands for the profile:

$ cd myapp
$ grails

If you hit TAB you will notice code generation commands specific to the profile including:

  • create-domain-resource - Creates a domain class annotated with the Resource annotation)
  • create-restful-controller - Creates a controller that extends RestfulController.

JSON and Markup Views

The REST profile includes the ability to define JSON and Markup views and the build.gradle features the ability to compile these views for production use.

The REST profile also creates JSON views to render the index action and common commands such as generate-views have been overridden to generate JSON views.

AngularJS Profile

An initial version of the AngularJS profile is now available, making it easier to create and integrate AngularJS with Grails 3 applications.

To create a Grails 3 AngularJS application use the angular profile as an argument to create-app:

$ grails create-app myapp --profile=angular

Then start interactive mode to see the available commands for the profile:

$ cd myapp
$ grails

You will notice new commands such as create-ng-component, create-ng-controller etc. that help you get going creating an AngularJS application.

The build.gradle is also preconfigured with the necessary Gradle plugins to integrate AngularJS with Asset Pipeline. The created Angular application can be found in grails-app/assets/javascripts.

1.1.3 GORM 5 Suite

Grails 3.1 ships with GORM 5 which is a brand new release of GORM supporting the following technologies:
  • Hibernate 3, 4 and 5 - for SQL databases GORM for Hibernate now supports the latest Hibernate 5.x release
  • MongoDB 3.x - GORM for MongoDB has been upgraded to the MongoDB 3.x Java driver and supports codec based persistence
  • Neo4j 2.3.x - GORM for Neo4j has been significantly improved and support the latest release of Neo4j
  • Cassandra - GORM for Cassandra supports the latest 2.0.x drivers

For more information refer to the new GORM 5 website.

1.1.4 Plugin Publishing Plugins

New Gradle plugins are available to simplify publishing of plugins and profiles.

To utilize the plugin apply the org.grails.grails-plugin-publish plugin (after any existing Grails plugins for Gradle):

apply plugin: "org.grails.grails-plugin"
apply plugin: "org.grails.grails-plugin-publish"

For a profile the grails-profile-publish plugin can be used instead:

apply plugin: "org.grails.grails-profile"
apply plugin: "org.grails.grails-profile-publish"

Then configure the plugin. For example:

grailsPublish {
    user = 'user'
    key = 'key'
    githubSlug = 'foo/bar'
    license {
        name = 'Apache-2.0'
    title = "My Plugin Title"
    desc = "My Plugin Description"
    developers = [johndoe:"John Doe"]

The user and key are your Bintray credentials. With this done you can continue to use bintrayUpload to publish your plugin. In addition, if you wish to update the Grails plugin portal, you simply need to configure your grails.org credentials:

grailsPublish {
    portalUser = "..."
    portalPassword = "..."

Then call notifyPluginPortal to update the Grails.org Plugins website:

gradle notifyPluginPortal

1.2 What's new in Grails 3.0?

This section covers the new features that are present in 3.0 and is broken down into sections covering the build system, core APIs, the web tier, persistence enhancements and improvements in testing. Note there are many more small enhancements and improvements, these sections just cover some of the highlights.

1.2.1 Core Features

Groovy 2.4

Grails 3.0 comes with Groovy 2.4 which includes many new features and enhancements.

For more information on Groovy 2.4, see the release notes for more information.

Spring 4.1 and Spring Boot 1.2

Grails 3.0 comes with Spring 4.1 which includes many new features and enhancements.

In addition, Grails 3.0 is built on Spring Boot 1.2 which provides the ability to produce runnable JAR files that can embed Tomcat, Jetty or Undertow containers.

Gradle Build System

Grails 3.0 deprecates the older Gant-based build system in favour of a new Gradle-based build that integrates closely with the Gradle plugin ecosystem.

See the new section on the new Gradle build for more information.

Application Profiles

Grails 3.0 supports the notion of application profiles via a new profile repository. A profile encapsulates an application structure, set of commands, plugins and capabilities. For example the "web" profile allows construction of web applications deployable to a Servlet container. In the future more profiles will be developed targeting different environments.

See the new section on Profiles for more information.

Redesigned API based on Traits

The Grails API has been redesigned so that public API is correctly populated under the grails. package whilst private / internal API that is subject to change can be found in the org.grails. package. The core API has also been rewritten and based around the Groovy Traits.

See the new documentation on Grails 3.0's core traits for more information.

1.2.2 Web Features

New Interceptors API

In previous versions of Grails, filters were used to define logic that intercepts controller action execution.

As of Grails 3.0, this API is deprecated and has been replaced by the new Interceptor API. An example interceptor can be seen below:

class MyInterceptor {

boolean before() { true }

boolean after() { true }

void afterView() { // no-op }


1.2.3 Development Environment Features

New Shell and Code Generation API

Replacing Gant, Grails 3.0 features a new interactive command line shell that integrates closely with Gradle and provides APIs for writing scripts that interact with Gradle and perform code generation.

The new shell integrates closely with the concept of application profiles with each profile capable defining profile specific commands. As with previous versions of Grails, plugins can define new shell commands that can invoke Gradle or perform code generation and project automation tasks.

See the new guide on Creating Custom Scripts for more information.

Enhanced IDE Integration

Since Grails 3.0 is built on Gradle, you can now import a Grails project using IntelliJ community edition or GGTS's Gradle tooling support without the need for Grails specific tooling. Grails 3.0 plugins are published as simple JAR files greatly reducing the need for additional IDE support specific to Grails.

Application Main Class

Each new Grails 3.0 project features an Application class that has a traditional static void main signature, meaning to run or debug a Grails 3.0 application from an IDE like IntelliJ or GGTS you can simply right-click on the Application class and execute to start your Grails application. All Grails 3.0 tests can also just be run from the IDE directly without needing to resort to the command line (even integration / functional tests!).

1.2.4 Testing Features

Integration and Geb Functional Tests

Grails 3.0 supports built in support for Spock/Geb functional tests using the create-functional-test command. Functional tests are based on Spring Boot's test running mechanism and load the application just once for an entire suite of tests. The tests can be run from and IDE and don't require the command line.

Gradle Test Running

Since Grails 3.0 is built on Gradle the test execution configuration is much more flexible and can easily configured to execute in parallel.