Groovy and Grails in Google App Engine

25
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Groovy and Grails in Google App Engine Benefit from a Java-like dynamic language to be more productive on App Engine Guillaume Laforge Head of Groovy Development

description

In this session, Guillaume Laforge, Groovy project manager, will walk you through a rapid introduction to Groovy, to get you up-to-speed with this popular JVM language. Then, he will guide you throughout demos on how to build some Web 2.0 mashups in a few lines of Groovy code, as well as how to deploy Grails application in the cloud. He'll then close the session showing you various ways you can leverage Groovy to even further streamline and simplify the development of App Engine applications, thanks to several tips and tricks only a dynamic language like Groovy can provide.Out of the session, you'll know:- enough of Groovy to get you started with the language- how to setup and configure a new App Engine application to use Groovy- how to write your first Groovy applications- how to deploy Grails applications on App Engine- how you can leverage the Groovy dynamic language to simplify the development of App Engine Groovy-powered applicationsGuillaume Laforgehttp://www.bestechvideos.com/2009/06/29/google-i-o-2009-groovy-and-grails-in-app-engine

Transcript of Groovy and Grails in Google App Engine

Page 1: Groovy and Grails in Google App Engine

Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Groovy and Grailsin Google App Engine

Benefit from a Java-like dynamic language to be more productive on App Engine

Guillaume Laforge

Head of Groovy Development

Page 2: Groovy and Grails in Google App Engine

2Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

• Groovy Project Manager

• JSR-241 Spec Lead

• Head of Groovy Developmentat SpringSource

• Initiator of the Grails framework

• Co-author of Groovy in Action

• Speaker: JavaOne, QCon, JavaZone, Sun TechDays, Devoxx, The Spring Experience, JAX, Dynamic Language World, IJTC, and more...

Guillaume Laforge

2

Page 3: Groovy and Grails in Google App Engine

3Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

• Groovy overview

–Features at a glance

–Groovlets

–Groovy templates

• Grails overview

–The Grails stack

–The App Engine plugin

• Summary

• Q&A

3

Page 4: Groovy and Grails in Google App Engine

4Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

A few words about Groovy

• Groovy is a dynamic language for the JVM

–with a Meta Object Protocol

–compiles directly to bytecode, seamless Java interop

• Open Source ASL 2 project hosted at Codehaus

• Relaxed grammar derived from Java 5

–Java 5 features out-of-the-box: annotations, generics, static imports, enums, varargs...

–borrowed good ideas from Ruby, Python, Smalltalk

– flat learning curve for Java developers

4

Page 5: Groovy and Grails in Google App Engine

5Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Features at a glance

• Fully Object-Oriented

• Joint compiler: seamless Java integration

• Closures: reusable/assignable code blocks

• Properties: no more getters / setters

• Optional typing: you decide!

• Various shortcut notations: less verbose than Java

• Handy wrapper APIs

–Groovlets, XML, JDBC, JMX, template engine, Swing UIs, collections...

• Strong ability for authoring Domain-Specific Languages

5

Page 6: Groovy and Grails in Google App Engine

6Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

A “normal” servlet

• Writing a servlet is no different than a classical standard Java servlet

–only a bit leaner: properties, semis, public keyword...

• import javax.servlet.http.*

class HelloServlet extends HttpServlet {

void doGet(HttpServletRequest req,

HttpServletResponse resp)

throws IOException {

resp.contentType = "text/plain"

resp.writer.println "Hello World!"

}

}

6

Page 7: Groovy and Grails in Google App Engine

7Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Groovylets: Groovy servlets

• General idea

–use scripts over classes

–use Groovy’s markup builder to easily create HTML

• html.html {

head {

title "Hello"

}

body {

p "Hello Groovy World!"

}

}

7

Page 8: Groovy and Grails in Google App Engine

8Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Groovylets: Groovy servlets

• Refererence the GroovyServlet in web.xml

• Add a URL mapping

• <servlet>

<servlet-name>GroovyServlet</servlet-name>

<servlet-class>

groovy.servlet.GroovyServlet

</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>GroovyServlet</servlet-name>

<url-pattern>*.groovy</url-pattern>

</servlet-mapping>

8

Page 9: Groovy and Grails in Google App Engine

9Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Groovy templates

• Groovy comes built-in with templating capability

• import groovy.text.SimpleTemplateEngine

def text = '''Dear $firstname,So nice to meet you in <% print city %>.See you in ${month},<%= signed %>'''

def binding = [firstname: "Guillaume", month: "June",city: "San Francisco", signed: "Duke"]

def engine = new SimpleTemplateEngine()

template = engine.createTemplate(text).make(binding)

9

Page 10: Groovy and Grails in Google App Engine

10Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

The Servlet template

• Similar to JSPs– <html>

<body>

<p>${message}</p>

</body>

</html>

• In your web.xml– <servlet>

<servlet-name>GroovyTemplateServlet</servlet-name>

<servlet-class>

groovy.servlet.TemplateServlet

</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>GroovyTemplateServlet</servlet-name>

<url-pattern>*.gtpl</url-pattern>

</servlet-mapping>10

Page 11: Groovy and Grails in Google App Engine

11Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

• A thin layer on top of:

–Groovlets & servlet template

–Google App Engine Java SDK APIs

• What it does

–Inject all the services in servlets and templates

–Adds syntax sugar and dynamic methods to the SDK API

11

F GAELYK

Page 12: Groovy and Grails in Google App Engine

12Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

• Groovy overview

–Features at a glance

–Groovlets

–Groovy templates

• Grails overview

–The Grails stack

–The App Engine plugin

• Summary

• Q&A

12

Page 13: Groovy and Grails in Google App Engine

13Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

From 10,000 feet...

13

QuickTime™ and aBMP decompressor

are needed to see this picture.

=

+ +

Page 14: Groovy and Grails in Google App Engine

14Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

In a few bullet points...

• Grails is a full stack web app development env.

–built on rock-solid and proven OSS frameworks

•Spring, Spring MVC, Spring WebFlow

•Hibernate, SiteMesh, Quartz

•Jetty, HSQLDB

–provides its ORM layer on top of Hibernate

–has got its own integrated build system and CLI

• Follows good and lean principles

–Convention over Configuration, MVC, DRY...

• Provides powerful time-saving capabilities

–Scaffolding, an extensible plugin system, taglibs, templates, and more

14

Page 15: Groovy and Grails in Google App Engine

15Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

The big picture

15

Page 16: Groovy and Grails in Google App Engine

16Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

The standard project structure

16

M

V

C

Page 17: Groovy and Grails in Google App Engine

17Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

The App Engine plugin

• Grails has got a very extensive and rich plugin system and ecosystem

–100+ existing and useful plugins available

•GWT, Flex, YUI, iUI, JMS, search, security, testing...

• A Google App Engine plugin was developed

– following the Grails development flow

–abstracts the app engine commands for deployments

– leverages the local engine

17

Page 18: Groovy and Grails in Google App Engine

18Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Setting up your project

• Create your Grails application as usual•grails create-app myapp

• Specify the App Engine app id•google.appengine.application = "myapp"

• Uninstall hibernate and install the GAE plugin•grails uninstall-plugin hibernate

grails install-plugin app-engine

• Run the application locally•grails app-engine run

18

Page 19: Groovy and Grails in Google App Engine

19Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Deploying applications

• Set the version number to 1, as Grails uses a different numbering scheme (x.y)

•grails set-version 1

• Get your application ready•grails app-engine package

• On first deployment•$APPENGINE_HOME/appcfg.sh update ./target/war

• For subsequent deployments•grails app-engine deploy

19

Page 20: Groovy and Grails in Google App Engine

20Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20

Page 21: Groovy and Grails in Google App Engine

21Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

• Groovy overview

–Features at a glance

–Groovlets

–Groovy templates

• Grails overview

–The Grails stack

–The App Engine plugin

• Summary

• Q&A

21

Page 22: Groovy and Grails in Google App Engine

22Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Summary

• Groovy

–Groovy is a first-class citizen on Google App Engine

–Lots of shorcut notations can streamline your code

–Groovy servlets and templates are great for crafting small applications

–Gaelyk further simplifies this pure Groovy approach

• Grails

–Grails is the next-generation web development stack

–Developing Grails application on the Google cloud is almost as lean and simple as for the usual crop of application servers

22

Page 23: Groovy and Grails in Google App Engine

23Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Further information

• Groovy in Google App Engine

–http://blog.springsource.com/2009/04/07/write-your-google-app-engine-applications-in-groovy/

• Grails in Google App Engine

–the Grails 1.1.1 release and App Engine screencast

•http://blog.springsource.com/2009/05/14/grails-111-released-with-google-appengine-support/

–the Google App Engine Grails plugin

•http://grails.org/plugin/app-engine

23

Page 24: Groovy and Grails in Google App Engine

24Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

• Groovy overview

–Features at a glance

–Groovlets

–Groovy templates

• Grails overview

–The Grails stack

–The App Engine plugin

• Summary

• Q&A

24

Page 25: Groovy and Grails in Google App Engine

25Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Q&A

• Questions & perhaps...

even answers :-)

25