| Table of Contents |
You should define a number of standard targets for Ant to divide the application into logical units. Each target should execute the set of tasks required to carry out a logical unit of work. The build process contains the init, dependent, compile, unittest, filter, and jar targets.
The init target contains all the property definitions, creating a group of all the declarations and keeping the tasks at the same level. Although you can place a property task as an upper-level attribute and not as part of the target, placing the init target block at the top of the build script helps evaluate the definitions when debugging the script.
The dependent target fails the build if one of the dependents is missing. The available tasks in the init target set a common [component name].dependencies property. If any dependency is missing, the [component name].dependencies property is not set. This results in the execution of the dependent target. The only task available in the dependent target is <fail>.
The compile target compiles source files to create class files. The compile target also creates the ${[component name].classes} subdirectory before compilation. After completing the compilation, the target copies properties files from the source tree to the class subdirectory. The following code shows a compile target:
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}"
destdir="${build.dir.classes}"
classpath="${classpath}"/>
</target>
The unittest target activates the execution of all relevant unit tests. The unit test for a component, such as channel, involves a number of running processes, such as Java Messaging Services (JMS) processes, which support the messaging layer. If you start a process, the build script blocks the unit test until the process finishes. The test depends on the JMS router running. You need a technique that allows you to execute both the router and the unit test simultaneously. You can allow the execution of both processes in parallel using the <parallel> and <sequential> tasks. These container tasks allow you to run sets of tasks in parallel or in sequence. Each task nested within a parallel task is executed simultaneously.
Listing 5-1 shows a parallel and a sequence task:
<parallel> <antcall target="startserver"/> <sequential> <sleep seconds="5"/> <antcall target="testserver"/> <antcall target="stopserver"/> </sequential> </parallel>
The above listing executes the server and tests the tasks running simultaneously. The <sleep> task precedes the unit test. The <sleep> task completes the initialization sequence before the unit test begins.
| Note |
You can use the <parallel> task to execute both the router and the unit test simultaneously. Executing the router and the unit test simultaneously creates a race condition. If the test executes before the router has completed its initialization process, the test fails. |
The filter target eliminates the development artifacts from the final product tree. For example, you can use the filter target to eliminate unit tests before you JAR the classes. A typical application that is developed using Ant defines various properties that contain filters. The <delete> task contained in the filter target uses these filters.
The jar target creates JAR files. These files contain all the Java-related artifacts, such as class files and property files, needed by the application. If you package a client/server application, it is advisable to package the client classes separately from the server classes. If the compilation process creates the classes in one directory structure, you can use filters to direct the classes to different JAR files.
In addition to the standard targets, you can add optional targets, such as clean, javadoc, all, checkin, and checkout, to the build script.
clean target: Removes all the generated artifacts from the development directories. The artifacts include the directory that contains the class files, any JAR, Web Archive (WAR), or Enterprise Archive (EAR) files, and any generated Javadoc target.
javadoc target: Generates API documentation in HTML format from comments in source code.
all target: Combines the other optional targets using the depends attribute. This attribute specifies the targets defined to clean, compile, test, filter, and JAR the classes of the application. The following code shows the all target:
<target name="ans" depends="init,clean,compile,unittest,filter,jar"/>
checkout target: Places the set of source files and other artifacts that the product depends on in the appropriate location in the project directory structure.
checkin target: Records all the modifications in the source code.
| Table of Contents |