Monthly Archive for March, 2011

Speed Up Maven GWT Compile Time For Developers

Google Web Toolkit is great for when you need to do AJAX on a Java centric web application. When you want to ensure type safety from the AJAX web layer all the way through to your persistence layer, there is nothing better than GWT.

Unfortunately because GWT code has to support 5 different rendering engines in the current major browsers, compilation time can be rather slow.  Our current project has six different GWT modules and the GWT compile step was starting to impact the code, compile, test, fix, rinse repeat cycle of our developers.

Add on top of this each locale supported by the app and the compilation time can start to get really long as GWT has to compile each module once for each locale and rendering engine combination.

Ideally you would use GWT Hosted mode for your development cycle, but sometimes you just need to run it as compiled JavaScript, thus requiring a lengthy recompile of your GWT modules.

The GWT docs actually have a section on how to speed up compile time by having the developers run the compile for one browser only.  However this involves creating an extra GWT module for each one of your existing modules.  We have five and creating five more modules to implement this speed up was rather unappealing.  There had to be a better way.

We are using Maven to manage our build process.  Surely there has to be a way to utilize Maven's very flexible configuration and plugin architecture to let us specify a profile that builds the GWT modules for only one browser rendering engine.

Here is the solution I came up with.

The key to compiling GWT for just one browser is in the user.agent property as shown in the GWT docs mentioned above.

The GWT docs show setting this property in the GWT module file for the browser specific HelloFirefox child module that inherits from the Hello module.

What we want is to have a parent module set the user.agent property and have all of our modules inherit from this module.  This property then needs to be set by Maven depending on which build profile we specify.

To this end, I created a MavenFilteredUserAgent module.  But because I need the property to be set by a Maven profile I had to work some Maven magic.

The trick here is to get Maven to filter the module file and then get GWT to use the filtered version during compile.

Here is the Maven magic that makes all this possible:

pom.xml

MavenFilteredUserAgent.gwt.xml

MyCompileOptimizedGWTModule.gwt.xml

Have all of your GWT modules inherit the MavenFilteredUserAgent as in the MyCompileOptimizedGWTModule example above.

Once in place you can then run

mvn install -Pgwt-firefox

and all of your GWT modules will compile for Firefox only.

You can now test your GWT app in Firefox having spent considerably less time waiting for it to compile.

Be very careful when using this speed up technique that you don't run your GWT modules in a browser different from the one you compiled for as you will get very confusing and meaningless JavaScript errors.  And definitely make sure that you never deploy an app compiled for just one browser to production.  GWT has been improved to make this mismatch more obvious as mentioned in issue 5861, but this improvement won't be available till the next release of GWT.

See Also:

Comment