Java Reference
In-Depth Information
As we mentioned, Ant isn't so much a tool as a framework for running tools. You can
use property elements to set the parameters a tool needs and a task to run that tool. A
great number of tasks come bundled with Ant, but you can also write your own custom
tasks. For more about developing with Ant, we recommend reading Ant in Action 3 and
exploring the Ant website.
Listing 9.1 shows the start of the build file for the example project from chapter 3.
This segment of the build file sets the default target and the properties your targets
and tasks will use.
Listing 9.1
Start of an Ant build file
C
B
<project name="example" default="test">
<property file="build.properties"/>
<property name="src.dir" location="src"/>
<property name="src.java.dir" location="${src.dir}/java"/>
<property name="src.test.dir" location="${src.dir}/test"/>
<property name="target.dir" location="target"/>
<property name="target.classes.java.dir"
location="${target.dir}/classes/java"/>
<property name="target.classes.test.dir"
location="${target.dir}/classes/test"/>
[...]
The listing starts B by giving the project the name example and setting the default
target to test . The test target appears in listing 9.3.
Next, we include the build.properties file C . Because programmers may store JAR
files in different locations, it's a good practice to use a build.properties file to define
their locations. Many open source projects provide a sample build.properties file you
can copy and edit to match your environment.
We use the property Ant task to define the directories for production and test
source code D as well as the location of the directories for compiled production and
test code E . It's a good practice to define source and test directories in separate loca-
tions for both source and compiled code. In our example, we end up with four prop-
erty definitions. Splitting out compiled production and test code makes it easy to
build other artifacts such as JAR files because all compiled production code is rooted
in its own directory.
An important aspect of Ant properties is that they're immutable. Once you set a
property value, you can't change it. If you try to redefine the value with another prop-
erty element, Ant ignores the request.
D
E
9.4.1
The javac task
For simple projects, running the javac compiler from the command line is easy
enough. For products with multiple packages and source directories, the configura-
tion of the javac compiler becomes more complicated. The javac Ant task allows a
3
http://manning.com/loughran/
 
 
 
 
 
Search WWH ::




Custom Search