简体   繁体   English

将tomcat和eclipse集成为热部署环境

[英]Integrating tomcat and eclipse as a hot-deploy environment

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. (使用Sysdeo插件,使用JBOss插件,使用过时的Eclipse进行操作,改用MyEclipse等),我找不到要引用的1个权威性资源。 So for my benefit, what is the simplest and most recommended procedure to set this up ? 因此,出于我的利益,最简单,最推荐的设置程序是什么?

This assumes I have eclipse and tomcat running independently. 假设我有eclipse和tomcat独立运行。 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 实际上,我已经按照此处的说明以非热部署方式集成了它们: 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

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 将Tomcat添加到服务器列表
  • make the project "Dynamic web project" (Either through the creation wizard or through the facets section in the settings) 将项目设置为“动态Web项目”(通过创建向导或通过设置中的构面部分)
  • add the project to tomcat, and configure its "Deployment assembly" 将项目添加到tomcat,并配置其“部署程序集”
  • double click tomcat from the list to open the configurations 从列表中双击tomcat以打开配置
  • Change "Publishing" to "Never publish automatically" (this means the server won't get restarted when you hit ctrl+s) 将“发布”更改为“从不自动发布”(这意味着在您按ctrl + s时不会重启服务器)
  • start tomcat in debug mode 在调试模式下启动tomcat

This will still reflect code changes but won't restart the server. 这仍将反映代码更改,但不会重新启动服务器。

Second, I've used the FileSync plugin for 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) 配置插件以将所有类从bin控制器发送到tomcat安装的WEB-INF/classes (这与配置部署程序集几乎相同)
  • 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) (可选)将所有从文件同步位置设置到单个变量的绝对路径外部化,并在eclipse中配置该变量(因此,如果所有成员都在使用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) 将Tomcat添加到Eclipse的“服务器”列表中,将“服务器位置”选项配置为“使用tomcat安装”(从服务器列表中双击tomcat时,屏幕打开)
  • start tomcat in debug mode 在调试模式下启动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 project that easily syncs the workspace with the servlet container. 我还创建了一个项目该项目可以轻松地将工作空间与servlet容器同步。

Simply let eclipse write the class files directly to the $TOMCAT/webapps/MyWebApp/WEB_INF/classes directory. 简单地让eclipse将类文件直接写入$ TOMCAT / webapps / MyWebApp / WEB_INF / classes目录。 Then, configure apache-tomcat to check if the class files have been updated, and reload them if they have. 然后,配置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配置为自动重新加载的方式。

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. JRebel-它使您无需重新启动Web应用程序即可进行编码。 It basically works by instrumenting your classes and adapting their changes. 它基本上是通过检测您的类并适应其更改来工作的。 It handles many more cases than hot deploy , including: 比热部署处理更多的情况 ,包括:

  • 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 plugins for third party frameworks, including: 它是商业化的价格详细信息 ,截至2018年6月为550美元/年),并且具有许多用于第三方框架的插件 ,包括:

  • Guice 吉斯
  • Spring 弹簧
  • Struts 2 支柱2

You get a free trial quite painlessly - I suggest you give it a shot. 您可以非常轻松地获得免费试用 -我建议您试试看。

Change your workspace in Eclipse to \\tomcat\\webapps Since it is just for your work, this should work fine. 在Eclipse中将您的工作区更改为\\ tomcat \\ webapps,因为它仅用于您的工作,所以应该可以正常工作。 Whatever changes you make in Eclipse is in the same directory tomcat looks for applications to deploy 无论您在Eclipse中进行什么更改,tomcat都在同一目录中寻找要部署的应用程序

In servers view , create a new server , don't add any resource (project) to the server yet. 在服务器视图中,创建一个新服务器,不要将任何资源(项目)添加到服务器。 Double click on the server in servers location. 双击服务器位置中的服务器。 Choose "Use tomcat installation". 选择“使用tomcat安装”。 Modify "Deploy path" to the tomcat deploy directory (Ex: c:\\server\\Tomcat 7.0.14\\webapps). 将“部署路径”修改为tomcat部署目录(例如:c:\\ server \\ Tomcat 7.0.14 \\ webapps)。 Extend "Publishing section". 扩展“发布部分”。 Check "Automatically publish after a build event". 选中“在构建事件后自动发布”。 Save and close. 保存并关闭。 From the eclipse menu check "Project -> Build automatically". 在Eclipse菜单中,选择“项目->自动生成”。 From the servers view , right click on your server , "Add and remove" , publish you project. 在服务器视图中,右键单击服务器,单击“添加和删除”,发布您的项目。

Why not use an integrated tomcat server from with in eclipse if this is just for development? 如果仅用于开发,为什么不在eclipse中使用集成的tomcat服务器? You can configure servers under window->preferences. 您可以在window-> preferences下配置服务器。 Once it's configured if you have a jsp called page.jsp you can right click it and select run on server. 一旦配置好了名为page.jsp的jsp,则可以右键单击它,然后选择“在服务器上运行”。 Under preferences->general->Web browser you can chose weather to use the built in browser or an external one. 在首选项->常规-> Web浏览器下,您可以选择天气以使用内置浏览器或外部浏览器。 Make some changes to your jsp, save, and refresh your page from the browser. 对jsp进行一些更改,保存并从浏览器刷新页面。 The changes will be automatically reflected once you save and refresh your browser. 保存并刷新浏览器后,更改将自动反映出来。

What works for me here is: 在这里对我有用的是:

context.xml like this: context.xml像这样:

<Context path="/myapp" docBase="myapp" debug="5"
    reloadable="false" antiResourceLocking="false" antiJARLocking="true">
<!-- this because i´m using Spring 3.1 with Tomcat -->
    <Loader     loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader" />

</Context>

Then, on Servers tab in Eclipse, on "Modules" tab, I disabled the "Auto Reload" to all the modules being deployed. 然后,在Eclipse的“服务器”选项卡上的“模块”选项卡上,我对正在部署的所有模块禁用了“自动重新加载”。

Also let the default config to let the auto publishing. 也让默认配置让自动发布。

And is using Maven with WTP, don´t forget to leave the contents of src/main/resources in, because Eclipse insists to exclude the contents of this folder of the final build. 并且正在将Maven与WTP一起使用,请不要忘记将src / main / resources的内容保留在其中,因为Eclipse坚持要排除最终版本中该文件夹的内容。

Only with all this I got full (VM) classes hot-deploy with resources hot-deploy!!! 只有这一切,我才能使用资源热部署来进行完整的(VM)类热部署!!!

I've managed to get this working on my development environment using the Spring Loaded JVM agent. 我已经设法使用Spring Loaded JVM代理在我的开发环境上工作了。 While I do develop Spring web applications, that project's description does mention that it is 当我确实开发Spring Web应用程序时,该项目的描述确实提到它是

usable on any bytecode that may run on a JVM 在可以在JVM上运行的任何字节码上可用

Using the following setup, I was able to get changes to publish automatically to an attached Tomcat instance (the usual Eclipse WTP method). 使用以下设置,我能够获得更改以自动发布到附加的Tomcat实例(通常的Eclipse WTP方法)。 I'm using Spring Tool Suite 3.7.3 based off Eclipse Mars.2 (4.5.2). 我正在使用基于Eclipse Mars.2(4.5.2)的Spring Tool Suite 3.7.3。

  1. Download the latest release JAR of Spring Loaded from their Github project . 从他们的Github项目下载最新版本的Spring Loaded JAR。 This has been tested with springloaded-1.2.5.RELEASE.jar. 已经使用springloaded-1.2.5.RELEASE.jar进行了测试。
  2. Setup a Tomcat Server in Eclipse as normal (tested with Tomcat 8.0.30) 照常在Eclipse中设置Tomcat服务器(已通过Tomcat 8.0.30测试)
  3. Open the Tomcat Server's configuration page in Eclipse (double-click on the server instance within the "Servers" tab). 在Eclipse中打开Tomcat服务器的配置页面(在“服务器”选项卡中双击服务器实例)。
  4. In the "Server Options" section, uncheck "Modules auto reload by default". 在“服务器选项”部分中,取消选中“默认情况下模块自动重新加载”。
    • Note: If you have web modules added to the server already, you may need to disable "Auto Reload" on them individually via the "Modules" tab (at bottom of configuration page window). 注意:如果已经将Web模块添加到服务器,则可能需要通过“模块”选项卡(位于配置页面窗口底部)分别禁用它们上的“自动重新加载”。
  5. In the "General Information" section, click "Open launch configuration" 在“常规信息”部分,单击“打开启动配置”
    • In the "Arguments" tab, add the following to the end of the "VM arguments": -javaagent:/path/to/downloaded/springloaded-1.2.5.RELEASE.jar -noverify 在“参数”选项卡中,将以下内容添加到“ VM参数”的末尾: -javaagent:/path/to/downloaded/springloaded-1.2.5.RELEASE.jar -noverify
    • To enable JARs to be watched in addition to project's .class files, configure the -Dspringloaded=watchJars= VM argument per this issue's comments . 要使除项目的.class文件外还可以监视JAR,请根据此问题的注释配置-Dspringloaded=watchJars= VM参数。

Start the server and publish Dynamic Web Application projects to it as usual. 启动服务器,然后像往常一样将Dynamic Web Application项目发布到该服务器。 Again, I was able to test this with Spring web applications. 同样,我能够使用Spring Web应用程序进行测试。 For example, modifying code, adding/subtracting methods, etc. in @Controller classes were applied nearly instantly after I save the file and Eclipse builds and publish the changed class. 例如,在我保存文件,Eclipse构建并发布更改后的类之后,@ Controller类中的修改代码,添加/减去方法等几乎立即被应用。 Lastly, the FAQ section of the Spring Loaded project did mention a few cases where things won't reload, so those would require a restart of the server/application. 最后,Spring Loaded项目的FAQ部分确实提到了一些情况,这些情况不会重新加载,因此需要重新启动服务器/应用程序。 But those exceptions are much less frequent. 但是这些例外的发生频率要低得多。

One more note: This does NOT work with the embedded Pivotal tc Server or VMware vFabric tc Server that comes with STS. 另外请注意:这不适用于STS附带的嵌入式Pivotal tc Server或VMware vFabric tc Server。 For those servers, there is a "Enable Java Agent-based reloading (experimental)" option, but that uses the older 1.2.0 release of Spring Loaded, which did not work for me. 对于这些服务器,有一个“启用基于Java代理的重新加载(实验性)”选项,但是使用的是Spring Loaded的1.2.0早期版本,这对我不起作用。

在开发环境中,只需将Tomcat服务器指向要在其中进行开发的目录中查找JSP。保存文件后,您便可以立即看到更改。

I've found a simplest way: use of symbolic links: I think this is the fastest way because there is no synchronization at all: tomcat directory and your project directory will be the same directory all the time. 我找到了一种最简单的方法:使用符号链接:我认为这是最快的方法,因为根本没有同步:tomcat目录和您的项目目录将始终处于同一目录。

Tomcat will periodically detect the changes and redeploy your application. Tomcat将定期检测更改并重新部署您的应用程序。

There is also a way to debug your application, so I humbly think is a good solution. 还有一种方法可以调试应用程序,因此我谦虚地认为这是一个很好的解决方案。

I've documented my solution here , for anyone interested. 我已经在这里记录了我的解决方案,供有兴趣的任何人使用。

In case, you're in hurry: 万一您急着:

  1. check "never publish automatically" in tomcat(other container) in eclipse. 在Eclipse的tomcat(其他容器)中检查“从不自动发布”。
  2. start tomcat(other container) in debug mode. 在调试模式下启动tomcat(其他容器)。

voila! 瞧! :) :)

In Eclipse, click on the Project in the top menu and make sure “Build Automatically” is selected. 在Eclipse中,单击顶部菜单中的“项目”,并确保选择“自动构建”。 This fixed my problem. 这解决了我的问题。 I was running Eclipse with Tomcat using my workspace metadata. 我使用工作区元数据在Tomcat上运行Eclipse。 I was also running Maven in a Dynamic Web Module project. 我也在动态Web模块项目中运行Maven。 To verify, add your application to Tomcat. 要进行验证,请将您的应用程序添加到Tomcat。 Start Tomcat from within Eclipse. 从Eclipse中启动Tomcat。 Make sure your application is deployed and running. 确保您的应用程序已部署并正在运行。 Open the Console tab in Eclipse. 在Eclipse中打开“控制台”选项卡。 Make a change in your application. 在您的应用程序中进行更改。 After a couple of seconds you should see your change getting automatically built and deployed to Tomcat. 几秒钟后,您应该会看到更改已自动构建并部署到Tomcat。

can we use the below option? 我们可以使用以下选项吗? Double click on Server in eclipse -> Server Locations -> Use tomcat installation. 双击Eclipse中的服务器->服务器位置->使用tomcat安装。 It takes control of the tomcat installation. 它控制了tomcat的安装。 You dont need to restart the server for changes in java classes. 您无需重新启动服务器即可更改Java类。

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

This may not be the best way, but it works for me. 这可能不是最好的方法,但是对我有用。

  • $tomcat/conf/Catalina/localhost/.xml $ tomcat / conf / Catalina / localhost / .xml
  • $contextPath/webapp/web/WEB-INF/lib/*.jar $ contextPath / webapp / web / WEB-INF / lib / *。jar
  • $contextPath/webapp/web/ $ contextPath / webapp / web /
  • $eclispeWorkbench/project-servlet/src/ $ eclispeWorkbench / project-servlet / src /
  • $eclispeWorkbench/project-servlet/servlet.jardesc (should export jar to lib dir in $contextPath) $ eclispeWorkbench / project-servlet / servlet.jardesc(应将jar导出到$ contextPath中的lib目录)
  • $eclipseWorkbench/project-jsp/ $ 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. 在Eclipse中为Tomcat使用远程调试,这将允许有限的热交换代码。 If you change static fields or add methods, you will need to export the jar and restart the context. 如果更改静态字段或添加方法,则需要导出jar并重新启动上下文。

Simplest, quickest: set Eclipse up to generate a WAR file when you build; 最简单,最快:在构建时将Eclipse设置为生成WAR文件。 as a post-build step, copy that WAR file into your tomcat webapps directory. 作为构建后的步骤,请将该WAR文件复制到您的tomcat webapps目录中。 Tomcat will recognize the change and refresh the site with no deployment necessary. Tomcat将识别更改,并在无需部署的情况下刷新站点。 Note that this isn't perfect; 请注意,这不是完美的。 users using that webapp may have a less than optimal experience, as Tomcat restarts the webapp "under" them; 使用该Web应用程序的用户的体验可能不尽如人意,因为Tomcat在其“下方”重新启动Web应用程序; but this does fit your request. 但这确实符合您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM