How do I set up multiple projects to share the same Ant targets?
Author: Deron Eriksson
Description: This Ant tutorial describes how to let multiple projects share the same Ant targets using the import task.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20
(Continued from page 1) If I click on the 'kitchen sink' target, tomcat-demo gets cleaned, compiled, built, pscp'd (secure ftpW) to my host, and deployed, and I receive an email when this is completed. Buildfile: C:\projects\workspace\tomcat-demo\build.xml clean: [delete] Deleting directory C:\projects\workspace\tomcat-demo\bin [mkdir] Created dir: C:\projects\workspace\tomcat-demo\bin copy-non-java-files: [copy] Copying 1 file to C:\projects\workspace\tomcat-demo\bin compile: [javac] Compiling 1 source file to C:\projects\workspace\tomcat-demo\bin war: [delete] Deleting: C:\projects\workspace\tomcat-demo\build\tomcat-demo.war [war] Building war: C:\projects\workspace\tomcat-demo\build\tomcat-demo.war pscp: [echo] uploading war file to server using pscp [exec] Server version: SSH-2.0-OpenSSH_3.9p1 [exec] We claim version: SSH-2.0-PuTTY_Release_0.58 [exec] Using SSH protocol version 2 [exec] Doing Diffie-Hellman group exchange [exec] Doing Diffie-Hellman key exchange [exec] Host key fingerprint is: [exec] ssh-rsa 1024 --:--:--:--:--:--:--:--:--:--:--:--:--:--:--:-- [exec] Initialised AES-256 client->server encryption [exec] Initialised HMAC-SHA1 client->server MAC algorithm [exec] Initialised AES-256 server->client encryption [exec] Initialised HMAC-SHA1 server->client MAC algorithm [exec] Using username "---". [exec] Sent password [exec] Access granted [exec] Opened channel for session [exec] Started a shell/command [exec] Using SFTP [exec] Connected to ---.---.---.--- [exec] Sending file tomcat-demo.war, size=230510 [exec] Sent EOF message [exec] Server sent command exit status 0 [exec] All channels closed. Disconnecting [exec] Server closed network connection [exec] tomcat-demo.war | 4 kB | 4.0 kB/s | ETA: 00:00:55 | 1% tomcat-demo.war | 36 kB | 36.0 kB/s | ETA: 00:00:05 | 15% tomcat-demo.war | 136 kB | 68.0 kB/s | ETA: 00:00:01 | 60% tomcat-demo.war | 225 kB | 112.6 kB/s | ETA: 00:00:00 | 100% [echo] done uploading war file to server using pscp undeploy: [undeploy] OK - Undeployed application at context path /tomcat-demo deploy-from-server: [echo] deploying server war file [deploy] OK - Deployed application at context path /tomcat-demo [echo] done deploying mail-build-and-deploy: [mail] Sending email: tomcat-demo.war was uploaded to the server and deployed [mail] Sent email with 0 attachments kitchen-sink: BUILD SUCCESSFUL Total time: 12 seconds If I clicked on the 'kitchen sink' target for tomcat-demo-2, then tomcat-demo-2 would also get deployed, once again utilizing the targets available in the ant-utilities project. The ant-utilities build-main.properties and build-main.xml files are included below: 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 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> <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="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> Note that AntSW is extremely flexible, and there are other ways to set up the sharing of targets without using an import. One other common way is to use the 'ant' task, which lets you call Ant targets in other Ant build files. As shown below, you can specify the build file to reference using the 'antfile' attribute and you specify the target to reference using the 'target' attribute. The 'inheritAll' specifies to use the properties of this project to override any properties in the build file being called. <target name="say-hi"> <ant antfile="../ant-utilities/build-main.xml" target="project-name-test" inheritAll="true"> </ant> </target> I highly recommend the Ant manual at http://ant.apache.org/manual/. It is a great resource for finding out more about the various Ant tasks. Eclipse's built-in code completion (control-space) is also very handy. |