Get ahead
VMware offers training and certification to turbo-charge your progress.
Learn more[caption id="attachment_1577" align="alignright" width="250" caption="Google App Engine Groovy"][/caption]
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.
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?
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.
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
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <application>myowngroovy</application> <version>1</version> </appengine-web-app>
import javax.servlet.http.*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.class HelloServlet extends HttpServlet { void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.contentType = "text/plain" resp.writer.println "Hello Google App Engine Groovy!" } }
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
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!
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?
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.
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.