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.

  • http://twitter.com/mohitkanwal Mohit Kanwal

    How does one clear the database using rake for Grails, for rails its standard but don’t know about grails !

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

      You shouldn’t need to use rake.  I’m not sure if there is a better way, but in the past I’ve just done:

      heroku addons:remove shared-database
      heroku addons:add shared-database

  • Travis

    very helpful, thank you!

  • Kevin Roberts

    Which versions of Grails does Heroku support? On 2.2.0…..
    I get
    —–> Error installing Grails framework or unsupported Grails framework version specified. Please review Dev Center for a list of supported versions

    Will the latest version get support soon?

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

      The Heroku Buildpack for Grails is at: https://github.com/heroku/heroku-buildpack-grails

      I’m not sure which versions they support but you could either file a support request or fork the buildpack and add support for any version you want.

      • Kevin Roberts

        Thanks, and it looks like they just added support for 2.2.0.

  • Keith Thomas

    Hi James, is there something more current than this. I’m blocked with nothing but trial and error to go on.

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

      Is this not working anymore?

      • Keith Thomas

        lol, it is working better than this comments section which just seems to have swallowed my reply :-/

        I’m on the verge of giving up. I think I need something more mature and not with surprising dependencies that have nothing to do with my dev environment, e.g. git. My whole local dev and deploy environment is being obstructed by git now.

        “git reset” shows this error now,

        Unstaged changes after reset:
        M grails-app/conf/BuildConfig.groovy

        Shame. Sorry.

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

          I’m not totally sure what you are asking. Are you wondering how to commit that file?

  • Matt Ganski

    I am trying to get this working on a grails maven project. I keep getting “Cannot create JDBC driver of class ‘org.postgresql.Driver’ for connect URL in the heroku logs on startup.

    I can run mvn grails:run-app -Denv=production

    locally with this same database configuration and everything starts up fine.

    The postgressql driver is a dependency in my pom. Here is the heroku log:

    2013-04-30T07:02:28.712240+00:00 app[web.1]: 2013-04-30 07:02:28,711 [main] ERROR context.GrailsContextLoader – Error initializing the application: Error creating bean with name ‘transactionManagerPostProcessor’: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘transactionManager’: Cannot resolve reference to bean ‘sessionFactory’ while setting bean property ‘sessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sessionFactory’: Cannot resolve reference to bean ‘hibernateProperties’ while setting bean property ‘hibernateProperties’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘hibernateProperties’: Cannot resolve reference to bean ‘dialectDetector’ while setting bean property ‘properties’ with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dialect

    2013-04-30T07:02:28.712240+00:00 app[web.1]: Detector’: Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class ‘org.postgresql.Driver’ for connect URL ‘jdbc:postgresql://ekkzwtficvxrrh:KK8jFm5CTjHgZykRvz4oxAYAFV@ec2-54-243-229-57.compute-1.amazonaws.com:5432/de4hf6meo68ftk’

  • Masoud Salehi

    James,man i just deployed my first app on a cloud and that has been the thing in my head for almost two years. I did it with your tutorial and i guess i will never 4get your name James Ward! Thanks man!

  • http://techlounge101.blogspot.com Kevin Tan

    I’ve added the Grails wrapper into the Grails project.

    Tried pushing to my heroku master and this is what I got:

    —–> Grails app detected

    —–> Grails 2.3.1 app detected

    WARNING: The Grails buildpack is currently in Beta.

    —–> Installing OpenJDK 1.6…done

    —–> Executing ./grailsw -Divy.default.ivy.user.dir=/app/tmp/cache compile –non-interactive

    Exception in thread “main” java.lang.NoClassDefFoundError: org/grails/wrapper/GrailsWrapper

    Caused by: java.lang.ClassNotFoundException: org.grails.wrapper.GrailsWrapper

    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)

    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)

    Could not find the main class: org.grails.wrapper.GrailsWrapper. Program will exit.

    ! Failed to build app

    ! Push rejected, failed to compile Grails app

    Is there something that I missed out?

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

      I’m not sure what the problem is. Can you file a bug? https://github.com/heroku/heroku-buildpack-grails

      • http://techlounge101.blogspot.com Kevin Tan

        Solved it. So I have to check in that wrapper folder that contains the Grails runtime so that Heroku would act according to it. But sadly the project crashed upon deploying since deployment takes more than a minute.

        This means for a Grails project, it will need more than 1 dyno to work.



  • View James Ward's profile on LinkedIn