Java 集成tomcat和eclipse作为热部署环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/998737/
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
Integrating tomcat and eclipse as a hot-deploy environment
提问by udit
I would like to setup eclipse and tomcat in an integrated fashion such that changes to my JSPs and servlets(if possible) are reflected immedietely without requiring a deployment.
我想以集成的方式设置 eclipse 和 tomcat,以便在不需要部署的情况下立即反映对我的 JSP 和 servlet(如果可能)的更改。
Ok, this is one of those questions that has plenty of answers all across the internet but they all seem different. (use Sysdeo plugin, use JBOss plugin, do stuff with an outdated Eclipse, use MyEclipse instead etc.) and I couldn't find 1 definitive resource to refer to. So for my benefit, what is the simplest and most recommended procedure to set this up ?
好的,这是在互联网上有很多答案的问题之一,但它们似乎都不同。(使用 Sysdeo 插件,使用 JBOss 插件,使用过时的 Eclipse,使用 MyEclipse 等),我找不到 1 个明确的资源来参考。因此,为了我的利益,设置它的最简单和最推荐的程序是什么?
This assumes I have eclipse and tomcat running independently. I actually have managed to integrate them in a non-hot deploy fashion using instructions here : http://www.ibm.com/developerworks/opensource/library/os-eclipse-tomcat/index.html
这假设我有独立运行的 eclipse 和 tomcat。我实际上已经设法使用这里的说明以非热部署方式集成它们:http: //www.ibm.com/developerworks/opensource/library/os-eclipse-tomcat/index.html
Eclipse version Version: 3.4.2 (Ganymede) Tomcat v6.0.20
Eclipse 版本 版本:3.4.2 (Ganymede) Tomcat v6.0.20
回答by Paul Sonier
Simplest, quickest: set Eclipse up to generate a WAR file when you build; as a post-build step, copy that WAR file into your tomcat webapps directory. Tomcat will recognize the change and refresh the site with no deployment necessary. Note that this isn't perfect; users using that webapp may have a less than optimal experience, as Tomcat restarts the webapp "under" them; but this does fit your request.
最简单、最快捷:在构建时设置 Eclipse 以生成 WAR 文件;作为构建后的步骤,将该 WAR 文件复制到您的 tomcat webapps 目录中。Tomcat 将识别更改并刷新站点,无需部署。请注意,这并不完美;使用该 webapp 的用户可能没有最佳体验,因为 Tomcat 会在他们“下”重新启动 webapp;但这确实符合您的要求。
回答by Robert Munteanu
Disclaimer: I'm just a happy customer, I don't work for Zero Turnaround and I'm not in any way affiliated with them.
免责声明:我只是一个快乐的客户,我不为零周转工作,我与他们没有任何关联。
Check out JRebel- it allows you to code without web application restarts. It basically works by instrumenting your classes and adapting their changes. It handles many more cases than hot deploy, including:
查看JRebel- 它允许您在不重新启动 Web 应用程序的情况下进行编码。它基本上通过检测您的类并调整它们的变化来工作。它处理的情况比 hot deploy 多得多,包括:
- Adding/removing methods
- Adding/removing constructors
- Changing interfaces
- Adding/removing implemented interfaces
- 添加/删除方法
- 添加/删除构造函数
- 更改接口
- 添加/删除已实现的接口
It's commercial, ( pricing details, $550/year as of June 2018 ) , and it has a lots of pluginsfor third party frameworks, including:
它是商业的(定价细节,截至 2018 年 6 月,每年 550 美元),并且它有很多第三方框架的插件,包括:
- Guice
- Spring
- Struts 2
- 吉斯
- 春天
- 支柱 2
You get a free trialquite painlessly - I suggest you give it a shot.
您可以毫不费力地获得免费试用- 我建议您试一试。
回答by Cameron
In your development environment, just point your Tomcat server to look for JSPs in whatever directory you're developing them in. As soon as you save the file, you'll be able to see the changes then.
在您的开发环境中,只需将您的 Tomcat 服务器指向您正在开发它们的任何目录中查找 JSP。一旦保存文件,您就可以看到更改。
回答by matt b
Are you open to using Jetty for development purposes?
您是否愿意将 Jetty 用于开发目的?
If so, it's pretty simple to use with Maven - just run mvn jetty:run
如果是这样,使用 Maven 非常简单 - 只需运行 mvn jetty:run
回答by Martin Tilsted
Simply let eclipse write the class files directly to the $TOMCAT/webapps/MyWebApp/WEB_INF/classes directory. Then, configure apache-tomcat to check if the class files have been updated, and reload them if they have.
只需让 eclipse 将类文件直接写入 $TOMCAT/webapps/MyWebApp/WEB_INF/classes 目录即可。然后,配置 apache-tomcat 以检查类文件是否已更新,如果已更新,则重新加载它们。
To configure tomcat to auto-reload a class when it changes, you need to
要将 tomcat 配置为在更改时自动重新加载类,您需要
edit $TOMCAT/conf/context.xml
and set :
编辑$TOMCAT/conf/context.xml
和设置:
<Context reloadable="true">
You may also have to edit and reload your webapps/$YourWebApp/web.xml
file and include :
您可能还需要编辑和重新加载您的webapps/$YourWebApp/web.xml
文件并包括:
<web-app reloadable="true">
I can't remember if both changes are needed but that's how I configured my tomcat-6.0.18 to auto-reload.
我不记得是否需要进行这两项更改,但这就是我将 tomcat-6.0.18 配置为自动重新加载的方式。
回答by TJR
This may not be the best way, but it works for me.
这可能不是最好的方法,但它对我有用。
- $tomcat/conf/Catalina/localhost/.xml
- $contextPath/webapp/web/WEB-INF/lib/*.jar
- $contextPath/webapp/web/
- $eclispeWorkbench/project-servlet/src/
- $eclispeWorkbench/project-servlet/servlet.jardesc (should export jar to lib dir in $contextPath)
- $eclipseWorkbench/project-jsp/
- $tomcat/conf/Catalina/localhost/.xml
- $contextPath/webapp/web/WEB-INF/lib/*.jar
- $contextPath/webapp/web/
- $eclispeWorkbench/project-servlet/src/
- $eclispeWorkbench/project-servlet/servlet.jardesc(应该将 jar 导出到 $contextPath 中的 lib 目录)
- $eclipseWorkbench/project-jsp/
I use an rsync command to sync up changes from my jsp project to the $contextPath.
我使用 rsync 命令将更改从我的 jsp 项目同步到 $contextPath。
Use remote debugging in Eclipse for Tomcat which will allow limited hot-swapping of code. If you change static fields or add methods, you will need to export the jar and restart the context.
在 Eclipse 中为 Tomcat 使用远程调试,这将允许有限的代码热交换。如果更改静态字段或添加方法,则需要导出 jar 并重新启动上下文。
回答by PaganSensei
Change your workspace in Eclipse to \tomcat\webapps Since it is just for your work, this should work fine. Whatever changes you make in Eclipse is in the same directory tomcat looks for applications to deploy
将 Eclipse 中的工作区更改为 \tomcat\webapps 由于它仅适用于您的工作,因此应该可以正常工作。您在 Eclipse 中所做的任何更改都在同一目录中 tomcat 查找要部署的应用程序
回答by Jared
Why not use an integrated tomcat server from with in eclipse if this is just for development? You can configure servers under window->preferences. Once it's configured if you have a jsp called page.jsp you can right click it and select run on server. Under preferences->general->Web browser you can chose weather to use the built in browser or an external one. Make some changes to your jsp, save, and refresh your page from the browser. The changes will be automatically reflected once you save and refresh your browser.
如果这只是为了开发,为什么不在 Eclipse 中使用集成的 tomcat 服务器?您可以在 window->preferences 下配置服务器。配置好后,如果您有一个名为 page.jsp 的 jsp,您可以右键单击它并选择在服务器上运行。在首选项-> 常规-> Web 浏览器下,您可以选择天气以使用内置浏览器或外部浏览器。对您的 jsp 进行一些更改,保存并从浏览器刷新您的页面。保存并刷新浏览器后,更改将自动反映。
回答by Bozho
There are two options.
有两种选择。
First, Eclipse lets you do this, with a slight change of configuration (see also this detailed post)
首先,Eclipse 允许您执行此操作,只需稍微更改配置(另请参阅此详细帖子)
- add Tomcat to the server list
- make the project "Dynamic web project" (Either through the creation wizard or through the facets section in the settings)
- add the project to tomcat, and configure its "Deployment assembly"
- double click tomcat from the list to open the configurations
- Change "Publishing" to "Never publish automatically" (this means the server won't get restarted when you hit ctrl+s)
- start tomcat in debug mode
- 将Tomcat添加到服务器列表
- 使项目成为“动态 Web 项目”(通过创建向导或设置中的 facets 部分)
- 将项目添加到tomcat,并配置其“部署程序集”
- 双击列表中的 tomcat 打开配置
- 将“发布”更改为“从不自动发布”(这意味着当您按 ctrl+s 时服务器不会重新启动)
- 以调试模式启动 tomcat
This will still reflect code changes but won't restart the server.
这仍将反映代码更改,但不会重新启动服务器。
Second, I've used the FileSync pluginfor a long time:
其次,我使用FileSync 插件已经很长时间了:
- configure the plugin to send all classes from the bin director to
WEB-INF/classes
of your tomcat installation (this is almost the same as configuring the deployment assembly) - configure all other resources to go to their respective locations
- optionally, externalize all absolute paths from the filesync location settings to a single variable, and configure that variable in eclipse (thus you will be able to commit the filesync settings as well, if all members are using eclipse)
- add Tomcat to the Servers list in eclipse, configure the "Server locations" option to be "Use tomcat installation" (the screen opens when you double-click tomcat from the servers list)
- start tomcat in debug mode
- 配置插件以将所有类从 bin Director 发送到
WEB-INF/classes
您的 tomcat 安装(这与配置部署程序集几乎相同) - 配置所有其他资源以转到其各自的位置
- 可选地,将文件同步位置设置的所有绝对路径外部化为单个变量,并在 eclipse 中配置该变量(因此,如果所有成员都使用 eclipse,您也可以提交文件同步设置)
- 在eclipse中将Tomcat添加到Servers列表中,将“Server Locations”选项配置为“Use tomcat installation”(在servers列表中双击tomcat会打开这个界面)
- 以调试模式启动 tomcat
It works perfectly for me that way. Every non-structural change is reflected immediately, without redeploy.
这种方式对我来说非常适合。每个非结构性变化都会立即反映出来,无需重新部署。
Update:
You can read more about the methods for hot-deploying here.
I also created a projectthat easily syncs the workspace with the servlet container.
回答by Tushar Sagar
can we use the below option? Double click on Server in eclipse -> Server Locations -> Use tomcat installation. It takes control of the tomcat installation. You dont need to restart the server for changes in java classes.
我们可以使用以下选项吗?在eclipse中双击Server -> Server Locations -> 使用tomcat安装。它控制了 tomcat 的安装。您不需要重新启动服务器以更改 Java 类。