Monday, January 25, 2010

Dynamic Web Projects in Eclipse

As I fixed a maven war project to more effectively use the Web Tools Project (WTP), I realized I could share the configuration I used with the hope to help others get some light as needed.  I'll be assuming you are familiar with the .classpath file as well as the .project file (which Eclipse uses heavily) to setup the dynamic web project.  Furthermore, I'll assume you are familiar with the notion of a "nature" within the Eclipse environment.  Lastly, I would like to refer readers to the official Eclipse FAQ.  In particular, I would like to refer you to the section where the sharing of the .project and .classpath files in version control (e.g. subversion, git, etc) is considered a good practice. Note that these tips will also work with the Springsource Tool Suite (STS)

The first thing you need to worry about is to make sure you have all bundles installed from the Java Enterprise Update Site.  Notice that this is different that the standard Eclipse distribution. If you successfully installed it, you should have the ability to create a new "Dynamic Web Project" from the new Project menu. Once that is in place and you created a new "Dynamic Web Project" take a look at the .classpath file. It has some entries of type "classpathentry". It should look something like:

The kind of classpathentry "con" means "classpath container". It's the way WTP will resolve classpath entries as you deploy your Dynamic Web Project via Eclipse. Now, take a look at your .project file. It should look something like:

There could be other buildcommand entries or other nature entries, however these are very important to have in order to enable all tooling support for a "Dynamic Web Project". Now, the one file that has some more information is found within the .settings folder. This file is named org.eclipse.wst.common.component. This file is very interesting. It shows how Eclipse (or STS) will deploy the project to the Server of your choice (e.g. Tomcat). It should look something like:

 Hopefully, this file speaks for itself (as far as to understand how WTP deploys this project as an "exploded" war).  Notice that I'm using a traditional maven directory structure for defining a webapp. As these files are in place, you should be able to execute the "Run In Server" command (from the "Run" menu) and, assuming you have a server configured, you should be able to see your web application running via Eclipse. Of course, this means you could not only "Run" but also "Debug" whereby all your breakpoints and other debugging tools would work seemlessly.

No comments: