GWT, Maven2, and Eclipse - living happily together (Part 1)

I wanted to post some instructions for getting GWT compiler working with Maven2, at least the I way I use it in some of my gwt development. I hope developers using Maven for their build will find this useful, since unfortunately GWT  doesn’t support maven out of the box. There is a third party Maven2 plugin already available for GWT, however I personally find that using raw GWT compiler is rather more flexible.

  1. First things first, I don’t use GWT’s embedded tomcat server and rely on developing/debugging GWT apps with the -noserver option. This way I can use whatever server technology (Jetty+Spring is my favorite) I like sacrificing very little of what GWT offers.
  2. My maven pom.xml has a profile that I use to compile all my GWT code and setup my server environment for hosted GWT mode development. Here it is:


<profile>
<id>gwt</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<tasks>
<path id="compile.class.path">
<dirset dir="${basedir}">
<include name="src/main/java"/>
</dirset>
<path refid="maven.compile.classpath"/>
</path>
<!--property name="compile_classpath" refid="compile.class.path"/>
<echo message="compile classpath: ${compile_classpath}"/>
<parallel threadCount="2" failonany="yes" -->
<java classname="com.google.gwt.dev.GWTCompiler"
fork="true"
failonerror="true"
maxmemory="1024m">
<classpath refid="compile.class.path"/>
<arg value="-out"/>
<arg value="${project.build.directory}/www"/>
<arg value="com.company.module1.gwt.Module1"/>
</java>
<java classname="com.google.gwt.dev.GWTCompiler"
fork="true"
failonerror="true"
maxmemory="1024m">
<classpath refid="compile.class.path"/>
<arg value="-out"/>
<arg value="${project.build.directory}/www"/>
<arg value="com.company.module2.gwt.Module2"/>
</java>
<!--/parallel-->
<copy todir="${basedir}/src/main/webapp">
<fileset dir="${project.build.directory}/www">
<exclude name="*gwt-tmp/"/>
<exclude name="*-aux/"/>
<exclude name="**/*.cache.html"/>
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

3.  If you understand what profile above does, then the command to compile all the server and client code is something like the following: mvn clean compile -P gwt  (this compilation needs to be usually done only once)

4.  Once compilation finishes, the server (in this case Jetty) can be easily brought up with: mvn jetty:run

5. The final step is to launch the GWT hosted mode via Eclipse. Below is an example of such launch configuration:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/sample"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt; &lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER&quot; javaProject=&quot;sample&quot; path=&quot;1&quot; type=&quot;4&quot;/&gt; "/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt; &lt;runtimeClasspathEntry id=&quot;org.eclipse.jdt.launching.classpathentry.defaultClasspath&quot;&gt; &lt;memento exportedEntriesOnly=&quot;false&quot; project=&quot;sample&quot;/&gt; &lt;/runtimeClasspathEntry&gt; "/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt; &lt;runtimeClasspathEntry internalArchive=&quot;/sample/src/main/java&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt; "/>
</listAttribute>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.gwt.dev.GWTShell"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-out src/main/webapp com.company.module1.gwt.Module1/Module1.html -noserver -port 8080"/> <!-- -port 6434"/> -->
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="sample"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xmx1024M"/>
</launchConfiguration>

in the next post I’ll describe how to setup GWT Maven2 dependencies correctly. I would certainly like Google to provide a maven central to grab all the GWT stuff from (as well as fixed GWT itself to better support native libraries under maven), however at the moment that needs to be done manually.

Related posts:

  1. GWT, Maven2, Eclipse - part2 Just wanted to finish my previous post on the Maven2...
  2. Google App Engine now with Java Google announced availability of their Google App Engine for Java....

  1. Charlie says:

    Nice job there, flexible indeed, and quick.

    I would note though, that the most recent release of the GWT-Maven plugin is pretty straightforward too (and there is an archetype, and docs, etc). It handles things differently than previous versions did.

    Also, it does support noserver, and connecting a debugger, and so on. It probably isn’t as flexible as just using AntRun, true, but it might be more convenient in some cases now to try the plugin too.

  2. Mootool says:

    This is a very nice article, I try to follow up for my example. OK, and I want to sugguest new best artical about GWT and Eclipse, That is Extjs-GWT on Eclipse which reduce development time.
    I like this example and try to use extjs-gwt in eclipse project.
    http://extjs-gwt.blogspot.com
    See more about Extjs-GWT on Eclipse by using Eclipse’s plugin.
    I think this Extjs-GWT article can help beginner to learn faster.

  1. There are no trackbacks for this post yet.

Leave a Reply