How do I deploy to Tomcat using Ant?
Author: Deron Eriksson
Description: This tutorial describes how to deploy to Tomcat with Ant and Tomcat's Manager.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page:    1 2 3 >

It's quite easy to use AntSW to deploy a warW file to TomcatSW, and this tutorial will show you one way how. This tutorial uses the following build.xml file.

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="tomcat-demo" default="war" basedir=".">
	<property file="build.properties" />
	
	<property name="war-file-name" value="${project-name}.war" />
	<property name="source-directory" value="src" />
	<property name="classes-directory" value="bin" />
	<property name="web-directory" value="web" />
	<property name="web-xml-file" value="web/WEB-INF/web.xml" />
	<tstamp prefix="build-info">
		<format property="current-date" pattern="d-MMMM-yyyy" locale="en" />
		<format property="current-time" pattern="hh:mm:ss a z" locale="en" />
		<format property="year-month-day" pattern="yyyy-MM-dd" locale="en" />
	</tstamp>
	<property name="build-directory" value="build" />

	<property name="ftp-remotedir" value="uploaded-wars/${project-name}/${build-info.year-month-day}" />

	<taskdef name="start" classname="org.apache.catalina.ant.StartTask" />
	<taskdef name="stop" classname="org.apache.catalina.ant.StopTask" />
	<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
	<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />

	<target name="stop" description="stop application in tomcat">
		<stop url="${tomcat-manager-url}" username="${tomcat-manager-username}"
			password="${tomcat-manager-password}" path="/${project-name}" />
	</target>

	<target name="start" description="start application in tomcat">
		<start url="${tomcat-manager-url}" username="${tomcat-manager-username}"
			password="${tomcat-manager-password}" path="/${project-name}" />
	</target>
	
	<target name="undeploy" description="undeploy from tomcat">
		<undeploy 
			failonerror="no"
			url="${tomcat-manager-url}"
			username="${tomcat-manager-username}"
			password="${tomcat-manager-password}"
			path="/${project-name}"
		/>
	</target>
	
	<target name="deploy" description="deploy to tomcat">
		<echo>deploying from client</echo>
		<deploy 
			url="${tomcat-manager-url}"
			username="${tomcat-manager-username}"
			password="${tomcat-manager-password}"
			path="/${project-name}"
			war="file:/projects/workspace/${project-name}/${build-directory}/${war-file-name}"
		/>
	</target>
	
	<target name="war" depends="">
		<mkdir dir="${build-directory}" />
		<delete file="${build-directory}/${war-file-name}" />
		<war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
			<classes dir="${classes-directory}" />
			<fileset dir="${web-directory}">
				<!-- Need to exclude it since webxml is an attribute of the war tag above -->
				<exclude name="WEB-INF/web.xml" />
			</fileset>
			<manifest>
				<attribute name="Built-By" value="${builder}" />
				<attribute name="Built-On" value="${build-info.current-date}" />
				<attribute name="Built-At" value="${build-info.current-time}" />
			</manifest>
		</war>
	</target>

	<target name="ftp" depends="" description="upload war file to server">
		<ftp 
			server="${ftp-server}" remotedir="${ftp-remotedir}"
			userid="${ftp-userid}" password="${ftp-password}"
			action="mkdir" verbose="yes">
		</ftp>
		<ftp 
			server="${ftp-server}" remotedir="${ftp-remotedir}"
			userid="${ftp-userid}" password="${ftp-password}"
			action="send" verbose="yes" depends="yes">
			<fileset file="${build-directory}/${war-file-name}" />
		</ftp>
	</target>

	<target name="mail-upload-complete">
		<mail from="ant@cakelycakes.com"
		      tolist="bigcakes@cakelycakes.com"
		      subject="${war-file-name} was uploaded to the server"
		      message="The ${war-file-name} file was uploaded to ${ftp-server} in ${ftp-remotedir}"/>
	</target>

	<target name="build-and-ftp" depends="war,ftp,mail-upload-complete" />
	<target name="build-and-deploy" depends="war,undeploy,deploy" />
	
</project>

The following build.properties file is referenced by the build.xml file.

build.properties

#build.properties file
project-name=tomcat-demo
builder=TeamCakes
ftp-server=FTP_SERVER_GOES_HERE
ftp-userid=FTP_USERID_GOES_HERE
ftp-password=FTP_PASSWORD_GOES_HERE
tomcat-manager-url=http://TOMCAT_SERVER_GOES_HERE/manager
tomcat-manager-username=TOMCAT_USERNAME_GOES_HERE
tomcat-manager-password=TOMCAT_PASSWORD_GOES_HERE

The project contains the build.xml and build.properties at the root level of the project.

tomcat-demo project

(Continued on page 2)

Page:    1 2 3 >