start stop tomcat with ant

Here is a simple ant target to start/stop your instance of tomcat running

<target name=”start-tomcat”>
<if>
<not>
<http url=”http://localhost:8080″ />
</not>
<then>
<java jar=”${tomcat.home}/bin/bootstrap.jar” fork=”true”>
<jvmarg value=”-Dcatalina.home=${tomcat.home}” />
</java>
<echo message=”Server Started”></echo>
</then>
<else>
<echo message=”Tomcat Instance Already Running”></echo>
</else>
</if>
</target>

<target name=”stop-tomcat”>
<if>
<http url=”http://localhost:8080″ />
<then>
<java jar=”${tomcat.home}/bin/bootstrap.jar” fork=”true”>
<jvmarg value=”-Dcatalina.home=${tomcat.home}” />
<arg line=”stop” />
</java>
<echo message=”Server Terminated”></echo>
</then>
<else>
<echo message=”No Tomcat Instance Running”></echo>
</else>
</if>
</target>

These targets would first check to see if any instance of tomcat is running or not else would initiate the process

Conditions:

  • your tomcat.home variable is set
  • server runs on localhost:8080

Leave a Comment