More Software posts
January 16, 2007
Better GWT build with maven2

I've been using the maven-googletoolkit2-plugin from http://code.google.com/p/gwt-maven/ for a few weeks and I today I needed to add GWT to another maven2 project. Since I last looked maven-googletoolkit2-plugin has moved on. The build defect I noticed last time I used it has gone and the plugins can be directly downloaded from a repository so you don't have to build them yourself, but the whole thing has become more complex and now seems to depend on tomcat (from looking at the source). This wasn't going to work for me as I'm using jetty.

So, I went with calling the GWTCompiler from an ant build.xml file and then using the maven-antrun-plugin to call the correct target in the build.xml file. Here's the plugin entry from the pom file. I'm passing into the build.xml the properties it needs. Most interesting are gwt.devJar, which is set in the profiles section of the pom to gwt-dev-mac or gwt-dev-windows depending on the platform, and pluginClasspath, which gives access to the classpath used by the plugin, including the maven-artifact-ant classes, and which you'll see used in build.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.1</version>
  <executions><execution>
    <id>CreateAdditonalArtifacts</id>
    <phase>process-classes</phase>
    <goals>
      <goal>run</goal>
    </goals>
    <configuration>
      <tasks>
       <property name="pluginClasspath" 
         refid="maven.plugin.classpath" />
       <property name="gwt.devJar" value="${gwt.devJar}"/>
       <property name="gwt.module" 
         value="com.zanthan.example.Example"/>
       <ant antfile="${basedir}/build.xml">
         <target name="CompileGWTSources" />
       </ant>
      </tasks>
    </configuration>
   </execution>
 </executions>
 <dependencies>
   <dependency>
     <groupId>org.apache.maven</groupId>
     <artifactId>maven-artifact-ant</artifactId>
     <version>2.0.4</version>
    </dependency>
    <!-- can't add gwt-dev jars here because doing so breaks the ant
    task above, instead they are in build.xml -->
  </dependencies>
</plugin>

The relevant pieces of build.xml are shown below. I'm using the dependencies ant task provided by maven-artifact-ant and made available via the typedef using the pluginClasspath property to get access to the gwt-user and gwt-dev jars directly from the repository. Then using the java task it's very easy to call the compiler.

<typedef uri="urn:maven-artifact-ant" classpath="${pluginClasspath}"
  name="dependencies" 
  classname="org.apache.maven.artifact.ant.DependenciesTask">
</typedef>
<target name="CompileGWTSources">
  <artifact:dependencies filesetId="gwtDev.classpath">
    <dependency groupId="com.google.gwt" artifactId="${gwt.devJar}" 
      version="1.3.1"/>
    <dependency groupId="com.google.gwt" artifactId="gwt-user" 
      version="1.3.1"/>
  </artifact:dependencies>
  <java fork="true" classname="com.google.gwt.dev.GWTCompiler">
    <jvmarg value="-XstartOnFirstThread"/>
    <arg value="-out"/>
    <arg value="src/main/webapp/scr"/>
    <arg value="-gen"/>
    <arg value="target"/>
    <arg value="${gwt.module}"/>
    <classpath>
      <pathelement location="src/main/gwt"/>
      <pathelement path="${pluginClasspath}"/>
      <fileset refid="gwtDev.classpath"/>
    </classpath>
  </java>
</target>

Overall a simpler solution that the maven-googletoolkit2-plugin one.

Posted by Alex at January 16, 2007 08:49 PM
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?