6. Writing Ant build File Create a file build.xml containing only one target: <?xml version="1.0"?> <project name="firstbuild" default="compile" > <target name="compile"> <javac srcdir="." /> <echo>compilation complete!</echo> </target> </project> It compiles all Java source code in and below the current directory "." according to Ant task <javac srcdir="." />
7.
8.
9.
10.
11.
12. Project Element Attribute Description name the name of the project. default * the default target to use when no target is supplied. basedir the base directory from which all path calculations are done.
13. Build File Example (Project) <project name="Sample Project" default="compile" basedir="."> <description> A sample build file for this project </description> </project>
14.
15. Target element Attribute Description name * the name of the target. depends a comma-separated list of names of targets on which this target depends. if the name of the property that must be set in order for this target to execute. unless name of the property that must not be set in order for this target to execute. description a short description of this target's function.
16. Build File Example (Target) <project name="Sample Project" default="compile" basedir="."> ... <!-- set up some directories used by this project --> <target name="init" description="setup project directories"> </target> <!-- Compile the java code in src dir into build dir --> <target name="compile" depends="init" description="compile java sources"> </target> <!-- Generate javadocs for current project into docs dir --> <target name="doc" depends="init" description="generate documentation"> </target> <!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name="clean" description="tidy up the workspace"> </target> </project>
17.
18.
19.
20. Build File Example (Properties) <project name="Sample Project" default="compile" basedir="."> <description> A sample build file for this project </description> <!-- global properties for this build file --> <property name="source.dir" location="src"/> <property name="build.dir" location="bin"/> <property name="doc.dir" location="doc"/> </project>
21.
22.
23.
24.
25. Completed Build File (1 of 3) <project name="Sample Project" default="compile" basedir="."> <description> A sample build file for this project </description> <!-- global properties for this build file --> <property name="source.dir" location="src"/> <property name="build.dir" location="bin"/> <property name="doc.dir" location="doc"/> <!-- set up some directories used by this project -->
26. Completed Build File (2 of 3) <!-- set up some directories used by this project --> <target name="init" description="setup project directories"> <mkdir dir="${build.dir}"/> <mkdir dir="${doc.dir}"/> </target> <!-- Compile the java code in ${src.dir} into ${build.dir} --> <target name="compile" depends="init" description="compile java sources"> <javac srcdir="${source.dir}" destdir="${build.dir}"/> </target>
27. Completed Build File (3 of 3) <!-- Generate javadocs for current project into ${doc.dir} --> <target name="doc" depends="init" description="generate documentation"> <javadoc sourcepath="${source.dir}" destdir="${doc.dir}"/> </target> <!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name="clean" description="tidy up the workspace"> <delete dir="${build.dir}"/> <delete dir="${doc.dir}"/> <delete> <fileset defaultexcludes="no" dir="${source.dir}" includes="**/*~"/> </delete> </target> </project>