如何在 Eclipse 中正确管理 Tomcat Web 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/787070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:40:28  来源:igfitidea点击:

How to properly manage Tomcat web apps inside Eclipse?

javaeclipsetomcat

提问by Shane

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.

我曾经在我的机器上单独运行Tomcat。我有一个 Ant 脚本可以重建我的项目,在本地部署它,然后重新启动 Tomcat。一切正常,但我无法在 Eclipse 中调试 Web 应用程序。

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.

所以我学习了如何在 Eclipse 中设置 Tomcat 并运行我的 Web 应用程序。现在的问题是我不完全理解如何以这种方式管理它。Eclipse 设置为根据更改自动构建我的项目,但这些更改似乎并不总是反映在 Web 应用程序中。有时我必须手动构建项目并手动“清理”服务器以反映更改。

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?

是否有关于如何管理此设置的规则?例如,如果我只更改一个JSP,那么它会自动同步吗?如果我更改了一个servlet类,那么我需要手动重建项目吗?这些规则是否一致,还是我应该每次都手动重建和清理?

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

附注。我正在使用 Eclipse 3.4.1 Java EE 包和 Tomcat v5.5

采纳答案by John Meagher

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

您可以按照您提到的方式使用 Eclipse 和 Tomcat。首先是如何设置它的基础知识:

  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.
  1. 在 Servers 视图中设置一个新的 Tomcat 服务器,指向你的 TOMCAT_HOME
  2. 确保您的项目是 Eclipse“Web 项目”。您可能需要创建一个虚拟文件并复制 .settings 中的一些文件(查看 wst 文件)。
  3. 通过右键单击服务器视图中的服务器并“添加和删除项目...”将项目部署到 Tomcat,将项目添加到服务器。

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.

您可以运行服务器并对其进行测试,就像在 Eclipse 之外运行 Tomcat 一样。如果您在调试模式下运行服务器,您可以设置断点并逐步执行代码。

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.

至于何时需要重新启动服务器 Eclipse 通常非常擅长自动部署更改。您几乎不需要重新启动以更改 jsp 页面。如果您更改一个类,它会在您更改方法主体时自动部署更改(通常)。如果您更改类的签名(添加或删除方法或更改它的参数),您几乎总是需要重新启动。对配置文件(web.xml 或类似文件)的任何更改也几乎总是需要重新启动。

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

要重新启动,只需单击服务器视图中的“调试”或“运行”按钮。您的所有更改都将重新部署到 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).

需要注意的一件事是,在默认配置中,不会使用 TOMCAT_HOME 中的“webapp”目录。相反,它将使用 Eclipse 工作区目录 (WORKSPACE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0) 下的文件夹。

回答by Tom

I found two things important to understand:

我发现有两件事需要理解:

  • Eclipse does not automatically realize if files were changed outside of Eclipse. clicking Refresh on a project does that, so does F5. You can also change a setting to refresh automatically, which, however, does not detect changes instantly (my gut feeling says up to 10 secs delay)

  • Working with servers has the concept of "Publishing" files to Tomcat. This normally happens automatically within a second after any change. Changing many classes can cause many server reloads, which can be a drag if a context reload takes some time (as complex Spring apps certainly do). So I changed a setting to not publish automatically (double-click Server instance)

  • Eclipse 不会自动意识到文件是否在 Eclipse 之外被更改。单击项目上的“刷新”会这样做,F5 也是如此。您还可以将设置更改为自动刷新,但不会立即检测到更改(我的直觉说最多延迟 10 秒)

  • 使用服务器的概念是将文件“发布”到 Tomcat。这通常会在任何更改后的一秒钟内自动发生。更改许多类会导致​​许多服务器重新加载,如果上下文重新加载需要一些时间(复杂的 Spring 应用程序肯定会这样做),这可能会造成拖累。所以我改了一个设置不自动发布(双击Server实例)

回答by Dima

Normally you should republish the application to get latest changes, don't forget to synchronize with file system first in case your files modified extrernally. You can specify that your application is automatically reloaded when modified, look for Auto-reload attribute in server.xml of your server configuration project. When set to true, tomcat will automatically reload your application. It's not always a good idea by the way. Modified JSP's should work automatically, no need to restart the applciation.

通常您应该重新发布应用程序以获取最新更改,不要忘记首先与文件系统同步,以防您的文件被外部修改。您可以指定您的应用程序在修改时自动重新加载,在您的服务器配置项目的 server.xml 中查找 Auto-reload 属性。当设置为 true 时,tomcat 将自动重新加载您的应用程序。顺便说一下,这并不总是一个好主意。修改后的 JSP 应该会自动工作,不需要重新启动应用程序。

回答by Shai

If you change the structure of a class that has already been loaded and used (add/remove members, change method signature etc.) your code changes will not be reflected. This is not an eclipse issue but a JVM issue. If you make simple code changes, like logic changes inside an existing method, your changes will take effect after the class is compiled and re-deployed. Regardless of that, if you change a public constant, you have to rebuild your project(s).

如果您更改已加载和使用的类的结构(添加/删除成员、更改方法签名等),则不会反映您的代码更改。这不是 Eclipse 问题,而是 JVM 问题。如果您进行简单的代码更改,例如在现有方法中更改逻辑,您的更改将在类编译并重新部署后生效。无论如何,如果您更改公共常量,则必须重建您的项目。