GWT Module using Maven

Note, this discussion is based on:

  • GWT libraries 1.6.4
  • Apache Maven 2.2.1

I like Maven because it makes me much more productive. It takes care of a lot a project management details and makes the code saved in my repository smaller. When it comes to creating GWT modules (reusable libraries), I was hoping that I could use Maven to keep track of dependencies between applications and modules, as well as dependencies between modules. This discussion presents what is needed to make a GWT module in a Maven project.

There are two important points to remember when making a GWT module using Maven:

  1. The produced JAR file must contain a module file (*.gwt.xml)
  2. The produced JAR file must contain the source files needed in the module. This is because these source files must be compiled in the context of the application.

Maven Project

The first step is to set up a normal Maven project to produce a JAR file artifact.

This command will generate two java files called App.java and AppTest.java. You can delete them.

In an example:

Modules must be compiled using Java 6, therefore the pom.xml file must be modified accordingly. Edit the pom.xml file

and add the following content at the appropriate place:

Module File

A module file is required. In Maven, this file should be in the resource directory. Create a module file in the usual resources directory with an appropriate name and an extension of .gwt.xml

Use the following content as a starting point for the module file:

There is nothing else required in the module file. Note that the name of the module is “Module0”, in the global name space. If one wants to use a longer name space such as “com.example.Module0”, then the full modulfe file name would be: “src/main/resources/com/example/Module0.gwt.xml”.

Since the Java source files for the module are to be located in a subdirectory of the module other than the standard “client” subdirectory (in this case, the name of the directory is “gwt”), one must add a “source” directive to the module file, as such:

Include Source Files in JAR artifact

As stated above, source files must be added to the JAR artifact. Therefore, the pom.xml file must be augmented to include the source files as well as the standard resources directory. Edit pom.xml and add the following:

Test JAR generation

For example, create a Bean class to test:

with the following content:

Now, generate artifact:

You can verify the content of your JAR artifact:

Note that the module file, the source and compiled version of the class are included.

Include Module in an application

To include the new module in an application or another module, two steps are required:

  1. Add a reference to the module file in the upstream application/module.
  2. Add the module JAR as a dependency of the upstream application/module.

Edit the GWT module file (.gwt.xml) in the upstream application/module and add the following directive:

Do not forget to put the fully qualified name, including the name space, if required.

If the upstream application/module uses Maven, then adding the module as a dependency is simple. Edit the pom.xml file in the upstream project and add the JAR artifact produced by the module:

Create a module with GWT Widgets

In the module, if one wishes to create GWT widgets, then one must include the core Web toolkit module from GWT. Two steps are required:

  1. Add dependency in the module file
  2. Add dependency in the Maven pom.xml file.

Edit the module file

and add the following directive:

Then, edit the pom.xml file

and add the dependency

One Reply to “GWT Module using Maven”

Leave a Reply

Your email address will not be published. Required fields are marked *