Milen Dyankov

Software Engineer, Consultant, Developer Advocate

Run GWT application in "hosted mode" from maven

April 26, 2009 | 2 Minute Read

It seems to get more and more cloudy in the IT world these days . It's a matter of time before the rain (of applications) starts. When this happen one will need the proper tools, to be able to add his/hers own few drops.

So I though it's about time to start experimenting with Google Web Toolkit. What I like the most about GWT is it's "hosted mode". The fact that Java code changes reflect the GUI right away and one don't have to wait for generate, compile, build, deploy, ... steps to complete is really speeding up the development process.

Since 99% of my projects use Maven the first thing to look for (after reading GWT tutorials) was a GWT maven plug-in. No surprise here - there is one (http://mojo.codehaus.org/gwt-maven-plugin). The GWT docs and gwt-maven-plugin docs gives a lot of information how to create and build GWT applications.  Unfortunately the released version of  gwt-maven-plugin (1.0 at the time of writing) does not support hosted mode.

The solution is to use the snapshot repository
    <pluginRepository>
        <id>mojo-snapshots</id>
        <url>http://snapshots.repository.codehaus.org</url>
    </pluginRepository>
and the 1.1-SNAPSHOT version :
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>gwt-maven-plugin</artifactId>
                <version>1.1-SNAPSHOT</version>
            </plugin>
        </plugins>
    </pluginManagement>
If the application only contains client code it can be then run in "hosted mode" by simply typing mvn gwt:run. The thing docs don't mention (again, at the time of writing this) is that "mvn gwt:run" will not build the server side of the application. Unless you do this yourself GWT-RPC call will not work. So there are three possible ways:

  1. let the Eclipse (or whatever IDE you use) build the classes in ${basedir}/src/main/webapp/WEB-INF/classes

    I personally don't like this approach. It does not solve the problem if you have multiple modules in maven as the dependent libraries will be still missing. Also you have to add some rules to your SCM system to ignore generated classes.

     
  2. do mvn compile war:inplace gwt:run

    This will create extracted version of the war in "${basedir}/src/main/webapp". It will solve the problem even for multiple modules setup but you still will have to deal with the SCM ignore rules.
     
  3. do mvn compile war:exploded gwt:run

    This will create extracted version of the WAR in a specified directory (by default it is ${project.build.directory}/${project.build.finalName} as stated in Maven WAR Plugin docs). In order for this to work the gwt-maven-plugin needs to be told where the exploded war is by adding this to it's configuration in pom.xml :
        <hostedWebapp>
            ${project.build.directory}/${project.build.finalName}
        </hostedWebapp>
    This IMHO is the best approach. Solves the problem even in case of multiple modules and your SCM managed folders remain clean.