Nicozan-Blog

Knowledge Seeking

GWT – Adding Jar Dependency to our GWT project. (Maven-Gwt-Plugin) March 17, 2011

Filed under: GWT,Java — nicozan @ 4:16 pm

Introduction

You might be asking yourself, Why am I doing this post?, because you might think that adding a dependency in a GWT Project using the Maven-GWT-Plugin is just as simple as adding a dependency in a Regular Maven project. Sorry to disappoint but apart from that you have to do something else … 🙂

Concepts

Every gwt project has a gwt.xml file that represents the GWT-module. This file contains specific information related to the module, for example:
—Entry-point (The java class that represents the web page that is going to be displayed in the web browser)
—Inherits (External Modules that our GWT-Module uses)

These two elements shown above are represented by tags. These tags are child elements of the root element module . The next code snippet shows how the gwt.xml file is structured.
MyWebApp.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='MyWebApp'>
 <!-- Inherit the core Web Toolkit stuff.                        -->
 <inherits name='com.google.gwt.user.User'/>
 <!-- Inherit the default GWT style sheet.  You can change       -->
 <!-- the theme of your GWT application by uncommenting          -->
 <!-- any one of the following lines.                            -->
 <inherits name='com.google.gwt.user.theme.standard.Standard'/>
 <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
 <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->

 <!-- Other module inherits                                      -->
 <inherits name='com.wordpress.nicozan.Model'/>

 <!-- Specify the app entry point class.                         -->
 <entry-point class='com.wordpress.nicozan.client.MyWebApp'/>

 <!-- Specify the paths for translatable code                    -->
 <source path='client'/>
 <source path='shared'/>
</module>

As you can see the name of the gwt.xml file is the same, and must be, as the Entry-point Java Class name. There are some modules that are standards and need to be inherited, (i.e : com.google.gwt.user.User)

Adding Jar Dependencies

Let’s consider the MyWebApp.gwt.xml shown before and that we have a project with two sub modules : webapp and model. The project has the next directory structure:

parent-project/                                    
|- pom.xml
|
|- model/                                      (External JAR module:)
|  |- pom.xml
|  \- src/main/java/
|     \- com/wordpress/nicozan/
|        |- Model.gwt.xml
|        \- model/utils/Element1.java
|
\- webapp/                                      (GWT-Module)
 |- pom.xml
 \- src/
    |- main/java/
    |  \ -com/wordpress/nicozan/
    |     |- MyWebApp.gwt.xml
    |     \- client/MyWebApp.java
    \- main/webapp/
       \- WEB-INF/web.xml

Take special attention to the model module. There is a gwt.xml file (Model.gwt.xml) that it’s in charge of specifying the source path from where the resources we’ll be built so the webapp module will then be able to use them. The Model.gwt.xml will look like this:

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

The Java classes are found recursively so you just need to specify the root directory.
The next thing to do is to generate this resources so the GWT webapp can use them. The pom.xml of the model module would look like this:

<project xmlns="http://maven.apache.org/POM/4.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
  <groupId>com.wordpress.nicozan</groupId>
  <artifactId>parent-project</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>model</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<build>    
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.java</include>
        <include>**/*.gwt.xml</include>
      </includes>
    </resource>
  </resources>
  <plugins>
        <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>gwt-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>resources</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
</project>

Now, when you do mvn clean install of the parent-project, the resources will be generated and the GWT webapp can consume them. 🙂

TIPS/NOTES:
– Don’t forget to add all the dependencies as you usually do in your common maven projects.
– Remember to add the maven-gwt-plugin to the pom.xml of the resource you want generate.
– You don’t need to do all the steps explained above if you are adding dependencies that are going to use on the server-side. In that case just add the dependency to the pom as usual.

Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s