GWT

This is a beta version. If you find errors, please report them or change them!

TABLE OF CONTENTS

The GWT includes a set of tools that can be run from the command-line to simplify and speed up common tasks.

  1. Configuring Your Path
  2. webAppCreator
  3. i18nCreator

Configuring your PATH

To easily invoke the command-line tools without entering their full path names, add them to your system command search path. To do this, in the PATH environment variable, identify the directory in which you unpacked the GWT distribution.

Windows

  1. Right-click on My Computer and select Properties
  2. Open the Advanced tab.
  3. Click the Environment Variables button.
  4. From the user variables list, select Path and click Edit
  5. At the end of the of the variable value, add a semicolon followed by the full path to the directory where you unpacked the GWT distribution (e.g., ;C:\gwt-2.0.0\).

Mac or Linux

Edit a file named .profile or .bash_profile in your home directory. For example, if you unpacked GWT in /home/user/gwt-2.0.0/, update your profile as follows:

PATH=$PATH:/home/user/gwt-2.0.0/
export PATH

You will need to log out of your account and log back in before the PATH setting takes effect.

webAppCreator

A command-line tool that generates a starter application and scripts for launching development mode and compiling to JavaScript. Use the files generated by these scripts as a starting point for building your own project.

webAppCreator [-overwrite] [-ignore] [-out dir] [-junit jar] [-XnoEclipse | -XonlyEclipse] moduleName
-out
The directory to write output files into (defaults to current)
-overwrite
Overwrite any existing files
-ignore
Ignore any existing files; do not overwrite
-junit
Specifies the path to your junit.jar
moduleName
The name of the module to create (fully-qualified Java class name)

Example

~/Foo> webAppCreator -junit /path/to/junit-3.8.1.jar -out foo com.example.foo.Foo
Created directory foo/src
Created directory foo/war
Created directory foo/war/WEB-INF
Created directory foo/war/WEB-INF/lib
Created directory foo/src/com/example/foo
Created directory foo/src/com/example/foo/client
Created directory foo/src/com/example/foo/server
Created directory foo/test/com/example/foo/client
Created file foo/src/com/example/foo/Foo.gwt.xml
Created file foo/war/Foo.html
Created file foo/war/Foo.css
Created file foo/war/WEB-INF/web.xml
Created file foo/src/com/example/foo/client/Foo.java
Created file foo/src/com/example/foo/client/GreetingService.java
Created file foo/src/com/example/foo/client/GreetingServiceAsync.java
Created file foo/src/com/example/foo/server/GreetingServiceImpl.java
Created file foo/build.xml
Created file foo/README.txt
Created file foo/test/com/example/foo/client/FooTest.java
Created file foo/.project
Created file foo/.classpath
Created file foo/Foo.launch
Created file foo/FooTest-dev.launch
Created file foo/FooTest-prod.launch
Created file foo/war/WEB-INF/lib/gwt-servlet.jar

The generated files are used as follows:

  • Running ant devmode from the command-line brings up the new application in development mode.
  • Running ant build from the command-line translates the Java app to JavaScript, creating a web folder called foo in the war directory.
  • Foo.launch is a launch configuration for Eclipse.
  • FooTest-dev.launch is a launch configuration for Eclipse that will run the project's tests in development mode.
  • FooTest-prod.launch is a launch configuration for Eclipse that will run the project's tests in production mode.
  • The other files implement a small sample GWT application.

You will notice that the build.xml file contains a number of rules to compile and deploy your application. These will help resolve the libraries needed at compile time / run time to compile and deploy your application, respectively. The generated build.xml file for the Foo project above will look like the following:

<?xml version="1.0" encoding="utf-8" ?>
<project name="Foo" default="build" basedir=".">
  <!-- Configure path to GWT SDK -->
  <property name="gwt.sdk" location="C:\gwt-2.0.0" />

  <path id="project.class.path">
    <pathelement location="war/WEB-INF/classes"/>
    <pathelement location="${gwt.sdk}/gwt-user.jar"/>
    <fileset dir="${gwt.sdk}" includes="gwt-dev*.jar"/>
    <!-- Add any additional non-server libs (such as JUnit) -->
    <fileset dir="war/WEB-INF/lib" includes="**/*.jar"/>
  </path>

  <target name="libs" description="Copy libs to WEB-INF/lib">
    <mkdir dir="war/WEB-INF/lib" />
    <copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet.jar" />
    <!-- Add any additional server libs that need to be copied -->
  </target>

  <target name="javac" depends="libs" description="Compile java source">
    <mkdir dir="war/WEB-INF/classes"/>
    <javac srcdir="src" includes="**" encoding="utf-8"
        destdir="war/WEB-INF/classes"
        source="1.5" target="1.5" nowarn="true"
        debug="true" debuglevel="lines,vars,source">
      <classpath refid="project.class.path"/>
    </javac>
    <copy todir="war/WEB-INF/classes">
      <fileset dir="src" excludes="**/*.java"/>
    </copy>
  </target>

  <target name="gwtc" depends="javac" description="GWT compile to JavaScript">
    <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
      <classpath>
        <pathelement location="src"/>
        <path refid="project.class.path"/>
      </classpath>
      <!-- add jvmarg -Xss16M or similar if you see a StackOverflowError -->
      <jvmarg value="-Xmx256M"/>
      <!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->
      <arg value="com.example.foo.Foo"/>
    </java>
  </target>

  <target name="devmode" depends="javac" description="Run development mode">
    <java failonerror="true" fork="true" classname="com.google.gwt.dev.DevMode">
      <classpath>
        <pathelement location="src"/>
        <path refid="project.class.path"/>
      </classpath>
      <jvmarg value="-Xmx256M"/>
      <arg value="-startupUrl"/>
      <arg value="Foo.html"/>
      <!-- Additional arguments like -style PRETTY or -logLevel DEBUG -->
      <arg value="com.example.foo.Foo"/>
    </java>
  </target>

  <target name="javac.tests" depends="javac" description="Compiles test code">
    <javac srcdir="test" includes="**" encoding="utf-8"
      source="1.5" target="1.5" nowarn="true"
      debug="true" debuglevel="lines,vars,source">
      <classpath location="C:\junit\junit-3.8.1.jar"/>
      <classpath refid="project.class.path"/>
    </javac>
  </target>

  <target name="test.dev" depends="javac.tests" description="Run development mode tests">
    <mkdir dir="reports/htmlunit.dev" />
    <junit fork="yes" printsummary="yes" haltonfailure="yes">
      <jvmarg line="-Xmx256m" />
      <sysproperty key="gwt.args" value="-logLevel WARN" />
      <sysproperty key="java.awt.headless" value="true" />
      <classpath>
        <pathelement location="src" />
        <pathelement location="test" />
        <path refid="project.class.path" />
        <pathelement location="C:\junit\junit-3.8.1.jar" />
      </classpath>
      <batchtest todir="reports/htmlunit.dev" >
        <fileset dir="test" >
          <include name="**/*Test.java" />
        </fileset>
      </batchtest>
      <formatter type="plain" />
      <formatter type="xml" />
    </junit>
  </target>

  <target name="test.prod" depends="javac.tests" description="Run production mode tests">
    <mkdir dir="reports/htmlunit.prod" />
    <junit fork="yes" printsummary="yes" haltonfailure="yes">
      <jvmarg line="-Xmx256m" />
      <sysproperty key="gwt.args" value="-prod -logLevel WARN -out www-test" />
      <sysproperty key="java.awt.headless" value="true" />
      <classpath>
        <pathelement location="src" />
        <pathelement location="test" />
        <path refid="project.class.path" />
        <pathelement location="C:\junit\junit-3.8.1.jar" />
      </classpath>
      <batchtest todir="reports/htmlunit.prod" >
        <fileset dir="test" >
          <include name="**/*Test.java" />
        </fileset>
      </batchtest>
      <formatter type="plain" />
      <formatter type="xml" />
    </junit>
  </target>

  <target name="test" description="Run development and production mode tests">
    <antcall target="test.dev" />
    <antcall target="test.prod" />
  </target>

  <target name="hosted" depends="devmode" description="Run development mode (NOTE: the 'hosted' target is deprecated)" />

  <target name="build" depends="gwtc" description="Build this project" />

  <target name="war" depends="build" description="Create a war file">
    <zip destfile="Foo.war" basedir="war"/>
  </target>

  <target name="clean" description="Cleans this project">
    <delete dir="war/WEB-INF/classes" failonerror="false" />
    <delete dir="war/foo" failonerror="false" />
  </target>

</project>

Notice that there are some properties here that you might like to extract to a build.properties file, such as the gwt.sdk property, to make it easier to share the same build script with teammates who may have different configurations on their development machines. Also, this build.xml file serves as an excellent base to grow on as your project takes on more dependencies or requires more specific build targets (for example, unit testing targets).

i18nCreator

A command-line tool that generates scripts to help with static string internationalization, along with sample properties files. Modify the .properties files to suit your project. Run the generated <classname>-i18n script to (re)generate a Java interface for accessing the tags defined in your properties files.

i18nCreator [-eclipse projectName] [-out dir] [-overwrite] [-ignore] 
            [-createMessages] [-createConstantsWithLookup] interfaceName
-eclipse
Creates a debug launch config for the named Eclipse project
-out
The directory to write output files into (defaults to current)
-overwrite
Overwrite any existing files
-ignore
Ignore any existing files; do not overwrite
-createMessages
Generate scripts for a Messages interface rather than Constants
-createConstantsWithLookup
Generate scripts for a ConstantsWithLookup interface rather than Constants
interfaceName
The fully-qualified name of the interface to create

Example

 ~/Foo> i18nCreator -eclipse Foo -createMessages com.example.foo.client.FooMessages
 Created file src/com/example/foo/client/FooMessages.properties
 Created file FooMessages-i18n.launch
 Created file FooMessages-i18n

Running FooMessages-i18n will generate an interface class in a file named FooMessages.java from FooMessages.properties that extends Messages (The messages will take parameters, substituting {n} with the nth parameter).

 ~/Foo> i18nCreator -eclipse Foo com.example.foo.client.FooConstants
 Created file src/com/example/foo/client/FooConstants.properties
 Created file FooConstants-i18n.launch
 Created file FooConstants-i18n

Running FooConstants-i18n will generate an interface from FooConstants.properties that extends Constants (The constants will not take parameters). To create a ConstantsWithLookup class, pass the -createConstantsWithLookup option.

In both examples, The launch configurations do the same thing as the scripts, but are intended to be run from within the Eclipse IDE.

Tip: When new entries are added to the properties file, the -i18n scripts must be run again.

See Also