Blogs

SpringSource Blog

Write your Google App Engine applications in Groovy

Guillaume Laforge
Google App Engine Groovy

Google App Engine Groovy

Google just announced that their Google App Engine cloud hosting platform now supports other languages than Python: namely Java and Groovy!

You can now effectively write your Google App Engine applications in Groovy!

A couple of weeks ago, the SpringSource Groovy team and the Google App Engine Java team worked together, hand in hand, to iron out the details, to ensure that the popular and award-winning Groovy dynamic language for the JVM would run well on this exciting platform. After having created together some patches for Groovy, in the area of constrained and strict security manager policies, the Groovy development team integrated these patches and released the updated Groovy 1.6.1 version in line for the D-Day. With this new release, you can directly use the "groovy-all" JAR in your WEB-INF/lib directory and get started writing your applications in Groovy, right away, and host them on Google's infrastructure.

In the rest of this article, I'll walk you through some easy steps to let you build your first Groovy powered App Engine webapp. I'll skip the basic installation steps, as they are outlined and explained very well in the App Engine documentation, and I'll focus more on the aspects of building the Groovy application per se. As you shall see, this is fairly easy.

Getting started

First of all, obviously, you'll need to have registered a Google account on Google App Engine, so that you can create applications on the platform, and be able to upload them in the cloud. You will also need to download and install the Google App Engine Java SDK. For all these steps, you should checkout the online documentation, which gives all the details you will need.

Once the SDK is installed, for the course of this tutorial, you should also download and install Groovy 1.6. You will only really need Groovy installed for the first part of this article, where we'll need to compile a servlet, otherwise, for the rest of the article, you won't need it anymore as we'll use Groovlets which are compiled by the Groovy runtime itself.

With Java, the SDK, and Groovy installed, we can proceed further and start a new project out of this Groovy-ready project template. Download that skeleton, unzip it in a directory of your liking, and let's have a look at what we have! Is it like opening a Christmas present?

Google App Engine Groovy project structure

I have unzipped the template project into a directory called gaedemo. At the root of this directory, you will see an src directory which will contain all of our Groovy and Java sources that need to be compiled (servlets, domain classes, utility classes, etc.). The deploy directory basically corresponds to our exploded webapp: you will see a classes directory for compiled classes, lib for the various JARs (the Groovy JAR as well as Google App Engine's own API JAR), and groovy for containing the Groovlets we'll develop in the second part of this article. You have also certainly noticed the appengine-web.xml file which is an App Engine-specific descriptor. The canonical web.xml file is also present, for defining your servlets, your mappings, and more.

Compiling your classes

With this overview of the project structure in mind, let's see what each key files contain. Let's start with the build.groovy file. Instead of creating an Ant build, I leveraged Groovy's AntBuilder, a lightweight DSL / scripting wrapper on top of Ant and the Ant tasks:

def ant = new AntBuilder().sequential {
    webinf = "deploy/WEB-INF"
    taskdef name: "groovyc", classname: "org.codehaus.groovy.ant.Groovyc"
    groovyc srcdir: "src", destdir: "${webinf}/classes", {
        classpath {
            fileset dir: "${webinf}/lib", {
                include name: "*.jar"
            }
            pathelement path: "${webinf}/classes"
        }
        javac source: "1.5", target: "1.5", debug: "on"
    }
}

We instanciate AntBuilder, create a property for the target WEB-INF directory, we define the groovyc Ant task which is the Groovy Joint Compiler which is able to compile both Groovy and Java interdependent classes together, by delegating the compilation of Java classes to the javac compiler — yet another proof of the seamless interoperability between both languages. After the definition of that task, we can call it to compile our source code, using the classpath made of the JARs in WEB-INF/lib and the compiled classes.

For calling that build file, granted you've installed Groovy, you will just have to use the following command to compile your project:

groovy build

Setting up the project descriptor

The appengine-web.xml file contains some metadata needed by Google App Engine for deploying your application. In particular, this is where you'll define the name of the application, or its version number. You'll have to update the file to use your own application name. So far, our descriptor looks like the following:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>myowngroovy</application>
    <version>1</version>
</appengine-web-app>

Creating your first servlet

Before diving into Groovlets, we'll start with a good old Servlet! As Google App Engine supports the Servlet 2.5 spec, we can write a simple Hello World! servlet. We create a file named HelloServlet.groovy in our src directory, which will contain the following code:

import javax.servlet.http.*

class HelloServlet extends HttpServlet {
    void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.contentType = "text/plain"
        resp.writer.println "Hello Google App Engine Groovy!"
    }
}

This looks pretty much like a normal Java servlet, although you'll notice the simpler syntax Groovy provides: the lack of semicolons, the optional public keyword, the property notation for getters/setters, the omission of semicolons.

Next step: we need to reference that servlet in web.xml, as follows:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Once the servlet is configured, we shouldn't forget to compile the servlet with our tiny build file:

groovy build

Uploading your application in the cloud!

If your Google App Engine SDK is properly configured, you should be able to run the following command, from the root of your project:

appcfg.sh update deploy/

That command will prompt for your credentials on first use, and further calls will show an output similar to the following lines:

Reading application configuration data...
Beginning server interaction for myowngroovy...
0% Creating staging directory
5% Scanning for jsp files.
20% Scanning files on local disk.
25% Initiating update.
28% Cloning 5 application files.
40% Uploading 1 files.
52% Uploaded 1 files.
90% Deploying new version.
95% Will check again in 1 seconds
98% Closing update: new version is ready to start serving.
99% Uploading index definitions.
Update complete.
Success.
Cleaning up temporary files...

If you see the word "Success", it's certainly because everything went pretty smoothly and that your application is ready to be accessed! Hitting the servlet from a URL similar to this one (depending on the name of the application you've chosen): http://myowngroovy.appspot.com/hello will show you the nice "Hello Google App Engine Groovy!" message!

Groovlets to the rescue!

Well, it's exciting to write plain old Java servlets with the Groovy language, right? It feels like the first day you had writen and made working your first servlet? Not quite so, it seems almost old-fashioned already. Fortunately, Groovy comes to the rescue with its Groovlets!

In a nutshell, Groovlets are mere Groovy scripts stored in WEB-INF/groovy, which are rendered by a Groovy servlet dispatcher, that compiles and renders those scripts.

Firstly, let's update our web.xml to add the GroovyServlet into the mix, and a URL mapping for redirecting to it all the URLs following the *.groovy pattern:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>GroovyServlet</servlet-name>
        <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GroovyServlet</servlet-name>
        <url-pattern>*.groovy</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Once this is done, we create our first Groovlet script under WEB-INF/groovy, and call it hello.groovy:

html.html {
    head {
        title "Hello"
    }
    body {
        p "Hello Groovy World!"
    }
}

This Groovy scripts uses a html variable bound to the binding of the script which is an instance of MarkupBuilder. It's a small utility DSL for creating any kind of XML or HTML markup. Instead of outputing raw HTML in the form of strings with a println statement, MarkupBuilder provides a cleaner and groovier syntax. Of course, you can make this markup however dynamic you like by mixing in some loops or conditionals, etc.

After reuploading the application, you can now access this Groovlet by hitting the URL http://myowngroovy.appspot.com/hello.groovy. No need to compile anything this time, since that's the GroovyServlet's job to compile those Groovlets scripts.

Wasn't that easy?

What's next?

We've only scratched the surface so far, but it's enough to get started with Groovy. As you'll discover in the Google App Engine APIs, there are a several interesting services that you can leverage from your Groovlets and servlets:

  • a datastore API that you can use to store your objects, either through a low-level schema-less API, or through JDO
  • an images API to do various transforms and apply filters on images
  • a mail API to send emails
  • a memcache API to cache expensive data structures or computation results
  • a URL fetcher API to retrieve remote URL content
  • a users API for your authentication needs using Google's user accounts

Of course, all these services can be used from your Groovlet scripts. You can also use third-party libraries and put them in your WEB-INF/lib. It would be interesting to investigate further the available APIs to see if a thin Groovy layer on top of them could even further simplify their usage, in a groovier way.

Currently, Groovlets and normal servlets are totally supported, but for instance, Grails applications aren't working on the current version of Google App Engine. We'll continue working on this with the Google App Engine team, so that you may use Grails as well, for more demanding applications.

GR8 Conference dedicated to Groovy, Grails and Griffon

If you want to learn more about Groovy and Grails, how to write Groovy-powered App Engine apps, you may also consider registering and coming to the GR8 Conference, a conference dedicated to Groovy, Grails and Griffon, with experts or makers of these technologies as speakers, with hands-on practical sessions. And with this announcement of the support of Groovy in App Engine, there's no doubt we'll also speak about that at the conference!

We're looking forward to reading your feedback on Groovy on App Engine, and we'd love to hear about all the suggestions you may make about how we can improve even more the experience developing Groovy apps in the cloud.

Similar Posts

Share this Post
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Slashdot
  • Technorati
  • Twitter
 

42 responses


  1. This is amazing: reading my feeds I first did read the announcement reg. Java support. After signing up, having a look at plain ol' Java Servlet stuff i got bored an moved on.

    And now your post regarding Groovy support. This is … fast …

    One small question: how much of the dark art of Groovy Metaprogramming is available in AppStore?

    Grails in AppStore might become *the* killer combo: Ease of development combined with ease of deployment, wow…


  2. Hello KoW,

    We've run most of Groovy's own unit tests from withing App Engine, to ensure most if not all the metaprogramming fu is working as expected, however, we still had some issues with running a Grails app right away. We'll need a little more work and time to ensure Grails also runs fine in that environment. Stay tuned for further announcements ;-)

    Guillaume


  3. There is no excuse whatsoever not to be learning Groovy right this minute. Awesome.


  4. Great work guys !

    Can't wait to get grails working on the appEngine too.

    Hmmm if you can integreate The App Engine datastore Api into Grails, it will rock !!

    Can't wait.

    Tarik


  5. One word: Groovy!! ;)

    I'm really looking forward to grails support, then the App Engine will really rock :P


  6. I can only echo KoW above – grails on app engine would be a killer combination. What is your strategy for doing this ? Are you trying to add support for the datastore to hibernate or will you use another plugin ? And what implications would that have for gorm ?


  7. Jon, currently, there are some weird unhelpful stacktraces when deploying Grails applications on GAE. We'll need the help of the GAE teams for debugging all this, but you can imagine how busy they are these days with the new release. Once this is working, we'll investigate what we can do to support BigTable natively in Grails. The idea would be to create a new persistence plugin with gorm-like features. So stay tuned for further announcements as we progress on all this.


  8. Excellent work from GAE and Groovy teams.
    Unfortunately I got to know about this release just now.
    Not sure will be able to make into first 10K users :(


  9. Great guys and great work! Can't wait to hear more good news from you guys!


  10. Really happy to see Groovy on GAE, but running into an issue with the example.

    I'm running the example on the local SDK (without uploading), and getting the following

    access denied (groovy.security.GroovyCodeSourcePermission /groovy/shell)

    RequestURI=/hello

    java.security.AccessControlException: access denied (groovy.security.GroovyCodeSourcePermission /groovy/shell)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:264)

    The uploaded application works fine on GAE, and also works fine both local and remote if you remove the following line -

    resp.writer.println new GroovyShell().evaluate("3*4")

    I'm on OS X. Are there java policy requirements to run groovy locally? Thanks,

    Colin


  11. Colin, yes, this is a known issue, unfortunately. When we worked on the Groovy support, it wasn't working on the local environment, although it's working in production. But it seems it hasn't been fixed in time for the D-Day. I hope they'll update the SDK to fix this issue soon.


  12. @Colin

    I've already opened an issue:
    http://code.google.com/p/googleappengine/issues/detail?id=1219

    comment and vote ;)


  13. I'm really wondering what a Grails persistence plugin for BigTable would mean.

    I was thinking of using Google App Engine for an application in the past so I dived into it a little bit. The Google datastore does not support joins, aggregate functions etc. In fact it's not a relational table.

    Now with the annoucement of support for JAva on GAE, and to see how easy it's to integrate Groovy I really got enthousiastic again with GAE (mainly because ease of deployment and scaling). But then I got bothered again with the datastore…

    But bottom line it's great news, and I'm happy that SpringSource/Groovy/Grails seem to be interested to.

    Cheers,
    Marcel


  14. Groovelets are super – but the main advantage in my mind is to be able to edit the groovy files and instantly see the change on the server. This AccessControlException is a real show stopper for me… I hope it gets sorted very soon.

    Caused by: java.security.AccessControlException: access denied (groovy.security.GroovyCodeSourcePermission /groovy/script)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)


  15. great to know that its possible to use groovy and add 3rd party libraries to WEB-INF/lib as well. now im wondering how many 3rd party libraries will actually run properly within the sandbox…

    thanks for the great post!


  16. Have found a workaround for running local groovlets as well using Google Eclipse Plugin.
    Here is the workaround -
    http://indoos.wordpress.com/2009/04/09/gaegroovy-localnremote-with-eclipse-plugin/


  17. Thanks Guillaume, you made my day. I really liked the Google App Engine concept, but could not do much about it because it lacked Java support.

    And now we know that it supports Groovy; that's amazing.

    And I also managed to get into the first 10K users. I am all excited to try out the example; and can't wait to see the Grails app running in the Google cloud!

    Amazing. Very nice work by SpringSource!

    -Deepak


  18. This does seem like a cool service but I wonder about the GAE concept. With EC2 you can have a Grails image ready to be deployed at any time and you have full control of the system.


  19. awesome! time to pick up Groovy… again!


  20. this is really nice! Waiting for Grails support!


  21. Wow, this is the best of two worlds for me. I never did get into the python having alreday invested some in the Groovy. Great news all around and thanks for the to the point intro Guillaume!


  22. A groovy shell in the cloud: http://verygroovy.appspot.com/

    I defined puts() as a method to print the output.

    Try executing something like: puts 'hello'

    Or: [1,2,3].each{ puts it}


  23. Hi Andreas, for the fun, last night, I also implemented a Groovy console:
    http://groovyconsole.appspot.com/
    You can do println's as I've rerouted the output.
    There's also live syntax highlighting.
    You can see the result, the output and the potential stacktraces.
    It's also ajaxey, for the Web 2.0 touch ;-)


  24. Hi Guillaume, great job. I admit your console has a much better look&feel (and app name). Mine isn't even web 1.0 ;)

    I think every Java application needs a Groovy console. Granted a better secured version because it's the equivalent of root access. Once you have your app in the cloud but you quickly need to manipulate data or test something, what is better suited then a Groovy console? Maybe you can user this as a selling point for Groovy. It's what we have been using Groovy for in the last 2 years.

    Since your groovyconsole is to good looking, are you willing to share the code for it?


  25. Andreas, yes, I'll have to put the code somewhere for sure! Either as a Groovy module, perhaps, or on GitHub or something like that. I'll add a comment here when it's available.


  26. I've pushed the sources on GitHub here:
    http://github.com/glaforge/groovywebconsole/tree/master


  27. http://www.androidph.com/2009/04/app-engine-tutorial-creating-beer-radar.html
    Here's my attempt to use new Java Language support for App Engine and Android as a client.


  28. Reading application configuration data…
    Beginning server interaction for myowngroovy…
    0% Creating staging directory
    5% Scanning for jsp files.
    20% Scanning files on local disk.
    25% Initiating update.
    java.io.IOException: Error posting to URL: http://appengine.google.com/api/appversion/create?app_id=myowngroovy&version=1&amp;
    403 Forbidden
    You do not have permission to modify this app (app_id=u'myowngroovy').

    :-(
    My Google account is activated, connected on the GAE dashboard with my gmail account – already having one Python app deployed for testing
    Any idea?
    Thanks


  29. Unable to upload app: Error posting to URL: http://appengine.google.com/api/appversion/create?app_id=myowngroovy&version=1&amp;
    403 Forbidden
    You do not have permission to modify this app (app_id=u'myowngroovy').

    I have the account activated, 9 applications remaining
    Is it just a pb with passwd when posting?


  30. Unable to upload app: Error posting to URL: http://appengine.google.com/api/appversion/create?app_id=myowngroovy&version=1&amp;
    403 Forbidden
    You do not have permission to modify this app (app_id=u'myowngroovy').

    I do have my account activated, 9 apps remaining.
    Is it a prob with passwd when sending the app?


  31. If anyone is interested in Spring on Google App Engine, I've written a post on how to get a basic Spring Hello World up and running: http://www.ardentlord.com/apps/blog/show/829881-spring-3-0-on-google-app-engine


  32. Hello!

    This article is excellent.
    I could upload this application to cloud,
    But I can not try this on local dev_appserver.
    How can I try application on local dev_appserver?
    #####################################################
    Following are error messages:
    1. on URL: http://localhost:8080/
    HTTP ERROR: 403
    FORBIDDEN
    RequestURI=/
    Powered by Jetty://

    2. http://localhost:8080/hello/
    HTTP ERROR: 404
    NOT_FOUND
    RequestURI=/hello/
    Powered by Jetty://


  33. You don't need Groovy to have a console in your application (although Groovy is nicer). Java 6 has a nice feature called Java Scripting, which allows you to run whichever dynamic language you want.
    I have such a console in all my applications thanks to MessAdmin 4.2 (http://messadmin.sourceforge.net). Oh, wait, this version is not released yet! Contact me directly for a copy until I get to push it to sourceForge… O:-)


  34. wow!
    This article is totally hip.


  35. Hello here's a set of tools done in flex talking amf to spring blazeDS on GAE/J cloud
    including a groovy console ;-) , app is way a proof of concept, but demonstrates few concepts.

    http://riaspringblaze.appspot.com

    For those interested in getting Adobe BlazeDS working on GAE/J here's how @

    http://martinzoldano.blogspot.com

    Cheers!


  36. Thanks for such a complete article! App engine away!


  37. From Laurent:

    Reading application configuration data…
    Beginning server interaction for myowngroovy…
    0% Creating staging directory
    5% Scanning for jsp files.
    20% Scanning files on local disk.
    25% Initiating update.
    java.io.IOException: Error posting to URL: http://appengine.google.com/api/appversion/create?app_id=myowngroovy&version=1&amp;
    403 Forbidden
    You do not have permission to modify this app (app_id=u'myowngroovy').
    :-(
    My Google account is activated, connected on the GAE dashboard with my gmail account – already having one Python app deployed for testing
    Any idea?
    Thanks

    >>

    Sodemized

    It's another scam Laurent. The federal government must have forced through impediments. Move on ..

    Centralization of your project is NOT GOOD anyway. Move on mate


  38. Asdasd, / Laurent, you have to change the appid in the appengine-web.xml file, otherwise, you're trying to deploy YOUR app on MY account!


  39. It's another scam Laurent. The federal government must have forced through impediments. Move on ..

    http://www.amlly.com/vb

    http://www.amlly.com


  40. China TwoWin Stone fabricate the absolute black granite,black slate,black basalt,black galaxy granite,shanxi black granite,granite monuments,granite memorial,granite headstones,stone monument,stone memorials,grave markers,animal memorials with good quality.


  41. Has anyone figured out if Grails/GWT app can be uploaded on GAE?
    If not what would be the choice for hosting? Thanks.


  42. Hi, nice Content, keep it up! I have bookmarked for future reference! All the best

41 trackbacks

Leave a Reply