Oct
29
2009
20

Tutorial: GWT, Maven and Eclipse with M2Eclipse

This tutorial shows how to develop Rich Internet applications with the Google Web Toolkit (GWT) using a state of the art Software  Development Environment, especially Maven integrated in Eclipse through m2eclipse (There is another plugin IAM, but in this tutorial we will use M2Eclipse). We will set up the environment, create a web application that is using Google Maps and debug it. This tutorial is the Maven version of this one using Cypal Studio.

Preliminaries

First we have to download Java and Eclipse. We will not download the Google Web Toolkit, Cypal Studio or the Google Maps library for GWT like in my last tutorial.

  1. Download and install the Java SE Development Kit
    You need the JDK to run Eclipse, the GWT Hosted Mode-Browser and the GWT-Compiler. On MacOS the JDK is installed by default, for other operating systems you can download the JDK from Sun.
  2. Download and unzip the Eclipse IDE for Java EE Developers
    Download Eclipse for EE Developers. You can unzip Eclipse to whereever you like, for example into the folder applications or programs.
  3. Sign up for a Google Maps Api-Key (optional, only necessary for production use)
    This step is optional for development on your local computer (localhost). You can sign up for a key here.

Setup the eclipse workspace and configure plugins

Configure the eclipse workspace and project. This section contains only the configuration of eclipse, no source code that you would check in for example into svn.

  1. Eclipse Workspace
    Open Eclipse and choose a directory as workspace (project configuration and source code will/can be stored here).
    eclipse-galileo-select-workspace

    When Eclipse has started, you will see the welcome screen:
    eclipse-galileo-welcome
    Close the Welcome screen with the arrow on the right side and the workspace is displayed:
    eclipse-galileo-workspace
  2. Add the m2eclipse plugin (Integration of Maven into Eclipse)
    Open Help/Install New Software… and click Add on the upper right side:
    eclipse-galileo-install-new-software
    Enter M2Eclipse as name, http://m2eclipse.sonatype.org/update/ as Location and click OK:
    eclipse-galileo-new-software-add-site
    Select the items shown and click Next:
    eclipse-galileo-install-software-available-software
    Review the details and click Next:
    eclipse-galileo-install-software-review-details
    Accept the license and click Finish:
    eclipse-galileo-install-software-review-license
    Wait until the download has finished and the installation is complete and restart Eclipse (click yes).

Your first GWT application with Maven and Eclipse

Now we will create a simple GWT application based on an archetype.

  1. Create a Maven project
    Select File/New/Other… , select Maven project and click Next:
    eclipse-galileo-new-project-maven
    Click Next:
    eclipse-galileo-new-maven-project-name-and-location
    Select the gwt-maven-plugin archetype from org.codehaus.mojo and click Next:
    eclipse-galileo-new-maven-project-archetype
    Enter Maven details for this project and click Finish:
    eclipse-galileo-new-maven-project-archetype-parameters
    Your workspace will look like this:
    eclipse-galileo-workspace-with-project
  2. Edit the pom.xml
    This is the Project Object Model – maven project configuration file. Change the version of GWT to 1.7.o
    <gwt.version>1.7.0</gwt.version>

    Add the following dependency to the dependencies:

    <dependency>
     <groupId>com.google.gwt.google-apis</groupId>
     <artifactId>gwt-maps</artifactId>
     <scope>provided</scope>
     <version>1.0.4</version>
     </dependency>

    Add the following line direct below the gwt.version-tag within the properties:

    <runTarget>com.claudiushauptmann.gwt.maven.sample.Application/Application.html</runTarget>

    Change the plugin version to 1.1:

    <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>gwt-maven-plugin</artifactId>
     <version>1.1</version>
     <executions>
     <execution>
     <goals>
     <goal>compile</goal>
     <goal>generateAsync</goal>
     <goal>test</goal>
     </goals>
     </execution>
     </executions>
     </plugin>
  3. Edit the Application.gwt.xml
    This is the gwt module descriptor. We have to tell it, that we are using the Google Maps library (Yes, we have to do this twice. One time for Java and on time for GWT, because GWT should not use all Java libraries). Add the following lines:

    <script src="http://maps.google.com/maps?gwt=1&amp;file=api&amp;v=2" />
     <inherits name="com.google.gwt.maps.GoogleMaps">
  4. Edit the entrypoint (Application.java)
    This is the code that will be executed when the site is openend:

    package com.claudiushauptmann.gwt.maven.sample.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.maps.client.MapWidget;
    import com.google.gwt.maps.client.control.MapTypeControl;
    import com.google.gwt.maps.client.control.SmallMapControl;
    import com.google.gwt.maps.client.event.MapClickHandler;
    import com.google.gwt.maps.client.geom.LatLng;
    import com.google.gwt.maps.client.overlay.Marker;
    import com.google.gwt.maps.client.overlay.Overlay;
    import com.google.gwt.user.client.ui.RootPanel;
    
    /**
     * Entry point classes define <code>onModuleLoad()</code>.
     */
    public class Application implements EntryPoint {
    
     /**
     * This is the entry point method.
     */
     public void onModuleLoad() {
     MapWidget mapWiget = new MapWidget(LatLng.newInstance(48.136559,
     11.576318), 13);
     mapWiget.setSize("500px", "300px");
    
     mapWiget.addControl(new SmallMapControl());
     mapWiget.addControl(new MapTypeControl());
    
     mapWiget.addMapClickHandler(new MapClickHandler() {
     public void onClick(MapClickEvent e) {
     MapWidget sender = e.getSender();
     Overlay overlay = e.getOverlay();
     LatLng point = e.getLatLng();
    
     if (overlay != null && overlay instanceof Marker) {
     sender.removeOverlay(overlay);
     } else {
     sender.addOverlay(new Marker(point));
     }
     }
     });
    
     RootPanel.get().add(mapWiget);</pre>
    }
    <pre>}

Building the project

The build will be run automatically when debugging the project.

  1. Select your project in the Project Explorer.
  2. Click Run/Run As/Maven install.

Debugging the project

Of course, you can use a plugin like Cypal Studio or the Google plugin for Eclipse. This is a way that works stable on Mac, Linux and Windows.

  1. Select your project in the Project Explorer.
  2. Select Run/Run As/Maven build…
  3. Type gwt:debug as goal, give your launch configuration a name and click Run:
    eclipse-galileo-maven-gwt-launch-configuration
  4. Select your project in the Project Explorer.
  5. Select Run/Debug configurations.
  6. Select Remote Java Application in the list on the left side and click the new launch configuration symbol on the upper left side and click Debug:
    eclipse-galileo-gwt-launch-configuration
  7. The hosted mode browser will open and every time you click on the map a marker will appear:
    GWT-hosted-mode
    GWT-hosted-mode-browser

Thank you for reading my tutorial! Please leave a comment and/or make a link from your web site.

Nov
16
2008
89

Tutorial: Google Maps with Java, GWT and Eclipse

The intention of this tutorial is to show how to use eclipse to develop a rich internet application with GWT that contains a Google Maps-map. The focus is less on the API provided by the Google Maps-wrapper for GWT, but how to use eclipse as a comfortable IDE. Google itself provides a very comprehensive example (source code) that demonstrates the possibilities of the wrapper.

UPDATE (01/07/09): Since there is some discussion about how to put this tutorial into action, I decided to provide a zip file containing the finished eclipse workspace of this tutorial: tutorialgwtmaps. Don’t forget to update the build path and the GWT home folder!

UPDATE 2 (01/14/09): Code Formatted.

Preliminaries
Download the required software and sign for a Google Maps API-key.

  1. Download and install the Java SE Development Kit
    You need the JDK to run Eclipse, the GWT Hosted Mode-Browser and the GWT-Compiler. On MacOS the JDK is installed by default, for other operating systems you can download the JDK from Sun.
  2. Download and unzip the Eclipse IDE for Java EE Developers
    Download Eclipse for EE Developers. You can unzip Eclipse to whereever you like, for example into the folder applications or programs.
  3. Download the Google Web Toolkit
    GWT is available at code.google.com. Download and unzip it to whereever you like.
  4. Download und install Cypal Studio for GWT
    Cypal Studio is a Eclipse plugin that integrates GWT into Eclipse the build process and the launch process. Unzip the plugin jars into <Eclipse home>/dropins/Cypal/plugins. You have to manually create the Cypal and plugins directories (installation manual).
  5. Download and unzip the Google Maps-library for GWT
    The Google Maps-library for GWT wrappes the javascript necessary to run Google Maps on your website. Download and unzip the library to whereever you like.
  6. Sign up for a Google Maps Api-Key
    This step is optional for development on your local computer (localhost). You can sign up for a key here.

Setup the eclipse workspace
Configure the eclipse workspace and project. This section contains only the configuration of eclipse, no source code that you would check in for example into svn.

  1. Eclipse Workspace
    Open Eclipse and choose a directory as workspace (project configuration and source code will/can be stored here).

    When Eclipse has started, you will see the welcome screen:

    Close the Welcome screen with the arrow on the right side and the workspace is displayed:
  2. Configuration of Cypal Studio
    Open the Preferences Dialog (Eclipse/Preferences on MacOS or Window/Preferences) and select Cypal Studio on the left side. Then Enter the folder where the Google Web Tookit is located and click OK:
  3. Create the GWT-Project
    Select File/New/Dynamic Web Project. Enter MyFirst Google Maps Application as project name and select Cypal Studio for GWT as configuration, then click Next.

    You can leave the default values on the next page and click Finish.

    You will see a new project on the left side in the project explorer:
  4. Add the Google Maps-library to the Java Build Path
    Rightclick your new project and select Properties in the popup menu, then select Java Build Path on the left side and Libraries on the top. Click Add External JARs… and add the file gwt-maps.jar located in the Google Maps for GWT-library.

Your first Google Maps-application
This section handles the source code and configuration files. It adjusts the GWT-template to create a Google Maps-map.

  1. Create a GWT Module
    Select File/New/Other…, then Cypal Studio/GWT Module and click Next:

    Enter a package (e.g.: com.claudiushauptmann.gwt.maps.samples) and a name for the module (e.g.: MyFirstMapsModule) and click Finish.

    Your workspace will look like this with 3 new files. A Java class with the module, a wrapper-html-file and a xml-file that defines the GWT-Module:
  2. Configure your project for Google Maps
    Add <inherits name=”com.google.gwt.maps.GoogleMaps”/> and<script src=”http://maps.google.com/maps?gwt=1&amp;file=api&amp;v=2″ /> to your Module XML-file (e.g.: MyFirstMapsModule.gwt.xml). You can enter your API-Key here (optional for localhost).

    &lt;module&gt;
    
    	&lt;!-- Inherit the core Web Toolkit stuff.                  --&gt;
    	&lt;inherits name='com.google.gwt.user.User'/&gt;
    
    	&lt;inherits name=&quot;com.google.gwt.maps.GoogleMaps&quot;/&gt;
    	&lt;script src=&quot;http://maps.google.com/maps?gwt=1&amp;amp;amp;file=api&amp;amp;amp;v=2&quot; /&gt;
    
    	&lt;!-- Specify the app entry point class.                   --&gt;
    	&lt;entry-point class='com.claudiushauptmann.gwt.maps.samples.client.MyFirstMapsModule'/&gt;
    
      	&lt;inherits name=&quot;com.google.gwt.user.theme.standard.Standard&quot;/&gt;
      	&lt;!-- &lt;inherits name=&quot;com.google.gwt.user.theme.chrome.Chrome&quot;/&gt; --&gt;
      	&lt;!-- &lt;inherits name=&quot;com.google.gwt.user.theme.dark.Dark&quot;/&gt; --&gt;
    
    &lt;/module&gt;
    
  3. Add some code that inserts a MapWidget into the web page:
    package com.claudiushauptmann.gwt.maps.samples.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.maps.client.MapWidget;
    import com.google.gwt.maps.client.control.MapTypeControl;
    import com.google.gwt.maps.client.control.SmallMapControl;
    import com.google.gwt.maps.client.event.MapClickHandler;
    import com.google.gwt.maps.client.geom.LatLng;
    import com.google.gwt.maps.client.overlay.Marker;
    import com.google.gwt.maps.client.overlay.Overlay;
    import com.google.gwt.user.client.ui.RootPanel;
    
    public class MyFirstMapsModule implements EntryPoint {
    
    	public void onModuleLoad() {
    		MapWidget mapWiget = new MapWidget(LatLng.newInstance(48.136559, 11.576318), 13);
    		mapWiget.setSize(&quot;500px&quot;, &quot;300px&quot;);
    
    	    mapWiget.addControl(new SmallMapControl());
    	    mapWiget.addControl(new MapTypeControl());
    
    	    mapWiget.addMapClickHandler(new MapClickHandler() {
    	      public void onClick(MapClickEvent e) {
    	        MapWidget sender = e.getSender();
    	        Overlay overlay = e.getOverlay();
    	        LatLng point = e.getLatLng();
    
    	        if (overlay != null &amp;amp;&amp;amp; overlay instanceof Marker) {
    	          sender.removeOverlay(overlay);
    	        } else {
    	          sender.addOverlay(new Marker(point));
    	        }
    	      }
    	    });
    
    	    RootPanel.get().add(mapWiget);
    	}
    }
    

The launch configuration
Now we want to run and debug our application. First we debug it in the Hosted Mode-Browser, the we compile it to javascript and open it with a standard internet browser.

  1. Create a launch configuration and start debugging
    Select Run/Debug Configurations… and create a new launch configuration with the button on the top left. Enter a name for the launch configuration and selct your project and your module, then click Apply.
  2. Debugging
    Click Debug and the Hosted Mode-browser starts:
  3. Compile and run in your Browser
    Click Compile/Browse, wait some seconds and your standard browser will open and show your application:

It would give me a great pleasure, if you wrote a short comment, whether this tutorial was helpful!

Powered by WordPress. Theme: TheBuckmaker