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 Heroku.com 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 application.properties 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!

This entry was posted in Grails, Heroku. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Burt Beckwith

    It’d be cool if there were a Grails plugin that handled stuff like configuring the DataSource for you :) It could also handle stuff like MongoDB, Redis, and RabbitMQ, and Memcached. http://grails.org/plugin/heroku

    • http://www.jamesward.com James Ward

      Ah Burt.  Sorry man.  I must have written this low on caffeine or something because I totally intended to point out the plugin.  My bad.  I’ll update this post.

    • http://www.jamesward.com James Ward

      Ok, I’ve updated the article with your awesome Heroku plugin.  Thanks for pointing out my screw-up.  :)  Let me know if I missed anything else.

  • Pingback: Questa settimana in Grails (2012-06) - luca-canducci.com - Il blog di Luca Canducci: notizie, tips e nuove tecnologie dal mondo dell’IT.

  • Pingback: An Army of Solipsists » Blog Archive » This Week in Grails (2012-06)

  • http://twitter.com/mscalora Mike Scalora

    Why not use :

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

    for the first part of step 6?

    • http://www.jamesward.com James Ward

      That’s better!  Thanks.  I will update this.

  • AndyDavisDisqus

    Hey James-

    Thanks for the post.  I had a spot of trouble getting my 2.0.1 app to run and resolved it by adding…

       compile ‘:webxml:1.4.1′

    …to my dependencies.  I am pretty sure I could also have excluded databases sessions since my toy apps only use one dyno.

    For reference the log output started with:

    2012-03-31T20:29:27+00:00 app[web.1]: 2012-03-31 20:29:27.565:WARN:oejw.WebAppContext:Failed startup of context o.e.j.w.WebAppContext{/,file:/tmp/jetty-0.0.0.0-45790-BlahBlahBlah-0.1.war-_-any-/webapp/},file:/app/target/BlahBlahBlah-0.1.war

    2012-03-31T20:29:27+00:00 app[web.1]: org.springframework.beans.factory.access.BootstrapException: Error executing bootstraps; nested exception is java.lang.IllegalStateException: The database-session plugin requires that the webxml plugin be installed

  • http://twitter.com/sarbogast Sebastien Arbogast

    Unfortunately, I’m slowly discovering that traditional boot timeouts and memory constraints in Heroku are somewhat limited for a real whorl Grails application, not to mention the fact that there seems to be issues with background processes.

    • http://www.jamesward.com James Ward

      Hi Sebastien.  Thanks for your feedback!  I’ve sent it to the product team.  Can you email me with more details about the issues you are facing?  jw at heroku dot com
      Thanks!

  • Subhro

    Hello,
    I have a grails project, with domain objects in a separate plugin and some services classes in another plugin.
    in my BuildConfig.groovy i do the following 
    plugins{….compile “:ycdomainplugin:0.1″compile “:ycsiteservicesplugin:0.1″

    }

    I am getting compilation error when i do a “git push heroku master”. None of the domain classes are being resolved. 
    I have done “grails install-plugin ….” for both the above mentioned plugin.

    Please help.

    Subhro.

    • http://www.jamesward.com James Ward

      Are those plugins in a repo that is accessible from Heroku?  Does this work locally if you do a “grails compile”?

      • Subhro

        yes, it compiles and runs the app locally with grails run-app
        The plugins are placed in the following dir structure
        /home/subhro/workspace/ycadminapp (the app i am trying to deploy)
        /home/subhro/workspace/ycdomainplugin
        /home/subhro/workspace/ycsiteservicesplugin

        The bottom 2 are the plugins.
        how do i “plugins in a repo that is accessible from Heroku”? Local setup og heroku to be tweaked? 
        Thanks James for the quick response.

        Thanks,
        Subhro.

        • http://www.jamesward.com James Ward

          That is a good question.  I know how to do this with Maven:
          https://devcenter.heroku.com/articles/local-maven-dependencies

          But I’m not sure how to add unmanaged dependencies to a Grails app.  Perhaps you can just drop the jars into lib directory, add them to the git repo, commit, and then try to push again.