2009
10.31

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.

> mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=<group-id> -DartifactId=<artifact-id>

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

In an example:

> mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=gwt -DartifactId=gwt-module0
> cd gwt-module0
> rm src/main/java/gwt/App.java
> rm src/test/java/gwt/AppTest.java
> find .
.
./src
./src/main
./src/main/java
./src/main/java/gwt
./src/test
./src/test/java
./src/test/java/gwt
./pom.xml

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

> vi pom.xml

and add the following content at the appropriate place:

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <configuration>
            <source>1.6</source>
            <target>1.6</target>
         </configuration>
      </plugin>
   </plugins>
</build>

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

> mkdir -p src/main/resources
> vi src/main/resources/Module0.gwt.xml

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

<!DOCTYPE module PUBLIC "//gwt-module/" "http://google-web-toolkit.googlecode.com/svn/tags/1.6.2/distro-source/core/src/gwt-module.dtd">
<module>
</module>

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:

<!DOCTYPE module PUBLIC "//gwt-module/" "http://google-web-toolkit.googlecode.com/svn/tags/1.6.2/distro-source/core/src/gwt-module.dtd">
<module>
   <source path="command"/>
</module>

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:

<build>
   <resources>
      <resource>
         <directory>src/main/java</directory>
         <includes>
            <include>gwt/**</include>
         </includes>
      </resource>
      <resource>
         <directory>src/main/resources</directory>
      </resource>
   </resources>
</build>

Test JAR generation

For example, create a Bean class to test:

> vi src/main/java/gwt/MyBean.java

with the following content:

package gwt;

import java.io.Serializable;

@SuppressWarnings("serial")
public class MyBean implements Serializable {

   private String id;

   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
}

Now, generate artifact:

> mvn install

You can verify the content of your JAR artifact:

> jar -tf target/gwt-module0-1.0-SNAPSHOT.jar
META-INF/
META-INF/MANIFEST.MF
gwt/
gwt/MyBean.java
gwt/MyBean.class
Module0.gwt.xml
META-INF/maven/
META-INF/maven/gwt/
META-INF/maven/gwt/gwt-module0/
META-INF/maven/gwt/gwt-module0/pom.xml
META-INF/maven/gwt/gwt-module0/pom.properties

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:

<inherits name='Module0'/>

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:

</dependencies>
   <dependency>
      <groupId>gwt</groupId>
      <artifactId>gwt-module0</artifactId>
      <version>1.0-SNAPSHOT</version>
   </dependency>
</dependencies>

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

> vi src/main/resources/Module0.gwt.xml

and add the following directive:

<inherits name='com.google.gwt.user.User'/>

Then, edit the pom.xml file

> vi pom.xml

and add the dependency

<dependencies>
   <dependency>
      <groupId>com.google.gwt</groupId>
      <artifactId>gwt-user</artifactId>
      <version>1.6.4</version>
      <scope>provided</scope>
   </dependency>
</dependencies>

1 comment so far

Add Your Comment
  1. [...] Step by step of creating a GWT project with Maven [...]