Wednesday, January 15, 2014

How to deploy a web application outside Tomcat and use JRebel to prevent restarts to see the code changes

There are 2 things I feel are slowing me down while building java applications. And 1st one is copying the web applications into webapps folder of the Tomcat.After I make changes to the java code I have to compile the code, make the war file and deploy it in the webapps folder of Tomcat. This slows down the development process and java developers just wish they can avoid this. There are couple of ways to avoid this.

Deploying web application outside the Tomcat WebApps folder


One of the way is to deploy the war outside the webapp of the Tomcat which is described below.

Move to the following location in your Tomcat installation folder.

<Tomcat Home>--> conf --> Catalina --> localhost

In this folder create a xml with the name of your web application.Say I want my service to be 'MyApp1' then create a 'MyApp1.xml' in the above path.

Add the below content in the file created.

<Context path="/MyApp1" docBase="C:\Users\vkiran\Desktop\MyApp1\war" reloadable="true">
</Context>

Your are defining the  context name for your application and providing a location which has your compiled web application for the Tomcat to load. When you start the Tomcat, Tomcat will load your web application 'MyApp1' from  'C:\Users\vkiran\Desktop\MyApp1\war' location. So you don't need to copy your war file to Tomcat webapp folder every time.

JRebel


But you still need to compile your code and restart Tomcat for every code change you make.Redeploying the application for every small change in the code is one of things I hate  most in Java. JRebel provides a relief from this constant recompiling mess. JRebel reloads the classes without redeploying the application and so you can view your code changes without restarting the applications.It supports most of the Java Frameworks, has plugins for all major java IDE's and is a great productivity tool for java developers. It is a paid tool and a trial version can be downloaded from here.

Download jrebel-5.5-nosetup.zip and extract the zip file.



In Tomcat --> bin folder, create a 'catalina-jrebel.bat' file with the following content.

@echo off
set JAVA_OPTS=-javaagent:[path/to/]jrebel.jar %JAVA_OPTS%
call "%~dp0\catalina.bat" %*

The jrebel.jar is available in the extracted zip file. Replace '[path/to/]' in the above script with the path to the jrebel folder on your system.

If you want to launch your Tomcat with JRebel enabled, run this catalina-jrebel.bat.

catalina-jrebel.bat run


If you configured jrebel correctly on Tomcat, you will see some thing similar on your command prompt when you start the Tomcat. Some information on where the jrebel log file goes, what is the version, statistics on the time saved using jrebel and license etc.


Also the command prompt will display the list of frameworks supported by jrebel but not used/enabled in your application and if there are any updates to jRebel.

If Tomcat is run as a service in your development environment, select 'Monitor Tomcat' from your Run --> Programs or double click on the Tomcat icon in the system tray. Then insert the below parameter in Java --> Java Options

-javaagent:[path/to/]jrebel.jar

Replace [path/to/] with the path to the jrebel folder on your system.



If you launch the Tomcat with JRebel on, you need not restart Tomcat to see your code changes. Just refresh your browser or test the functionality without restarting the application. JRebel reloads the modified classes and so you can concentrate on your code changes without getting distracted by the continuous re-deployments.

2 comments:

  1. Hello...thank you for good information...do you have a tutorial in video or youtube?thank you...

    ReplyDelete