Tuesday, August 13, 2013

How to restart (stop/start) the tomcat using Ant

In case you are using tomcat on your local machine and using ant script to deploy your project war to tomcat webserver, this build script can come handy.

Please note, this may not work on your machine just by doing a copy paste. You may need to do a bit of change in the build.properties file to change the parameters there and also little bit of change in the build.xml to suit your exact requirement.

When I wrote this script, this build.xml was kept in the project home directory, which is normally the case for most of the web projects.

Our Goal:


  • Our goal here is to stop the tomcat if already running, then copy the war file to tomcat's webapps directory and after that start the tomcat again.
  • In addition, skip the tomcat stop, if it is not running already. Just copy the war file to tomcat's webapps directory and after that start the tomcat again.

Steps this script does:


  • The ant script, first start executing the target named, tomcat-start (which is meant to start the tomcat).
  • As you can see the depends attribute,  tomcat-start, depends on  deploy-local, so it executes the deploy-local
  • deploy-local in turn depends on tomcat-stop.
  • tomcat-stop depends on the check-port.
  • what check-port does is, it sets the property, "tomcat.running". Sets this parameter to true if it the tomcat is running on localhost, port 8080.
  • Please note that the port can differ depending on your tomcat configuration/installation. So, you might need to change this port to the one on which your tomcat runs.
  • Once this is done, the tomcat-stop, stops the tomcat, if "tomcat.running" sets to true.
  • deploy-local, copies the war file to tomcat's webapps directory.
  • Once the war is copied, tomcat-start starts the tomcat, and you app is ready to be tested on your local machine.

build.properties:


 project.name=MyProject  
 project.dir=C\://My_WorkSpace//MyProject  
 #Tomcat Local setting  
 tomcat.home=C\://Program Files (x86)//Apache Software Foundation//Tomcat 5.0  

 

build.xml



<?xml version="1.0" encoding="UTF-8"?>
<project name="${project.name}" default="tomcat-start" basedir=".">
 <!-- previous default task : tomcat-start -->
 <property file="build.properties" />
 <property environment="env" />


  
 <!--stopping tomcat  , if it is found running -->
 
 <target name="tomcat-stop" if="tomcat.running" depends="check-port" >
  <echo message="Tomcat is running...stopping it"/> 
     <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" >
         <jvmarg value="-Dcatalina.home=${tomcat.home}"/>
         <arg line="stop"/>
     </java>
 </target>
 

 <!-- Check whether Tomcat is running -->

   <target name="check-port" description="Check whether Tomcat is running">
     <echo message="Checking whether Tomcat is running"/>
     <condition property="tomcat.running">
       <socket server="localhost" port="8080"/> 
     </condition>
   </target>


 <!--    deploy-local: delete and copy ear --> 
 
 <target name="deploy-local" depends="tomcat-stop">
  
  <delete>
   <fileset dir="${tomcat.home}/webapps">
       <include name="${project.name}.war"/>
   </fileset>
  </delete>
  
  <delete dir="${tomcat.home}/webapps/${project.name}"/>
  
  <copy todir="${tomcat.home}/webapps">
   <fileset dir="${dist.dir}">
    <include name="**/${project.name}.war" />
   </fileset>
  </copy>
  
  <tstamp>
   <format property="TODAY_INDIA" pattern="HH:mm:ss:sss zzz" locale="en,In"/>
  </tstamp>
  <echo> Build Completed At: ${TODAY_INDIA}</echo>
  
      
 </target>
 
 <!-- start tomcat, it depends on deploy-local -->
 
 <target name="tomcat-start" depends="deploy-local">
       <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
           <jvmarg value="-Dcatalina.home=${tomcat.home}"/>
           <arg line="start"/>
       </java>
 </target>
  
 
</project>

Related Links:

 


3 comments:

  1. It's really a cool and elpful piece of info. I'm satisfied that you shared
    this helpful info with us. Please keep us informed like this.

    Thanks for sharing.

    ReplyDelete

Prototype

Prototype is another creation pattern. Intent:  - Intent is to create objects by cloning existing instance  - Specify the kinds of obj...