|
How do I package and use a customized task?
Author: Deron Eriksson
Description: This Ant tutorial shows how to package a custom Ant task into a jar file and reference that in a build.xml file
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
In another tutorial, I demonstrated custom-writing an AntSW task. I created an Ant 'var' task based on the com.cakes.ant.Variable class that could used as a variable in Ant, which is fairly useful since an Ant property can't be changed after it has been initially set. In this tutorial I'll package up that task and use it in the multiple-project build-main.xml file that I've been building up over the course of several tutorials. The structure of the 'teamcakes-ant-util' project from the last tutorial is shown below. It features a build.xml file and Variable class.
In the last Ant tutorial, we created the project's build.xml file to demonstrate the difference between our variable task and the Ant property element. In this tutorial, we'd like to package up our task. Since it's a JavaSW class, we can do this by packaging the class in a jarW file. In our 'ant-utilities' project that we created, we already have the capability of building jar files and versioned jar files. Since we've already done this work, let's reference the ant-utilies build-main.xml and build-main.properties files. We can accomplish that with the following: <property file="../ant-utilities/build-main.properties" /> <import file="../ant-utilities/build-main.xml" /> I also created the 'create-teamcakes-ant-util-versioned-jar' target that creates a 'build-version' directory for our jar file and then builds the jar file. The modified build.xml file is shown here: build.xml<?xml version="1.0" encoding="UTF-8"?> <project name="teamcakes-ant-util" default="" basedir="."> <property name="project-name" value="${ant.project.name}" /> <property name="major-version-number" value="1" /> <property file="../ant-utilities/build-main.properties" /> <import file="../ant-utilities/build-main.xml" /> <taskdef name="var" classname="com.cakes.ant.Variable" classpath="bin" /> <target name="property-test"> <property name="myProp" value="value1" /> <echo>myProp is ${myProp}</echo> <property name="myProp" value="value2" /> <echo>myProp is ${myProp}</echo> </target> <target name="variable-test"> <var name="myVar" value="value1" /> <echo>myVar is ${myVar}</echo> <var name="myVar" value="value2" /> <echo>myVar is ${myVar}</echo> </target> <target name="variable-test-2" depends="variable-test"> <echo>myVar is ${myVar}</echo> </target> <target name="create-teamcakes-ant-util-versioned-jar"> <mkdir dir="build-version" /> <antcall target="jar-version" /> </target> </project> The ant-utilities build-main.properties file is included below: ant-utilities build-main.properties#build-main.properties file project-name=ant-utilities 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=-- tomcat-home=/apache-tomcat-5.5.20
The ant-utilities build-main.xml file is included below: ant-utilities build-main.xml<?xml version="1.0" encoding="UTF-8"?> <project name="ant-utilities" default="war" basedir="."> <property file="build-main.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" /> <path id="project-classpath"> <fileset dir="web/WEB-INF/lib" includes="*.jar" /> <fileset dir="${tomcat-home}/bin" includes="*.jar" /> <fileset dir="${tomcat-home}/common/lib" includes="*.jar" /> <fileset dir="${tomcat-home}/server/lib" includes="*.jar" /> </path> <path id="jar-project-classpath"> <fileset dir="lib" includes="*.jar" /> <fileset dir="${tomcat-home}/bin" includes="*.jar" /> <fileset dir="${tomcat-home}/common/lib" includes="*.jar" /> <fileset dir="${tomcat-home}/server/lib" includes="*.jar" /> </path> <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="project-name-test"> <echo>hello, the project is: ${project-name}</echo> </target> <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="pscp" depends="" description="securely upload war file to server"> <echo message="uploading war file to server using pscp" /> <exec executable="pscp.exe"> <arg value="-v" /> <arg value="-pw" /> <arg value="${ftp-password}" /> <arg value="${build-directory}/${war-file-name}" /> <arg value="${ftp-userid}@${ftp-server}:${ftp-remotedir}/${war-file-name}" /> </exec> <echo message="done uploading war file to server using pscp" /> </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="clean"> <delete dir="bin" /> <mkdir dir="bin" /> </target> <target name="copy-non-java-files"> <copy todir="bin" includeemptydirs="false"> <fileset dir="src" excludes="**/*.java" /> </copy> </target> <target name="compile" depends="clean,copy-non-java-files"> <javac srcdir="src" destdir="bin" classpathref="project-classpath" /> </target> <target name="compile-jar-classes" depends="clean,copy-non-java-files"> <javac srcdir="src" destdir="bin" classpathref="jar-project-classpath" /> </target> <target name="clean-jar"> <delete dir="build" /> <mkdir dir="build" /> </target> <target name="jar"> <jar basedir="bin" destfile="build/${project-name}.jar"> <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> </jar> </target> <target name="build-jar"> <antcall target="compile-jar-classes" /> <antcall target="clean-jar" /> <antcall target="jar" /> </target> <target name="jar-version"> <buildnumber /> <property name="version-number" value="${major-version-number}.${build.number}" /> <jar basedir="bin" destfile="build-version/${project-name}-${version-number}.jar"> <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}" /> <attribute name="Implementation-Version" value="${version-number}" /> </manifest> </jar> </target> <target name="build-jar-version"> <antcall target="compile-jar-classes" /> <mkdir dir="build-version" /> <antcall target="jar-version" /> </target> <target name="build-and-deploy-from-server" depends="war,ftp,undeploy,deploy-from-server,mail-build-and-deploy" /> <target name="build-and-deploy-from-server (secure)" depends="war,pscp,undeploy,deploy-from-server,mail-build-and-deploy" /> <target name="kitchen-sink" depends="compile,war,pscp,undeploy,deploy-from-server,mail-build-and-deploy" /> </project> (Continued on page 2) |

