Writing a GWT library

A GWT library can be used to package classes for mutualization and/or modularization. A GWT library is just a java archive (JAR) containing both classes and java sources for later GWT compilation and a gwt.xml module descriptor.

Packaging

The only distinction with a standard JAR project is the mix of sources and classes in the output folder. A simple way to achieve this is to add a dedicated resource in the POM :

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

Another option is to let the plugin detect the required source to be included, based on the module descriptor. The benefict is that the plugin will not include java files that are not declared as GWT source by the module descriptor, avoiding end-user to reference your internal classes (usually for library that include both client and server side components).

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>gwt-maven-plugin</artifactId>
        <version>2.4.0</version>
        <executions>
          <execution>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

Using general purpose JARs as GWT library

Many users want to use common usage libraries as GWT modules. Some would like to reuse the server-side business classes on client side, using a shared Maven module. The requirement to include sources in the JAR can then be annoying : including sur JAR in the webapp means the source code will be distributed with the application.

gwt-maven-plugin provides a convenient *hack* to work around this restriction using Maven convention for source jars. The ''compileSourcesArtifacts'' parameter can be used to select a subset of project dependencies (using "groupId:artifactId" syntax). The plugin will download (or get from your local repository) the artifact sources and add them to the compiler Classpath.

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>gwt-maven-plugin</artifactId>
        <version>2.4.0</version>
        <compileSourcesArtifacts>
          <compileSourcesArtifact>com.myCompany:domain</compileSourcesArtifact>
        </compileSourcesArtifacts>
      </plugin>

The only missing fragment to allow using this library from your GWT code is to create a ''gwt.xml'' module file. Create file ''com/mycompany/Domain.gwt.xml'' to match package ''com.mycompany.domain'' to be included as GWT code resource :

<module>
  <inherits name="com.google.gwt.user.User" />
  <source path="domain" />
</module>

You can include this file in the ''domain'' JAR (as it will reasonably "pollute" the artifact)

  |_ domain
  |  |_ src/main/java/com/mycompany/domain/SomeUseFullClass.java
  |  |_ src/main/resources/com/mycompany/Domain.gwt.xml
  |_ webapp
     |_ src/main/resources/com/mycompany/web/MyApp.gwt.xml

... or create one for your webapp by creating a phantom package to only contain this file.

  |_ domain
  |  |_ src/main/java/com/mycompany/domain/SomeUseFullClass.java
  |_ webapp
  |  |_ src/main/resources
        |_ com/mycompany/Domain.gwt.xml
        |_ com/mycompany/web/MyApp.gwt.xml