Java Reference
In-Depth Information
build file to invoke the compiler so you no longer need to rely on the command
line. Ant provides a task for almost all build-related jobs that you'd otherwise do on
the command line, including dealing with repositories like
CVS
. You can also find
tasks not defined by Ant itself that deal with just about anything. We mentioned
CVS
, but if you're using Subversion for revision control, you can use SvnAnt
4
out of
the Subclipse project.
A standard practice is to create
compile
targets in your build file to invoke the Ant
javac
task to compile production and source code. The
javac
task lets you set all com-
piler options, such as the source and destination directories, through task attributes.
Listing 9.2 shows the production and test compile targets that call the Java compiler.
Listing 9.2
The build file compile targets
B
<target name="compile.java">
<mkdir dir="${target.classes.java.dir}"/>
<javac destdir="${target.classes.java.dir}">
<src path="${src.java.dir}"/>
</javac>
</target>
C
D
E
<target name="compile.test" depends="compile.java">
<mkdir dir="${target.classes.test.dir}"/>
<javac destdir="${target.classes.test.dir}">
<src path="${src.test.dir}"/>
<classpath>
<pathelement location="${target.classes.java.dir}"/>
</classpath>
</javac>
</target>
F
G
H
<target name="compile" depends="compile.java, compile.test"/>
First, we declare the target
compile.java
B
to make sure that the destination direc-
tory exists
C
and compile the production sources
D
. Ant resolves the property
target.classes.java.dir
set at the top of the build file (in listing 9.1) and inserts
it in place of
${target.classes.java.dir}
. The
mkdir
task creates a directory
C
,
but if it already exists, Ant continues quietly. The build then calls the Java compiler
with
javac
, giving it the destination
D
and source
E
directories.
The
compile.test
target has a dependency on the
compile.java
target, spec-
ified with the
depends
attribute
F
(
depends="compile.java"
). When you call
the
compile.test
target, if Ant hasn't called the
compile.java
target yet, it does
so immediately.
You may have noticed that you don't explicitly add the
JU
nit
JAR
to the classpath.
Remember that when you installed Ant, you copied the
JU
nit
JAR
file to the
${
ANT_HOME
}/lib directory in order to use the
junit
Ant task. Because junit.jar is
4