|
|
Sample build script
<project name="MyProject" default="dist" basedir=".">
<description> Project Description </description>
<!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/>
<target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target>
<target name="compile" depends="init" description="compile the source " > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target>
<target name="dist" depends="compile" description="generate the distribution" > <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/> </target>
<target name="clean" description="clean up" > <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>
Listing 2-1 can be broken down into the following sections:
-
Description: the <description> tag contains a description of your
project for your own use
-
Build script initialization: your build script will undoubtedly need
initialization parameters. In this case we define the directory location for
the source code, the directory where compiled source code is built, and the
directory where our final distributable files, such as JAR, WAR, and EAR
files, are to be stored. In addition to specifying locations, properties can
specify "value" attributes.
-
Init target: the "init" target is used to initialize the build environment.
In this case the build script obtains the timestamp when the build starts
(for consistent naming and labeling) and then creates the build directory.
-
Compile target: the "compile" target is dependent on the "init" target,
which means that the "init" target will be executed prior to starting the
"compile" target. It uses the <javac> Ant task to compile all source
files found in the source directory to the build directory. Later we will
refine this to include only the source files that we identify.
-
Dist target: the "dist" target is dependent on the "compile" target, which
means that both the "compile" and "init" targets will be executed prior to
the start of the "dist" target. The "dist" target creates a "lib" folder in
the distribution directory and then uses the <jar> Ant task to build a
JAR file in the distribution's lib folder containing all files in the build
directory. Again we will refine this later to include filters to only
archive specified files.
-
Clean target: each build script that you write should have a "clean" target
to cleanup then entire environment so that all source files will be compiled
and all archives will be rebuilt on the next build. In this case we simply
delete the build and distribution directories.
Ant can be considered a pluggable framework, and through the mechanisms of Ant
tasks, you can plug in additional functionality. The Ant manual can be
accessed online through the following website:
http://ant.apache.org/manual/
If you navigate to "Ant Tasks" and then choose "Overview of Ant Tasks" you
will see a list and a description of all Ant tasks. The left navigation pane
allows you to quickly navigate to the core tasks and to optional tasks. The
core tasks provide you with all that you need to build your projects while the
optional tasks include external tasks (functionality not built into Ant) such
as JUnit integration.
|