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

我希望以集成的方式设置 eclipse 和 tomcat,这样对 JSP 和 servlet 的更改(如果可能的话)可以立即反映出来,而不需要进行部署。

好吧,这是一个在互联网上有很多答案的问题,但是它们看起来都不一样。(使用 Sysdeo 插件,使用 JBOss 插件,使用过时的 Eclipse,使用 MyEclipse 等等) ,我找不到一个可供参考的权威资源。那么,为了我的利益,建立这个系统最简单和最推荐的步骤是什么呢?

这里假设我有 Eclipse 和 tomcat 独立运行。实际上,我已经成功地以一种非热部署的方式集成了它们,使用说明如下: Http://www.ibm.com/developerworks/opensource/library/os-eclipse-tomcat/index.html

Eclipse 版本: 3.4.2(Ganymede) Tomcat v6.0.20

99072 次浏览

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.

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:

  • 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:

  • Guice
  • Spring
  • Struts 2

You get a free trial quite painlessly - I suggest you give it a shot.

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.

Are you open to using Jetty for development purposes?

If so, it's pretty simple to use with Maven - just run mvn jetty:run

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.

To configure tomcat to auto-reload a class when it changes, you need to

edit $TOMCAT/conf/context.xml and set :

<Context reloadable="true">

You may also have to edit and reload your webapps/$YourWebApp/web.xml file and include :

<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.

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/

I use an rsync command to sync up changes from my jsp project to the $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.

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

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.

There are two options.

First, Eclipse lets you do this, with a slight change of configuration (see also this detailed post)

  • 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

This will still reflect code changes but won't restart the server.

Second, I've used the FileSync plugin for a long time:

  • 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

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.

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.

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". Modify "Deploy path" to the tomcat deploy directory (Ex: 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". From the servers view , right click on your server , "Add and remove" , publish you project.

What works for me here is:

context.xml like this:

<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.

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.

Only with all this I got full (VM) classes hot-deploy with resources hot-deploy!!!

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 will periodically detect the changes and redeploy your application.

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.
  2. start tomcat(other container) in debug mode.

voila! :)

I've managed to get this working on my development environment using the Spring Loaded JVM agent. While I do develop Spring web applications, that project's description does mention that it is

usable on any bytecode that may run on a JVM

Using the following setup, I was able to get changes to publish automatically to an attached Tomcat instance (the usual Eclipse WTP method). I'm using Spring Tool Suite 3.7.3 based off Eclipse Mars.2 (4.5.2).

  1. Download the latest release JAR of Spring Loaded from their Github project. This has been tested with springloaded-1.2.5.RELEASE.jar.
  2. Setup a Tomcat Server in Eclipse as normal (tested with Tomcat 8.0.30)
  3. Open the Tomcat Server's configuration page in Eclipse (double-click on the server instance within the "Servers" tab).
  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).
  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
    • To enable JARs to be watched in addition to project's .class files, configure the -Dspringloaded=watchJars= VM argument per this issue's comments.

Start the server and publish Dynamic Web Application projects to it as usual. Again, I was able to test this with Spring web applications. 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. 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. 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. 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.

In Eclipse, click on the Project in the top menu and make sure “Build Automatically” is selected. This fixed my problem. I was running Eclipse with Tomcat using my workspace metadata. I was also running Maven in a Dynamic Web Module project. To verify, add your application to Tomcat. Start Tomcat from within Eclipse. Make sure your application is deployed and running. Open the Console tab in Eclipse. Make a change in your application. After a couple of seconds you should see your change getting automatically built and deployed to Tomcat.