Developing Web Applications With Ant by Richard Hightower - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

The Hello World application project build file.

One of the differences in the application project build file is the way that it compiles the Java source: <target name="compile" depends="prepare"
description="compile the Java source.">
<javac srcdir="./src" destdir="${build}">
<classpath >

<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>

</classpath>
</javac>
</target>

Notice that the compile target specifies all the JAR files in the common lib directory (<include name="**/*.jar"/>). The greetmodel.jar file is in the common lib directory, so it is included when the javac task compiles the source. Another difference is the way the application project build file packages the Ant source:

<target name="package" depends="compile"
description="package the Java classes into a jar."> <jar jarfile="${app_jar}"
manifest="./META-INF/MANIFEST.MF"
basedir="${build}" />
</target>

Notice that the package target uses the jar task as before, but the jar task's manifest is set to the manifest file described earlier. This is unlike the model project build file, which did not specify a manifest file; the model used the default manifest file. The application project build file's manifest file has the entries that allow us to execute the JAR file from the command line.

In order to run the Hello World Java application, after we run the application project build file we go to the output common lib directory (tmp/app/lib) and run Java from the command line with the -jar command-line argument:

$ java -jar greetapp.jar
Hello World!

You may wonder how the application loaded the Greeting interface and the GreetingFactory class. This is possible because the manifest entry Class-Path causes the JVM to search for any directory or JAR file that is specified (refer to the JAR file specification included with the Java Platform documentation for more detail). The list of items (directory or JAR files) specified on the Class-Path manifest entry is a relative URI list. Because the greetmodel.jar file is in the same directory (such as /tmp/app/lib) and it is specified on the Class-Path manifest, the JVM finds the classes in greetmodel.jar.

One issue with the application project is its dependence on the model project. The model project must be executed before the application project. How can we manage this? The next section proposes one way to manage the situation with an Ant build file.