Working with Eclipse, Maven and Tomcat
It occurred to me recently that my previous article on working with Eclipse, Maven and Tomcat and making them "play nicely" seems a little out of date. There have been quite a few advancements in all of those technologies since the original publication of the guide. The old article gets plenty of hits as it is linked from a Stackoverflow thread, so by way of ensuring this information is current, I am going to detail how I currently work with Maven web-app projects to run on Tomcat using Eclipse as an IDE. It is considerably simpler now than it used to be to get a working usable setup.
Goals
- Application will run in Eclipse and standalone without modification - hence simple debugging is possible
- Don't need to deploy the application to test it
- No configuration required in the web container to make the application work
Running a web application using Tomcat in Eclipse
I am using Eclipse Indigo (although the same should work with Helios and the M2Eclipse plugin). Assuming you have a Maven WAR project in Eclipse with a deployment descriptor located in the project at src/main/webapp/WEB-INF/web.xml
, you can launch it easily in Eclipse by doing
- Right-click your web app project -> Run as -> Run configurations
- Right click Maven Build in the left pane -> New
- Set the name to something suitable (usually your project name)
- For base directory, enter
${workspace_loc:/your-project-name}
- For goal, enter
tomcat:run
- Click run
For future launches, you should simply be able to click the Run as
or Debug as
icons on the toolbar and select the name that you chose in step 3. You can set breakpoints if needed.
Running a web application standalone
Here, there are two choices.
From the root of the Maven project, you can issue the command mvn tomcat:run
. Alternatively, you can build your WAR file using mvn package
and copy the .WAR file from the target/
directory to a Tomcat or other servlet container deployment directory and start it up.