Thursday, May 03, 2012

GWT: Getting Maven Generated GWT Project working within Eclipse

Recent events have thrust me back into the world of GWT Development. So much has changed. RequestFactories, EventBuses, all sorts of improvements. I haven’t had to dig this deep in a while. But these are great improvements, and they take GWT that extra mile.

The source of my recent aggravations hasn’t been with GWT. It has been with Maven. Maven is a wonderful build tool. There is something really nice about being able to say “Hey, Maven, I want to build this project, and it has these dependencies”, and having Maven go out and fetch everything, set up a class path, and build. Awesome. Or at least in theory. One of my biggest headaches has been building Eclipse Plugins, but that will wait until another blog post.

This one is about GWT, Eclipse, and Maven. Now my Eclipse setup is pretty straight forward. I use the BIRT All-In-One 3.7.2 distribution, with the m2eclipse plugin installed separately (because the BIRT distribution has an incomplete m2eclipse plugin installed), the GWT Designer and Window Builder Plugins, and Googles Eclipse GWT Plugin (well, technically GWT Designer and the GWT Eclipse Plugin are BOTH Googles now….).
“So why so glum, chum?” I ask myself. Well, because things don’t quite work as advertised out of the box. GWT Designer and the GWT Eclipse Plugin provide AWESOME support for creating new projects and making things just work. Maven, no so much. What doesn’t work? Well, besides the thousands of red ‘X’ everywhere in Eclipse, GWT Designer doesn’t work, and the Google Eclipse Plugin run doesn’t quite work. At least with GWT 2.4, and the 3.7.2 Indigo setup of Eclipse. So, let me outline how I fixed these annoying issues with a step by step on creating a new GWT project with Maven and making the necessary fixes in Eclipse to make things work. I won’t cover getting RequestFactory working, I’ll leave that for the next post.
1.       Create a new Maven. Start by going to New/Other.

2.       Select Maven/Maven Project.

3.       Leave everything alone on the next page of the wizard.

4.       On the next screen, for the archetype, enter “gwt-maven-plugin”. (Note: m2eclipse does something really stupid in my environment (or maybe all environments in general, I havent investigated it further) where it will refuse to create more than one project of the same archtype in my workspace. In order to resolve this, I need to go and completely delete my .m2 respository, located at /.m2, and let it re-index). If you haven’t indexed your Maven repository before, it might take a while, so be patient and let it pop up in the grid.

5.       Fill out the appropriate info for the Maven project, such as Group and Artifact ID.

6.       Click Finish.
OK, cool, now we have a GWT project in Eclipse. In fact, you can run it if you right-mouse click on the project, Run/Maven Build…, and set the goal to gwt:run.

But the project will have a lot of Red ‘X’.
And it won’t run in Eclipse, which means I can’t debug it. But it will run just fine in Maven. But I don’t want it to run in Maven, I want it to run in Eclipse. Why do I want it to run in Eclipse? Because I want to use all of those really nifty debug capabilities. Otherwise, why bother using GWT, I could just use some other Javascript framework, like Jquery, or Dojo. So, how do I get to the point where I can run and debug?
First things last. Lets get rid of all of those ugly errors. One of the biggest causes of the red ‘X’ is the reliance on the generated source files that Maven builds for the Internationalization and the Asyncronous interface. So I need to get these files there. The easiest way is to have Maven generate the source files for me and copy them over.
First, I go to Run/Maven – Generate Sources.

This will create the source files under /target/generated-sources/gwt//. Since most of these errors are coming from missing client source, I just need to copy them all over to the /src/main/java//client folder. 

Now, I need to tell Maven not to generate these files, otherwise I will get an error telling me there are duplicate classes. Edit the POM, and remove the generateAsync and i18n goals. That section should look like the following when done.


But there are still some Red X. Why? The field verifier class is not being found. To fix this, I just went to Project/Clean. Strange, but that fixed it.

So, I still have a red X on my project, but I will ignore that since this is some Maven specific junk.

So, I want to try to run my project in Eclipse now to see what happens.
Run/Maven Build…, and set the goal to gwt:eclipse.
This will generate a Launch file. Refresh the project and it will appear. But there is a problem. If I try to launch it, I will get the following error.
This will resolve itself if I go to Run Configuration, and edit the Run configuration for the new project, and go to any of the other tabs. If it doesn't, find the DevMode class from the Search Dialog. But another problem manifests itself at this point. No modules appear under Available Modules.
You’re killing me… If I try to run this, I just get the help dialog for the GWT Dev Mode. But this is a simple fix also. All I need to do is Close this dialog. Right-Mouse click on the project or hit Alt-Enter to bring up the Projects Property Editor, and go under the Source section. Under the Source section, select the Excluded item under Sample/src/main/resource, and hit the Remove button.
When I go back to edit the Run Configuration, my SampleModule will be back.
And not only did this step fix the problem with the Launch configuration, it also fixed another issue where files could not be opened with the GWT Designer Window Builder editor.
I visited a lot of sites researching this issue, and that last step was nowhere to be found, and was erroneously reported as being some issue with Eclipse not finding the gwt.xml unless it was in the first source directory it encounters. This may have been the case in previous version of Eclipse, but in Indigo this solved my problem.

So there you go. If you followed these steps, you should have a setup that is as close to the project that Maven generated and still be able to run in Eclipse so you can use it’s debugging capabilities. This definitly isn’t as nice as the New Project wizards, but hey, its Mavenized, and it’s a GWT project. Or at least it’s a GWT-RPC based GWT project, and not a RequestFactory one. But that’s another story…

No comments: