2010-01-18

New GWT Projects in Eclipse

I had a bit of trouble getting a GWT project setup in Eclipse so that I could use a generated Ant build.xml for creating war packages but also utilize the fancy GWT Eclipse plugin features. The New Web Application Project wizard sets up the GWT project and creates some handy starter app code, but there's no option within the Google context menu to simply create a war. You can deploy directly to Google App Engine which is pretty cool, but I prefer to be able to deploy and run my webapps from my own server so that I know they can be deployed on any Tomcat installation.
GWT comes with a utility called webAppCreator which does most of this, but also provides a build.xml and JDT launch configuration file:
webAppCreator -out MyWebApp com.mycompany.mywebapp.MyWebApp
In addition to creating the same directory structure and starter app code as the wizard, webAppCreator generates a README.txt file that, among other things, explains how to setup the project in Eclipse:
File -> Import... -> Existing Projects into Workspace

Browse to the directory containing this [README], select "MyWebApp".
  
Be sure to uncheck "Copy projects into workspace" if it is checked.
 
Click Finish.
You'll have Eclipse .project and .classpath files, but the project isn't configured for the GWT plugin. To do this, go to the Properties dialog for the project, Google > Web Toolkit. Turn on Use Google Web Toolkit, make sure it knows the location of your GWT installation, and hit OK.
  • To run the project using the Development Mode view within Eclipse, select Run As > Run Configurations..., and you should have Web Applications > MyWebApp. Select and run it; to stop it click the red square in the Development Mode toolbar.
  • To run the project using the standalone Development Mode java app, select Run As > Run Configurations..., and you should have Java Applications > MyWebApp. Select and run it; the standalone app seems to be a bit more versatile than the Eclipse view, but adds some overhead.
Either of these modes can be started through the Debug As menu as well.
Finally, to package the project into a war fit to be deployed, go to Run > External Tools > External Tools Configurations, and create a new Ant build. Pick the build.xml for your project, your project as the base directory, then move to the Targets tab, and select the war target, deselecting the build target since war depends on build already. It's a good idea to name this Configuration "MyWebApp war" or similar. When the target is run, your code will be compiled into the war subdirectory, which will then be zipped into MyWebApp.war in the project directory.

1 comment:

  1. Thanks a lot for this post. It saves a lot of time trying to import a sample module and run in Eclipse.
    Ryan Nieh
    ryan.nieh@gmail.com

    ReplyDelete