GWT and Maven: create a project and run it in hosted mode

Title is explicit: how to create a GWT project with Maven? Nothing fancy here, just taking the sample project the Eclipse GWT plugin generates and turning it into a Maven project.

All sources presented in this post can be obtained from my Github.

As for the steps to follow, 

Reorganizing files

Nothing fancy here. We just need to make some adjustments to meet the traditional organization of Maven projects.

  • src/main/java: All your Java files and packages;
  • src/main/resources: Your .gwt.xml file (it is not Java, is it?) in the proper directory. If you have localization, you also might want to put your .properties files here.
  • src/main/webapp: the tricky part. Here is the folder where you should include the index page, default CSS and (most important if you use GWT-RPC services) the web.xml file which defines the mappings to servlets.
  • src/test: nothing in here for this project. Told you: nothing fancy for this project.

I recommend creating two projects:

  • GWT plugin’s sample project;
  • A Maven empty project which you will adapt.

You just need to copy your files from the first to the second one in the proper place. Having a Maven project enables Eclipse to watch the required directories and interpret them as source folders.

Setting a basic POM

Nothing unexpected: the GWT plugin replaces the GWT SDK, the war plugin packages the application. You also have to include GWT dependencies according to your needs. In the case of this sample, that leads to the following.

Adjustments: automatic generation of Async interfaces and message bundles

Async interfaces

Have you noticed in my POM?

That is right, the plugin generates the Async file for services. No more the hassle of creating the service interface, implementation and then the ServiceAsync interface. The latter is automatically generated.

One important adjustment to make: you were used to instantiate a service using:

URL mapping is no longer correct using this method. To instantiate a service, you should now do the following:

Still, not having to write the service interface twice is a time saver.

What about i18n?

Writing both a .properties file and the associated interface? Yes, that is a hassle too. But here again, the plugin can help, provided some configuration.

Of course, you will have to adapt the configuration so that the i18nMessagesBundle refer to the location of your file in the src/main/resources directory.

Taking GWT generated sources into account

For both cases above, the GWT plugin generates the sources in a target subdirectory. If you are using Eclipse, your IDE will start flashing ‘Missing class’ error messages everywhere in the project for any of the generated class.

To prevent that, you just have to go in your project properties: right-click the project root → Properties → Java Build Path. Once there, display the Source tab and click Add Folder. In there, just check target/generated-sources/gwt.

It should appear as the last line on the following screen.

Source folders for project

Running your GWT project in hosted mode

A little tricky here. You actually have to play a little with the Maven GWT plugin so that this could work, but once you are done, you will not need a GWT SDK any longer: the plugin will take care of everything, including running a Jetty server.

It took me some time to find the solution, but I finally agreed with Milen Dyankov on the cleanest solution.

I will not get into details as about whys, just give the how. If you want to read deeper into this, just have a look at Milen’s post on the subject. If however you are just greedy for the solution, here is what I learned from his post.

Basically, you have to generate the WAR in its exploded form and tell the Maven GWT plugin where to find it. So, the POM section should look like this:

Now all you got to do is to run the project through the proper command. I suggest:

(thanks to kramp for his help on the hosted mode part)

OK, you should be good to go! Remember: you can get a sample project from my Github

Published by

Cyrille Chopelet

Programming addict, UX philosopher, casual gamer, sci-fi enthusiast, hi-tech dilettante, ... Some people even call me a geek.

"Wit beyond measure is man's greatest treasure." − Rowena Ravenclaw

This site uses Akismet to reduce spam. Learn how your comment data is processed.