Search This Blog

Saturday, March 6, 2010

What does "Hello World" look like in Google App Engine?

Google's App Engine is an platform as a service cloud offering that is free for most uses.  The documentation is pretty clear but I found that some of the steps did not work out of the box for me.  For that reason, I'm going to take some time and document the steps I used to get a "Hello World" web application pushed out into Google's cloud.

Context: all I want to do for now is to be able to build and deploy the simplest web application possible.  I'm using version 1.3.1 of the App Engine SDK, JDK 1.6.0_17,  Ant 1.7.1, IntelliJ IDEA 9.0.1 Ultimate Edition and Ubuntu Linux 9.10.

Step 1: get your self a Google App Engine (GAE) account and an application id.  These steps are nicely documented on the Google App Engine Experiments blog and I suggest you see his directions for getting yourself set up.

Step 2: get a project template in place.  Luckily, the GAE SDK provides a nice template complete with "Hello World" code.  Copy the contents of appengine-java-sdk-1.3.1/demos/new_project_template to your working directory. 

Step 3: edit the build.xml file so that it points to your installation of the GAE SDK.  The Ant property you are looking for is appengine.sdk.

Step 4: edit src/WEB-INF/appengine-web.xml so that that the application tag contains your application id.  If you miss this step, your application will not deploy.

Step 5:  build the project.  All you do is type ant and wait a couple seconds.  You'll see messages about classes being enhanced with DataNucleus, which is framework that sits in front of various persistence engines.  I'm guessing GAE uses this to more easily support JPA and JDO.

Step 6: run a quick test of the web app.  To do this, we'll launch the bundled web container via ant runserver.  If everything is working, you should be able to point your browser at http://localhost:8080/ and see a nice little "Hello App Engine" message.  When you are done, ctrl-c the server to shut it down (I haven't figured out a nice way to shut it down yet).

Step 7: deploy the WAR into the cloud.  The Ant file has an update target but that failed for me.  ant update resulted in "Your authentication credentials can't be found and may have expired. Please run appcfg directly from the command line to re-establish your credentials."  I found a thread that seems to indicate that the cookie used to store credential expires after 24 hours.  This is what I did to make things work: /opt/appengine-java-sdk-1.3.1/bin/appcfg.sh update www followed by ant update the rest of the day.  To verify that my app was uploaded I visited http://myappid.appspot.com/ and was pleased to see the app running as expected.  IntelliJ IDEA has support for GAE and I verified that I can use that as well to upload the application.

It is fairly obvious that the GAE build system expects a certain directory structure layout and performs byte code manipulations -- it isn't just a simple matter of compiling .java files and packaging them up into a WAR file.  To help me learn how the build system works, I plan on porting the build to use Gradle.  I also have a Spring MVC application based on Spring 3.0.0 that I'm going to port over to GAE to learn about the restrictions GAE imposes on your application.  Expect posts on those two topics in the not so distant future.

No comments:

Post a Comment