Understanding the Sample Build File Ant allows you to define in XML how to build various targets within a project. A target has a name and may have one or more other targets as dependencies: <target name="rebuildAll" depends="clean,build" /> The above line defines a target named rebuildAll. When you execute this target (keep reading), Ant first ensures that the targets named clean and build have been executed. A target contains a list of commands, or tasks, to execute. The Ant software installation provides a manual describing a large number of tasks that will suffice for most of your needs. If you can't find an appropriate task, you can programmatically create your own. The clean target contains the single task named delete. In this example, the delete task tells Ant to delete a file system directory with the name provided in quotes. <target name="clean"> <delete dir="${build.dir}" /> </target> Ant allows you to define properties that provide a construct similar to constants in Java. When the delete task executes, Ant will replace ${build.dir} with the value of the property named build.dir. The property named build.dir is declared in the agileJava Ant script as: <property name="build.dir" value="${basedir}\classes" /> This declaration sets the value of build.dir to ${basedir}\classes. The use of ${basedir} is in turn a reference to the property basedir, defined in the project element for the agileJava Ant script: <project name="agileJava" default="junitgui" basedir="."> (The . indicates that basedir is set to the current directorythe directory in which Ant is executed.) When executing Ant, you can specify a target: ant rebuildAll The default attribute in the project element indicates the target to execute if none is specified. In this example, the junitgui target is the default and gets run if you execute ant with no arguments: ant A list of targets can be obtained by executing: ant -projecthelp This will show main targetsthose targets that specify a description attribute. Using some built-in smarts, Ant executes tasks only when necessary. For example, if you execute the junitgui target, Ant will only run javac compiles against source that has not changed since the last time you executed junitgui. It uses the timestamps of the class files to make this determination. To summarize the agileJava project, there are three main targets: build, junitgui, and rebuildAll. There are two subtargets, init and clean. The build target depends on the init target, which ensures that the build output directory (./classes) exists. The build target compiles all sources in the source directory (./source) to the build output directory, using Ant's built-in javac task. The javac task specifies a number of attributes, including the attribute classpath, specified as a nested element of the javac task. The classpath attribute references a path element by the name classpath; this path element includes the JUnit jar file and the classes directory. The junitgui target depends on the build target. If the build target succeeds, the junitgui target executes the JUnit GUI using the Java VM, passing in AllTests as the parameter.[7] The rebuildAll target depends on execution of the clean target, which removes the build output directory, and on the build target. Refer to the Ant manual for more detailed information. There are also several books available on Ant. One very comprehensive book is Java Development with Ant.[8] |