Integrating tomcat and eclipse as a hot-deploy environment
Asked Answered
S

16

87

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.

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 ?

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 version Version: 3.4.2 (Ganymede) Tomcat v6.0.20

Shull answered 15/6, 2009 at 22:23 Comment(2)
4 years later, I'm baffled by the fact that it's still an issue. Come on, it's pretty fundamental.Mafalda
7 years later (almost), I think I've discovered a solution: https://mcmap.net/q/238015/-integrating-tomcat-and-eclipse-as-a-hot-deploy-environmentUpsydaisy
L
75

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.

Loseff answered 31/5, 2011 at 14:24 Comment(8)
@VladimirTsvetkov no - these are two separate optionsLoseff
I'm trying your first approach but I'm using Maven Overlays and when I change a file in one of the overlays the server is still restarted (the overlay is not dynamic web project but dynamic web module). Any ideas?Impression
I have the impression that by changing "Publishing" to "Never publish automatically" the JSPs are not reloaded/recompiled anymore when changed. The class-reloading part works fine though by starting Tomcat in debug mode.Hajji
I do it in another way: first, change to automatically publish; second, click Modules tab in server configuration, then click edit, uncheck auto deploy enabled, I think this is the most simple way. PS: your first way does not work for me.Speakeasy
@hiway, where is this Modules tab inside server configuration tab?Guerdon
@Loseff .. I tried your method .. and it works perfectly. However, this results in JSP modifications are not reflected anymore?Venable
did you declare the src/main/webapp folder to be synced?Loseff
I just want to add, not sure if this is just my setup. When I installed tomcat-8.0.26 into eclipse mars, it already reloads whenever i changed anything in either my servlet or my JSP files. I didn't do any configurations, it just automatically happens. I didn't experience this in previous versions.Freckle
A
30

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.

Abradant answered 16/6, 2009 at 1:34 Comment(3)
According to the documentation, tomcat.apache.org/tomcat-6.0-doc/config/context.html they recommend against placing Context elements in the server.xml file, but it should monitor /WEB-INF/classes/ and /WEB-INF/lib for any changes. Where did you read about the attribute for the web-app element?Walk
Sorry, misread your answer a little. The context.xml "should be loaded for each web application".Walk
How do you configure where Eclipse writes the class-files to ?Hajji
H
7

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.

Hbomb answered 15/6, 2009 at 22:42 Comment(3)
Jrebel, when it works, it works. When it doesn't, its a nightmare. I have been beating my head with their support for over a week and I still cant get it to work in my simple spring mvc setup. They will not help further without me sending them my app or a helloworld of my app. AND, your prices are out of date - they now charge a bomb. Thank God I only used their trial versionSkyscraper
Current price is $365/year/seatRichburg
And now its $550/year/seatFrequentation
B
3

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

Barth answered 16/6, 2009 at 15:10 Comment(0)
C
3

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.

Clova answered 7/2, 2012 at 9:17 Comment(0)
R
2

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.

Rother answered 16/6, 2009 at 18:16 Comment(0)
A
2

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

Amu answered 7/5, 2012 at 20:53 Comment(0)
U
2

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.

Upsydaisy answered 6/5, 2016 at 5:11 Comment(2)
It seems you must take Spring in, How about Non Spring application.Gristle
Though you need the SpringLoaded JAR to be configured for Tomcat, the running applications themselves does NOT need to be Spring applications (nor include any Spring JARs). I tested this successfully with a simple, single HelloWorld Servlet.Upsydaisy
L
1

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.

Latrena answered 15/6, 2009 at 22:45 Comment(0)
M
0

Are you open to using Jetty for development purposes?

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

Myoglobin answered 16/6, 2009 at 0:45 Comment(0)
T
0

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.

Torrid answered 16/6, 2009 at 2:19 Comment(0)
S
0

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.

Singletree answered 18/11, 2011 at 12:22 Comment(0)
H
0

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.

Hammack answered 19/5, 2014 at 7:45 Comment(1)
can i know why there is a need to use remote java application since it is using localhost meaning I am debugging using my own local machineTenfold
S
0

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! :)

Sesqui answered 7/1, 2015 at 7:36 Comment(0)
K
0

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.

Krasnodar answered 3/10, 2017 at 2:30 Comment(0)
D
-2

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.

Deviate answered 15/6, 2009 at 22:33 Comment(1)
ok...I guess I didnt make my intentions clear. I really am not interested in deploying the app to some users, but I'm trying to run a tomcat with eclipse for my everyday development needs and to shorten the feedback cycle of (make change) -> (See Result) instead of (Make Change) -> Deploy -> (See Result)Shull

© 2022 - 2024 — McMap. All rights reserved.