Maven Recipe : GWT development profile
It’s no secret that I am a fan of GWT for web development. I believe it to be a superior solution for large-scale Javascript development. However, it does come with some downsides. Verbosity is one of them, but we have a solution for that - see how to cut down GWT’s verbosity. Another one is the long compilation time. Since GWT generates permutations for multiple browser targets - 6 in version 2.1 - you will wait more than needed if you just need to compile for one browser.
It often is the case that a single-permutation application is deployed for smoke testing, and then a application with all permutations is deployed to Q&A. This is usually achieved by creating a development module which reduces the number of permutations to one. I will show you how to integrate this in a natural way into your Maven build.
I will start with the simplest possible Maven/GWT project, as shown below:
I will not insist over the contents of the project, as it is not important. The project only has one client-side class, and is based on the archetype from the excellent gwt-maven plugin from Codehaus.
In the pom.xml, the module to compile is configured to be the production one:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.1.0-1</version>
<configuration>
<!-- contents omitted -->
<module>ro.lmn.maven.recipe.gwtdev.ProfileDemo</module>
</configuration>
</plugin>
A compiler run currently generates six permutations and takes 23 seconds on my machine. I am lucky to have four cores at my disposal, but usually build server CPU time is scarce, so the effect is amplified.
robert@neghvar:~/git-repos/gwtdev-profile (master)> mvn clean package
...
[INFO] --- gwt-maven-plugin:2.1.0-1:compile (default) @ gwtdev-profile ---
[INFO] Compiling module ro.lmn.maven.recipe.gwtdev.ProfileDemo
[INFO] Compiling 6 permutations
[INFO] Compiling permutation 0...
[INFO] Process output
[INFO] Compiling
[INFO] Compiling permutation 1...
[INFO] Compiling permutation 2...
[INFO] Compiling permutation 3...
[INFO] Compiling permutation 4...
[INFO] Compiling
[INFO] Compiling permutation 5...
[INFO] Compile of permutations succeeded
[INFO] Linking into /home/robert/git-repos/gwtdev-profile/target/gwtdev-profile-1.0-SNAPSHOT/ProfileDemo
[INFO] Link succeeded
[INFO] Compilation succeeded -- 22.702s
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29.101s
[INFO] Finished at: Sat Mar 05 23:59:24 EET 2011
[INFO] Final Memory: 12M/142M
[INFO] ------------------------------------------------------------------------
To cut down on compilation time I will use a Dev module which compiles just for recent versions of FireFox:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to="ProfileDemo">
<inherits name="ro.lmn.maven.recipe.gwtdev.ProfileDemo" />
<set-property name="user.agent" value="gecko1\_8" />
</module>
To select it in my pom.xml I will create a dev profile, which overwrites some of the configuration entries of the gwt-maven-plugin:
<profiles>
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<configuration>
<module>ro.lmn.maven.recipe.gwtdev.ProfileDemoDev</module>
<draftCompile>true</draftCompile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
For added speed, I’ve enabled draft compilation, which means that the GWT compiler will spend less effort on trying to optimise the resulting javascript.
Rerunning the build with the -Pdev argument results in just one permutation being generated:
robert@neghvar:~/git-repos/gwtdev-profile (master)> mvn -Pdev clean package
...
[INFO] --- gwt-maven-plugin:2.1.0-1:compile (default) @ gwtdev-profile ---
[INFO] Compiling module ro.lmn.maven.recipe.gwtdev.ProfileDemoDev
[INFO] Compiling 1 permutation
[INFO] Compiling permutation 0...
[INFO] Compile of permutations succeeded
[INFO] Linking into /home/robert/git-repos/gwtdev-profile/target/gwtdev-profile-1.0-SNAPSHOT/ProfileDemo
[INFO] Link succeeded
[INFO] Compilation succeeded -- 14.091s
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.939s
[INFO] Finished at: Sun Mar 06 00:08:55 EET 2011
[INFO] Final Memory: 11M/104M
[INFO] ------------------------------------------------------------------------
The speed saving is of 35% and will likely increase as the GWT code increases in size and complexity.
We have reached our goal of creating a GWT development profile. By using a Maven profile we can make minimal modifications to our configuration and even apply different compiler options.
The complete source code for this article is available at https://github.com/rombert/Maven-Recipe—GWT-development-profile. If you have any suggestions or corrections, please comment. Or better yet, fork me.