Skip to content

Grails

pledbrook edited this page · 22 revisions

Grails

Overview

Grails is a framework for rapidly developing web applications that can be deployed to any Java servlet container, such as Tomcat. Based on the dynamic language Groovy and the Spring framework, it brings the paradigm of Convention over Configuration to the Java platform with the expressiveness of a Java-like dynamic language.

With its focus on making things as easy as possible, Grails is a natural companion for Cloud Foundry and the two finally make the dream of one-step deployment a reality. Simply by installing the Cloud Foundry Grails plugin, you can immediately deploy an application to the Cloud Foundry service via a simple command (assuming you have an account). And if you want your application to use one of the provided services, you just have to install and use the corresponding Grails plugin, for example the Hibernate plugin for the MySQL service, the Redis plugin for Redis, etc. You can install any or even all of these into a single application.

Please see the installation instructions in the official Grails documentation for instructions on how to get going.

If you want to get started right away, try one of our sample applications:

Note that if you wish to deploy these applications yourself, you will need to deploy them under a different application name.

Grails Cloud Foundry Plugin

The Cloud Foundry plugin for Grails adds command line utilities to deploy and manage Grails applications.

To get started you can install the Cloud Foundry plugin for Grails into any Grails application via Grails' install-plugin command:

$ grails install-plugin cloud-foundry

Once this is done type the following command to get more information about the available Cloud Foundry commands:

$ grails cf-help

Assuming you have configured your Cloud Foundry credentials, tt is a simple three-step process to create and deploy your first Grails application to Cloud Foundry:

$ grails create-app my-example
$ cd my-example
$ grails install-plugin cloud-foundry
$ grails prod cf-push

For further information on deploying Grails applications to Cloud Foundry refer to the Cloud Foundry plugin documentation.

Services

There are currently 3 services available to Grails applications: SQL, Redis and MongoDB. Each service is enabled automatically if you install the equivalent plugin for either Hibernate (the default GORM implementation), Redis or MongoDB. Note that you do not need to configure any production URL or connection settings, this will happen automatically when your application is deployed. More details of each service are discussed below.

SQL

The SQL service is a MySQL database that is automatically provisioned if you have the Hibernate plugin for Grails installed. Grails features an Object-Relational mapper built on Hibernate called GORM which is used to provide transparent persistence to applications. A simple GORM entities looks like the following:

package org.bookstore
class Book {
    String title
    Date releaseDate
    Author author
}

Grails creates query methods for you, meaning no need to write SQL:

def results = Book.findAllByTitle("The Shining", [max:10, sort:"title", order:"desc", offset:100] )
results = Book.findAllByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
results = Book.findAllByReleaseDateBetween(firstDate, new Date())
results = Book.findAllByTitleLike("%Hobbit%"

The official GORM documentation (http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html) covers how to go about creating persistent classes and storing instances in a relational database.

Redis

Redis will become available to your application if you provision and bind a Redis service to your application or if you simply install the Redis plugin for Grails.

A low-level Redis API is provided allowing you to easily execute Redis commands:

redis.select(2)
redis.flushdb()
redis["message"] = "World"
println redis["message"]

In addition your can create GORM classes that are mapped to Redis via GORM for Redis. Refer to the plugin documentation for more information.

MongoDB

MongoDB will be available to your application if you provision and bind a Mongo service to your application or if you simply install the MongoDB plugin for Grails.

The MongoDB plugin for Grails allows you to map GORM entities to MongoDB Collections and provides rich querying as was as a low-level API to access the MongoDB driver for Java directly. Refer to the plugin documentation for more information.

How to

FAQ

Something went wrong with that request. Please try again.