JaVa
   

Hello World Main Project

The Hello World Java app depends on the existence of the Hello World model common library file. If we try to compile the app before the model, we get an error. The app requires the model, so we need a way to call the model project buildfile and the app 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 app buildfile as subprojects (the model and app projects are the first of many subprojects that we want to fit into a larger project).

<project default="build" >
 <target unless="setProps" 
 description="setup the properties.">
 <property value="/tmp/app" /> <property value="true" />
 </target>
 <target depends="setProps" description="initialize the properties.">
 <property value="${outdir}/lib" />
 </target>
 <target depends="init" description="clean up the output directories.">
 <ant dir="./Model" target="clean">
 <property value="${outdir}" /> <property value="true" />
 </ant>
 <ant dir="./app" target="clean">
 <property value="${outdir}" /> <property value="true" />
 </ant>
 <delete dir="${outdir}" />
 </target>
 <target depends="init" description="prepare the output directory.">
 <mkdir dir="${build}" />
 <mkdir dir="${lib}" />
 </target>
 <target depends="prepare" description="build the model and app modules.">
 <ant dir="./model" target="package">
 <property value="${outdir}" /> <property value="true" />
 </ant>
 <ant dir="./app" target="package">
 <property value="${outdir}" /> <property value="true" />
 </ant>
 </target>
</project>


Analysis of the Master Buildfile

Notice that the main project buildfile simply delegates to the app 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 app project's clean target using the ant task again. Both are demonstrated as follows:

 <target depends="init" description="clean up the output directories.">
 <ant dir="./Model" target="clean">
 <property value="${outdir}" /> <property value="true" />
 </ant>
 <ant dir="./app" target="clean">
 <property value="${outdir}" /> <property 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 app subprojects, as follows:

 <target depends="prepare" description="build the model and app modules.">
 <ant dir="./model" target="package">
 <property value="${outdir}" /> <property value="true" />
 </ant>
 <ant dir="./app" target="package">
 <property value="${outdir}" /> <property value="true" />
 </ant>
 </target>


Thus, we can build both the app 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 app in , 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.


JaVa
   
Comments