How do I use Ant to FTP a war file and deploy it to Tomcat?
Author: Deron Eriksson
Description: This Ant tutorial shows how to FTP a war file to a server and deploy it to Tomcat from the server.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20
The TomcatSW tasks that come with the catalina-ant.jar are very useful, but I ran into problems when trying to use the 'deploy' task for my 'java-tutorials' warW file, which is a fairly large war file. When running the 'deploy' task (that utilizes the Catalina DeployTask class), I would receive a 'java.net.SocketException: Unexpected end of file from server' error message: As you can imagine, I was annoyed by this and decided on a different strategy. Rather than trying to deploy the large war file from my local machine directly to the Tomcat Manager via AntSW (which gave that weird error), I decided a better strategy would be to FTPW the war file to the server and then to deploy to Tomcat from the war file on the server using the 'deploy' task. Most of the targets and tasks that I needed were already in my build.xml file. I just needed to create a 'deploy-from-server' target. The build.xml file is shown below. build.xml<?xml version="1.0" encoding="UTF-8"?> <project name="java-tutorials" 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" /> <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="deploy-from-server"> <echo>deploying server war file</echo> <sleep seconds="5"/> <deploy url="${tomcat-manager-url}" username="${tomcat-manager-username}" password="${tomcat-manager-password}" path="/${project-name}" localwar="file:/${server-home-directory}/uploaded-wars/${war-file-name}" /> <echo>done deploying</echo> </target> <target name="war"> <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-build-and-deploy"> <mail from="${email-from}" tolist="${email-to}" subject="${war-file-name} was uploaded to the server and deployed" message="The ${war-file-name} file was uploaded to ${ftp-server} in ${ftp-remotedir} and deployed to the /${project-name} context"/> </target> <target name="build-and-deploy-from-server" depends="war,ftp,undeploy,deploy-from-server,mail-build-and-deploy" /> </project> The following build.properties file is referenced by the build.xml file. build.properties#build.properties file project-name=java-tutorials builder=TeamCakes ftp-server=--- ftp-userid=--- ftp-password=--- tomcat-manager-url=http://---/manager tomcat-manager-username=--- tomcat-manager-password=--- server-home-directory=--- email-to=--- email-from=--- (Continued on page 2) |