Friday, March 16, 2012

Ant target for Apache Tomcat

This tutorial will explain how to write an ant target to start and stop tomcat. For this task we use a special class located in "tomcat/bin/bootstrap.jar" namely "org.apache.catalina.startup.Bootstrap". Also this  jar will be depend on another jar which is located at the same location "tomcat/bin/tomcat-juli.jar".

First add these locations to a class path like below.
(here the ${tomcat} refers to the root of your tomcat directory.)


<path id="tomcat-classpath">
 <fileset dir="${tomcat}/lib">
  <include name="*.jar" />
 </fileset>
 <fileset dir="${tomcat}/bin">
  <include name="tomcat-juli.jar" />
  <include name="bootstrap.jar" />
 </fileset>
</path>


Then we write start-tomcat target to start the apache tomcat.


<target name="start-tomcat">
 <java classname="org.apache.catalina.startup.Bootstrap" fork="true">
  <classpath refid="tomcat-classpath" />
  <jvmarg value="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" />
  <jvmarg value="-Djava.util.logging.config.file=${tomcat}/conf/logging.properties" />
  <jvmarg value="-Dcatalina.home=${tomcat}" />
  <jvmarg value="-Dcatalina.base=${tomcat}" />
  <jvmarg value="-Djava.io.tmpdir=${tomcat}/temp" />
  <arg line="start"/>
 </java>
</target>


And then we write the stop-tomcat target to stop the running tomcat instance.


<target name="stop-tomcat">
 <java classname="org.apache.catalina.startup.Bootstrap" fork="true">
  <classpath refid="tomcat-classpath" />
  <jvmarg value="-Dcatalina.home=${tomcat}" />
  <arg line="stop" />
 </java>
</target>


Ok! we are done!!!

No comments:

Post a Comment