Building Google App Engine Java projects with Maven2

Ok, so I had a whole bunch of work to do today and I’ve just thrown it out the window: Google has unleashed Java support for Google App Engine.

This is huge news. The implementation is solid (it does its best, within the restrictions of Google’s architecture, to support standards and avoid architectural lock-in).

Let’s cut to the chase: I spent this afternoon working out how to build App Engine Java projects with Maven 2 instead of Ant.

I do this for several reasons, the main one being that the CMS I’m building uses Maven as a build system and I love it. A secondary reason is that Maven easily gives me the power to target OSGI, App Engine, and plain ol’ Java Servlets with the one project.

So here we go. First, here’s a script to install some of Google’s more obscure libraries into your local Maven repository: <pre lang="bash">#!/bin/sh

install Google App Engine dependencies into local maven repository

copyright 2009 Gravity Rail Pty Ltd

export CURR=pwd export SDK=pwd export LIB=”${SDK}/lib” export VERS=”1.2.0”

cd $LIB

mvn install:install-file -Dfile=${LIB}/appengine-tools-api.jar \ -DgroupId=com.google \ -DartifactId=appengine-tools \ -Dversion=${VERS} \ -Dpackaging=jar \ -DgeneratePom=true

mvn install:install-file -Dfile=${LIB}/shared/appengine-local-runtime-shared.jar \ -DgroupId=com.google \ -DartifactId=appengine-tools \ -Dversion=${VERS} \ -Dpackaging=jar \ -DgeneratePom=true

mvn install:install-file -Dfile=${LIB}/shared/geronimo-el1.0_spec-1.0.1.jar \ -DgroupId=org.apache.geronimo.specs \ -DartifactId=geronimo-el1.0_spec \ -Dversion=1.0.1 \ -Dpackaging=jar \ -DgeneratePom=true

mvn install:install-file -Dfile=${LIB}/shared/geronimo-jsp2.1_spec-1.0.1.jar \ -DgroupId=org.apache.geronimo.specs \ -DartifactId=geronimo-jsp2.1_spec \ -Dversion=1.0.1 \ -Dpackaging=jar \ -DgeneratePom=true

mvn install:install-file -Dfile=${LIB}/shared/geronimo-servlet2.5_spec-1.2.jar \ -DgroupId=org.apache.geronimo.specs \ -DartifactId=geronimo-servlet2.5_spec \ -Dversion=1.2 \ -Dpackaging=jar \ -DgeneratePom=true

mvn install:install-file -Dfile=${LIB}/user/orm/geronimo-jpa3.0_spec-1.1.1.jar \ -DgroupId=org.apache.geronimo.specs \ -DartifactId=geronimo-jpa3.0_spec \ -Dversion=1.1.1 \ -Dpackaging=jar \ -DgeneratePom=true

mvn install:install-file -Dfile=${LIB}/user/orm/geronimo-jta1.1_spec-1.1.1.jar \ -DgroupId=org.apache.geronimo.specs \ -DartifactId=geronimo-jta1.1_spec \ -Dversion=1.1.1 \ -Dpackaging=jar \ -DgeneratePom=true

mvn install:install-file -Dfile=${LIB}/user/orm/datanucleus-appengine-1.0.0.final.jar \ -DgroupId=org.datanucleus \ -DartifactId=datanucleus-appengine \ -Dversion=1.0.0.final \ -Dpackaging=jar \ -DgeneratePom=true

cd $CURR</pre> Next, let’s modify the ant build.xml file in the root of our project slightly to take account of Maven’s default project layout: <pre lang="xml">

<import file="${sdk.dir}/config/user/ant-macros.xml" />

<path id="project.classpath">
	<pathelement path="war/WEB-INF/classes" />
	<fileset dir="war/WEB-INF/lib">
		<include name="**/*.jar" />
	</fileset>
	<fileset dir="${sdk.dir}/lib">
		<include name="shared/**/*.jar" />
	</fileset>
</path>

<target name="copyjars"
  description="Copies the App Engine JARs to the WAR.">
	<mkdir dir="war/WEB-INF/lib" />
	<copy
    todir="war/WEB-INF/lib"
    flatten="true">
		<fileset dir="${sdk.dir}/lib/user">
			<include name="**/*.jar" />
		</fileset>
	</copy>
</target>

<target name="compile" depends="copyjars"
  description="Compiles Java source and copies other source files to the WAR.">
	<mkdir dir="war/WEB-INF/classes" />
	<copy todir="war/WEB-INF/classes">
		<fileset dir="src/main/resources">
			<exclude name="**/*.java" />
		</fileset>
	</copy>
	<javac
    srcdir="src/main/java"
    destdir="war/WEB-INF/classes"
    classpathref="project.classpath"
    debug="on" />
	<!-- copy webapp files -->
	<copy todir="war/">
		<fileset dir="src/main/webapp">

		</fileset>
	</copy>
</target>

<target name="datanucleusenhance" depends="compile"
  description="Performs JDO enhancement on compiled data classes.">
	<enhance_war war="war" />
</target>

<target name="runserver" depends="datanucleusenhance"
  description="Starts the development server.">
	<dev_appserver war="war" />
</target>

<target name="update" depends="datanucleusenhance"
  description="Uploads the application to App Engine.">
	<appcfg action="update" war="war" />
</target>

<target name="update_indexes" depends="datanucleusenhance"
  description="Uploads just the datastore index configuration to App Engine.">
	<appcfg action="update_indexes" war="war" />
</target>

<target name="rollback" depends="datanucleusenhance"
  description="Rolls back an interrupted application update.">
	<appcfg action="rollback" war="war" />
</target>

<target name="request_logs"
  description="Downloads log data from App Engine for the application.">
	<appcfg action="request_logs" war="war">
		<options>
			<arg value="--num_days=5"/>
		</options>
		<args>
			<arg value="logs.txt"/>
		</args>
	</appcfg>
</target>

</project>

</pre> Finally, we put in our pom.xml file that calls ant for tasks like deployment (doesn’t quite work yet): <pre lang="xml"> 4.0.0 com.transitplatform TransitAppEngine war Transit App Engine 1.0-SNAPSHOT war/WEB-INF/classes src/main/resources src/main/java ** **/*.java maven-compiler-plugin 1.6</source> 1.6 maven-antrun-plugin 1 compile run 2 deploy run

</build>


<dependencies>
	<dependency>
		<groupId>org.apache.wicket</groupId>
		<artifactId>wicket</artifactId>
		<version>1.4-rc2</version>
	</dependency>
	<dependency>
		<groupId>org.apache.geronimo.specs</groupId>
		<artifactId>geronimo-jsp_2.1_spec</artifactId>
		<version>1.0.1</version>
	</dependency>
	<dependency>
		<groupId>org.apache.geronimo.specs</groupId>
		<artifactId>geronimo-jpa_3.0_spec</artifactId>
		<version>1.1.1</version>
	</dependency>
	<dependency>
		<groupId>org.apache.geronimo.specs</groupId>
		<artifactId>geronimo-jpa_3.0_spec</artifactId>
		<version>1.1.1</version>
	</dependency>
	<dependency>
		<groupId>ant</groupId>
		<artifactId>ant</artifactId>
		<version>1.6.5</version>
	</dependency>
	<dependency>
		<groupId>ant</groupId>
		<artifactId>ant-launcher</artifactId>
		<version>1.6.5</version>
	</dependency>
	<dependency>
		<groupId>commons-logging</groupId>
		<artifactId>commons-logging</artifactId>
		<version>1.1.1</version>
	</dependency>
	<dependency>
		<groupId>commons-el</groupId>
		<artifactId>commons-el</artifactId>
		<version>1.0</version>
	</dependency>
	<dependency>
		<groupId>org.datanucleus</groupId>
		<artifactId>datanucleus-core</artifactId>
		<version>1.1.0</version>
		<exclusions>
			<exclusion>
				<groupId>javax.transaction</groupId>
				<artifactId>transaction-api</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>org.datanucleus</groupId>
		<artifactId>datanucleus-jpa</artifactId>
		<version>1.1.0</version>
	</dependency>
	<dependency>
		<groupId>org.datanucleus</groupId>
		<artifactId>datanucleus-appengine</artifactId>
		<version>1.0.0.final</version>
	</dependency>
	<dependency>
		<groupId>tomcat</groupId>
		<artifactId>jasper-compiler</artifactId>
		<version>5.0.28</version>
	</dependency>
	<dependency>
		<groupId>tomcat</groupId>
		<artifactId>jasper-runtime</artifactId>
		<version>5.0.28</version>
	</dependency>
	<dependency>
		<groupId>javax.jdo</groupId>
		<artifactId>jdo2-api</artifactId>
		<version>2.3-SNAPSHOT</version>
		<exclusions>
			<exclusion>
				<groupId>javax.transaction</groupId>
				<artifactId>transaction-api</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>javax.transaction</groupId>
		<artifactId>jta</artifactId>
		<version>1.1</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>jcl104-over-slf4j</artifactId>
		<version>1.4.3</version>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-simple</artifactId>
		<version>1.4.3</version>
	</dependency>
</dependencies>


<repositories>
	<repository>
		<id>datanucleus</id>
		<name>Datanucleus Repository</name>
		<url>http://www.datanucleus.org/downloads/maven2/</url>
	</repository>
</repositories>

</project> </pre> Hope you find this useful, and good luck! Also, let me know if you have any corrections, compliments, duffel-bags-full-of-cash, etc.

Download a sample project here:

mavenappengine