jump to navigation

Starting and Stopping Tomcat from Ant April 14, 2008

Posted by Phill in Application Servers.
Tags: , , , ,
comments closed

One issue I have with Tomcat is that it doesn’t always undeploy an application cleanly. Sometimes it leaves a few .jar files in the WEB-INF/lib folder. I don’t know if there is any way around this, however it is inconvenient because it means every time I want to redeploy an application I’m working on at the moment I need to restart Tomcat.

Anyway, I’ve found an easy way of restarting Tomcat from within an Ant script – although unfortunately it only works on Windows! If you have Tomcat configured to run as a service, just drop the following into your build.xml:

<target name="tomcat-start">
	<echo>Starting Tomcat</echo>

	<exec executable="net">
		<arg value="start" />
		<arg value="${tomcat.service.name}" />
	</exec>
</target>

<target name="tomcat-stop">
	<echo>Stopping Tomcat</echo>

	<exec executable="net">
		<arg value="stop" />
		<arg value="${tomcat.service.name}" />
	</exec>
</target>

Note: you will need to have the property tomcat.service.name defined elsewhere – on my system the property value is “Apache Tomcat”.

Then, when you want to deploy a .war file to Tomcat, you just need to use <antcall target=”tomcat-stop”/> and <antcall target=”tomcat-start”/> at the start and end of your deploy target respectively. I also added in a “<delete dir =”tomcat-webapp-dir/app-name” /> after Tomcat was stopped just to be on the safe side – this may not be necessary though.