Being able to hot deploy changes you make to your Java web applications is very important. This makes development more productive since it eliminates the build and deploy steps. You use your IDE to make changes and you can see and test your changes right away. Here are ways to do it when using Eclipse.
1. Servers View
The easiest way to hot deploy your Java web application is to create a Server in the Eclipse Servers view and run your web application inside the Server. For this article, We will use Tomcat 7 as our Server.
Your web application must be assigned the Dynamic Web Module Project Facet for this to work. If you didn’t create this web application using the Eclipse New Project Wizard, you will have to assign this Project Facet in Project Properties.
Now make changes to your controller and you should see Eclipse automatically reload the web application a few seconds after you save your changes.
2. Maven Jetty Plugin
If you use Maven to run your server, you can use the Jetty Plugin. It can be configured to periodically scan for changes and automatically redeploy the webapp.
Add the Jetty plugin in pom.xml and run jetty:run. Version 6.1.26 is the latest as of this writing.
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.26</version> <configuration> <scanIntervalSeconds>5</scanIntervalSeconds> </configuration> </plugin>
project-dir> mvn jetty:run
Now make changes to your controller in Eclipse and you should see the Maven Jetty plugin automatically reload the web application.
If you have to run Tomcat from the command line or as a service, you can use JRebel. If you need to reload libraries or you have a more complicated application setup, you also will have to use JRebel. There’s a small annual license fee, and it’s free for OSS developers.
To set up, get a JRebel license and install the JRebel Eclipse Plugin from the Eclipse update site http://www.zeroturnaround.com/update-site/. When prompted for a license, copy the license file to the plugin directory and point the license prompt to that file. The plugin directory is something like <eclipse-home>pluginsorg.zeroturnaround.eclipse.embedder_4.*jrebel, and this is where jrebel.jar lives. If you use Pulse, this will be under the Genuitec/Common folder.
To setup the web application, right click on the project and go to JRebel -> Generate rebel.xml in src/main/resources.
To setup Tomcat, create <tomcat-home>/bin/startup-jrebel. cmd and use this to run Tomcat.
@echo off set JAVA_OPTS=-javaagent:"<jrebel-home>jrebel.jar" %JAVA_OPTS% call "%~dp0startup.bat" %*
Now make changes to your controller in Eclipse and you should see JRebel automatically reload the class.
4. Exploded WAR
Another approach is to deploy an exploded war file, and configure Eclipse to write classes to it. You can write a custom Project Builder to copy the .class files to the exploded war file.
If your operating system supports it, you can also create a symbolic link so you don’t have to worry about copying the files. If you don’t want to deploy an exploded war file, just overwrite the .war file every time you make changes in Eclipse using a custom Builder and Tomcat should reload the web application automatically.