Thursday, August 4, 2011

GAE (Google App Engine) development with NetBeans 7.0.1

As noted in the Google App Engine getting started guide, create a project folder with the following hierarchy -


ProjectFolder/ 
    src/ ...Java source code... 
        META-INF/ ...other configuration... 
    war/ ...JSPs, images, stylesheets, html, tiles, ...
        WEB-INF/ ...app configuration...viz. web.xml, ...
            classes/ ...compiled classes... 
            lib/ ...JARs for libraries...

Download the GAE SDK and extract it to your desired folder.  The SDK contains the GAE library jars and other files.

Download the example build.xml file from the GAE getting started tutorial and place it in the project folder.  Change it's sdk.dir property to point to the folder where you extracted the GAE SDK.

build.xml sdk.dir property

In NetBeans, create a new Java Free Form project. This project will use the Apache Ant build system which is configured using the build.xml file downloaded previously.

New Free Form project

Select project folder location and give a name to the project

Select project location

Map the targets in build.xml to NetBeans project actions. NetBeans intelligently selects the proper options to build and run the project so there's nothing to be done here.

Map build.xml targets to project actions

Locate the source folders. If the sources are in src folder then NetBeans detects it automatically so there's nothing to be done here.

Specify sources folder

Specify the classpath. This should match the path specified in build.xml. The path specified in build.xml is used for the actual build. The path specified in NetBeans is used for things like code completion and refactoring. Add all the library jars (usually in folder lib). It doesn't matter if you miss a few. You can add them later from Project => Properties whenever you find a missing reference in code.

Add library jars

And finally, specify the project output folder. This is where the compiled classes are placed. It's usually ProjectName/war/WEB-INF/classes.

Specify output folder for built classes

Here's a screenshot of my GAE project folder hierarchy.

Example project folder hierarchy

Build the project like any other NetBeans Java project. Run the project. This starts the development server listening on port 8080. Fire up a web browser and point it to your application e.g. http://localhost:8080/.

Project Run console output

Register your application on the GAE website and get it's id.  Add this id in file appengine-web.xml along with an arbitrary version number.

The GAE SDK has a commandline tool appcfg which can be used to upload your application to GAE. Open a terminal in your project folder and type
appcfg    update    war
It will ask for your google id and password during the first run but not during subsequent runs.  Sunsequent uploads can be done from within NetBeans. Map a new project action 'Update GAE' (call this whatever you like) to the build.xml update target from
Project => Properties => Build & Run => Add context menu item.

Add project action for upload target
New context menu option - Update GAE
Console ouput for Upload action

Login to your GAE dashboard and set the default version of your application to the one you uploaded.

2 comments:

  1. Thanks for this very useful post. What modification should I make to the build.xml since it uses JDO, and I use JPA?

    ReplyDelete
    Replies
    1. I'm sorry for the late reply. I'm unable to answer your query as i have been out of touch with GAE development for quite a while.

      Delete