Home   Notes    Contact Me

Ant

Local

External


Zipping files

<project> <target name="Makezip" description="Creates a zip of certain files"> <zip zipfile="zipfilename.zip"> <fileset dir="dirToZip"> <include name="**/*"/> </fileset> </zip> </target> </project>
<target name="Makezip" description="Creates a zip of certain files"> <zip zipfile="zipped.zip" basedir="test/bin"> <include name="prog.exe"/> <include name="d1.dll"/> <include name="d2.dll"/> </zip> </target>

Ant, calling another ant build

<!-- runs the target deploy in the ant file build.xml located in the sub dir Server --> <target name="deploy"> <ant dir="Server" antfile="build.xml" target="deploy"/> </target>

Properties, From the Command Line

# command line: ant -DmyProp=fred # target in build.xml: <target name="show myProp"> <echo message="myProp is ${myProp}" /> </target>

Replace text during copy

<copy file="proj/test.sln" tofile="proj/test2008.sln"> <filterset begintoken="." endtoken=','> <filter token='vcproj"' value='2008.vcproj"'/> </filterset> </copy>

delete

# Delete a directory and all files in it # delete the directory './target': <delete dir="./target" />

Environment Variables

<!-- To be able to use environment vars you must have the following line in your build.xml file: --> <property environment="env" /> <target name="showenv"> <echo message="Username is ${env.USERNAME}" /> </target>

OS specific builds

<condition property="is.windows" value="Yup I am Windows"> <os family="windows"/> </condition> <target name="say" depends="say-windows,says-unix"> </target> <target name="say-windows" if="is.windows"> <echo message="I am windows" /> </target> <target name="say-unix" unless="is.windows"> <echo message="I am unix" /> </target>

Conditional, dependant on 2 properties

<target name="solaris-release-only" unless="is.windows" if="env.ARCH_MODE"> <fail message="For Release builds under Solaris, ARCH_MODE must not be set (it is currently ${env.ARCH_MODE})."/> </target> <target name="solaris-debug-only" unless="is.windows"> <antcall target="solaris-debug-only-Known-Not-To-Be-Windows"/> </target>

Copy

<!-- copy and/or rename a file --> <copy file="../origfile.txt" tofile="./out/outfilename.log" /> <!-- copy to a directory (no renaming --> <copy file="../origfile.txt" todir="./out" /> <!-- copy a directory --> <copy todir="${tomcat-webappdir}/WEB-INF"> <fileset dir="./directoryToCopyFrom/WEB-INF"/> </copy>

Exectuting external programs

<exec executable="cmd.exe" dir="./pathToBeIn" os="Windows 2000,Windows 2003,Windows XP,Windows Vista" failonerror="true"> <arg value="/c mvn eclipse:eclipse"/> </exec>

Properties, Built-in

user.dir Directory you were in when you ran ANT

Running it

java -cp ant.jar org.apache.tools.ant.Main [options] [target]