Hello World Main Project
The Hello World Java application depends on the existence of the Hello World model common library file. If we try to compile the application before the model, we get an error. The application requires the model, so we need a way to call the model project buildfile and the application project buildfile in the right order.
Creating a Master Buildfile
We can control the execution of two buildfiles by using a master buildfile. The master buildfile shown in the following listing and is located in the root directory of the Model 2 Hello World Example of the main project. This buildfile treats the model and application buildfile as subprojects (the model and application projects are the first of many subprojects that we want to fit into a larger project).
<project name="main" default="build" >
<target name="setProps" unless="setProps"
description="setup the properties.">
<property name="outdir" value="/tmp/app" />
<property name="setProps" value="true" />
</target>
<target name="init" depends="setProps"
description="initialize the properties.">
<property name="lib" value="${outdir}/lib" />
</target>
<target name="clean" depends="init"
description="clean up the output directories.">
<ant dir="./Model" target="clean">
<property name="outdir" value="${outdir}" />
<property name="setProps" value="true" />
</ant>
<ant dir="./Application" target="clean">
<property name="outdir" value="${outdir}" />
<property name="setProps" value="true" />
</ant>
<delete dir="${outdir}" />
</target>
<target name="prepare" depends="init"
description="prepare the output directory.">
<mkdir dir="${build}" />
<mkdir dir="${lib}" />
</target>
<target name="build" depends="prepare"
description="build the model and application modules.">
<ant dir="./model" target="package">
<property name="outdir" value="${outdir}" />
<property name="setProps" value="true" />
</ant>
<ant dir="./application" target="package">
<property name="outdir" value="${outdir}" />
<property name="setProps" value="true" />
</ant>
</target>
</project>
Analysis of the Master Buildfile
Notice that the main project buildfile simply delegates to the application and model subproject and ensures that the subprojects' buildfiles are called in the correct order. For example, when the clean target is executed, the main project's buildfile uses the ant task to call the model project's clean target. Then, the main project calls the application project's clean target using the ant task again. Both are demonstrated as follows:
<target name="clean" depends="init"
description="clean up the output directories.">
<ant dir="./Model" target="clean">
<property name="outdir" value="${outdir}" />
<property name="setProps" value="true" />
</ant>
<ant dir="./Application" target="clean">
<property name="outdir" value="${outdir}" />
<property name="setProps" value="true" />
</ant>
<delete dir="${outdir}" />
</target>
A similar strategy is used with the main project's build target. The build target calls the package target on both the model and application subprojects, as follows:
<target name="build" depends="prepare"
description="build the model and application modules.">
<ant dir="./model" target="package">
<property name="outdir" value="${outdir}" />
<property name="setProps" value="true" />
</ant>
<ant dir="./application" target="package">
<property name="outdir" value="${outdir}" />
<property name="setProps" value="true" />
</ant>
</target>
Thus, we can build both the application and model projects by running the main project. This may not seem like a big deal, but imagine a project with hundreds of subprojects that build thousands of components. Without a buildfile, such a project could become unmanageable. In fact, a project with just 10 to 20 components can benefit greatly from using nested buildfiles. We will use this same technique as we create the Web application in Chapter 6, the applet, and the EJB of this project. The master buildfile orchestrates the correct running order for all the subprojects. We could revisit this main project after we finish each additional subproject and update it. In the next section, we will discuss the applet buildfile.