Friday, September 23, 2011

Apache Tomcat - Web Application Deployment

Have you ever tried deploying web applications in Tomcat, keeping the war file somewhere other than 'webapps' directory. For example, inside your preferred directory.

Once I had to find a method to do so. So I found out what actually happens when we put a .war file into 'webapps' directory in Tomcat. (This is the conventional way we use to deploy web applications in Tomcat.)

Here is the directory structure of Tomcat




As we all know when we put a .war file in to the 'webapps' directory in Tomcat, while Tomcat is running, it is deployed automatically. During the deployment process, Tomcat unpacks the .war file and creates a directory with the name of the application and keeps it inside the 'webapps' directory. Further, it puts a context XML file inside $CATALINA_HOME/conf/Catalina/localhost/ directory.

These things happen automatically, according to a preconfigured method. But we can deploy web applications, by manually creating the above mentioned context XML file.

Here are the simple steps we can follow in order to deploy web applications in Tomcat server without putting the .war file inside 'webapps' directory.

To do so, we need to create the context XML manually and put it in to the $CATALINA_HOME/conf/Catalina/localhost/ directory.

  1. Keep the application-name.war file inside your preferred directory.
  2. Create the context XML file with the name application-name.xml 
  3. This context XML file should contain the "Context element" as below
  4. Put the context XML file inside $CATALINA_HOME/conf/Catalina/localhost/ directory.

Once you put the context XML file inside the above mentioned directory, Tomcat search for the .war file (or the unpacked web application folder) in the location where you given as the "docBase" attribute value in the above mentioned "Context element". Then it will unpack the .war file in to the "webapps" directory.
So now the web application is deployed. :) 

Note : Remember, if you put the context XML file inside Tomcat , before placing the .war file in the location you mentioned, Tomcat will throw an exception saying it cannot find the war file in the location you've mentioned.




No comments: