Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly manage Tomcat web apps inside Eclipse?

I used to run Tomcat separately on my machine. I had an Ant script that would rebuild my project, deploy it locally, and restart Tomcat. That all worked ok, but I wasn't able to debug the web app inside Eclipse.

So I learned how to setup Tomcat inside Eclipse and got my web app running. Now the problem is that I don't understand fully how to manage it this way. Eclipse is set to automatically build my project on changes, but those changes don't seem to always be reflected in the web app. Sometimes I have to manually build the project and manually "clean" the server for the changes to be reflected.

Are there rules somewhere about how to manage this setup? For instance, if I only change a JSP then will it automatically be synchronized? If I change a servlet class, then I need to manually rebuild the project? Are these rules consistent, or should I just manually rebuild and clean every time?

I would really appreciate it if someone could give me the best practice rules or point me to a good resource to learn how to manage this environment.

PS. I am using Eclipse 3.4.1 Java EE package and Tomcat v5.5

like image 308
Shane Avatar asked Apr 24 '09 18:04

Shane


1 Answers

You can use Eclipse and Tomcat in the way you mention. First the basics of how to set it up:

  1. In the Servers view setup a new Tomcat server pointing to your TOMCAT_HOME
  2. Make sure your project is an Eclipse "web project". You may need to create a dummy one and copy over some of the files in .settings (look at the wst files).
  3. Deploy your project to Tomcat by right clicking on the server in the Servers view and "Add and Remove Projects..." to add your project to the server.

You can run your server and test it out just like you were running Tomcat outside of Eclipse. If you run the server in Debug mode you can set breakpoints and step through the code.

As for when you will need to restart the server Eclipse is usually pretty good about auto-deploying the changes. You will pretty much never need to restart for changes to jsp pages. If you change a class it will auto-deploy the change (usually) if you change the body of a method. If you change the signature of a class (add or remove a method or change args for it) you will almost always need to restart. Any changes to configuration files (web.xml or similar) will also almost always require a restart.

To restart just click on the "Debug" or "Run" button in the Server view. All your changes will be redeployed into Tomcat.

One thing to watch out for is that in the default configuration your "webapp" directory in TOMCAT_HOME will not be used. Instead it will use a folder under your Eclipse workspace directory (WORKSPACE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0).

like image 156
John Meagher Avatar answered Sep 18 '22 16:09

John Meagher