Run Grails on the Cloud with Heroku

Support for Grails on Heroku was recently announced and I’d like to walk you through the steps to create a simple Grails app and then deploy it on the cloud with Heroku. Before you get started install Grails 2.0.0, install the Heroku toolbelt, install git, and signup for a account. Don’t worry, you won’t need to enter a credit card to give this a try because Heroku gives you 750 free dyno hours per application, per month. (Wondering what a “dyno” is? Check out: How Heroku Works) Let’s get started.

Step 1) Create the app:

grails create-app grailbars

You’ll need to do the rest of this from inside the newly created “grailbars” directory:

cd grailbars

Step 2) Create a new domain object:

grails create-domain-class com.jamesward.grailbars.Bar

Step 3) Edit the grails-app/domain/com/jamesward/grailbars/Bar.groovy file so it looks like:

package com.jamesward.grailbars

class Bar {
    String name

Step 4) Generate the controllers and views for the Bar model:

grails generate-all com.jamesward.grailbars.Bar

Step 5) Run the app locally to test it out:

grails run-app

Now open it in your browser: http://localhost:8080/grailbars

Step 6) Heroku provides a free Postgres database that we want our application to use. The easiest way to do that is using the Heroku Grails Plugin. Install the “heroku” and the “cloud-support” plugins:

grails install-plugin heroku
grails install-plugin cloud-support

Then edit the grails-app/conf/BuildConfig.groovy file and add the Postgres JDBC driver as a dependency by adding the following to the “dependencies” section:

runtime 'postgresql:postgresql:9.1-901-1.jdbc4'

Step 7) Setup a git repository for the project which will be used for transferring the files to Heroku:

grails integrate-with --git
git init
git add grails-app test web-app
git commit -m init

Step 8) Login with the Heroku CLI:

heroku login

Step 9) Ceate a new application using the “cedar” stack:

heroku create -s cedar

Step 10) Upload your application to Heroku:

git push heroku master

Now open the application running on Heroku in your browser (the URL was in the “heroku create” and “git push” output). Verify that it works. Awesome! You just built a Grails app and deployed it on the Cloud with Heroku! For more details on using Grails on Heroku, visit the Heroku Dev Center and checkout the Heroku Grails Plugin documentation. Let me know if you have any questions. Thanks!