jump to navigation

Starting and Stopping Tomcat from Ant April 14, 2008

Posted by Phill in Application Servers.
Tags: , , , ,
trackback

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.

Advertisements

Comments

1. Peter Thomas - April 29, 2008

You can also consider the approach I blogged here:

http://ptrthomas.wordpress.com/2006/03/25/how-to-start-and-stop-tomcat-from-ant/

It’s not Windows specific and you can easily pass JVM options etc.

2. Phill - April 29, 2008

Yes, that would also work. I think the reason I went with the Windows-only option was just because I already had Tomcat configured as a Windows service and I didn’t want to run a separate instance from Ant.

But your option would probably be better for most cases! 🙂

3. Bogdan - April 29, 2009

Here is also a variant to recreate the service from scratch every redeployment. That is very useful if you are maintaining several hosts and doing the changes in service setup:
http://gusiev.com#Ant task to install tomcat service


Sorry comments are closed for this entry

%d bloggers like this: