Writing a Basic Buildfile
Problem
You want to write a basic Ant buildfile.
Solution
Example 3-1 lists a simple Ant buildfile that you may use as a boilerplate for your own projects.
Example 3-1. Boilerplate Ant buildfile
<?xml version="1.0"?> <project name="Template Buildfile" default="compile" basedir="."> <property name="dir.src" value="src"/> <property name="dir.build" value="build"/> <property name="dir.dist" value="dist"/> <!-- Creates the output directories --> <target name="prepare"> <mkdir dir="${dir.build}"/> <mkdir dir="${dir.dist}"/> </target> <target name="clean" description="Remove all generated files."> <delete dir="${dir.build}"/> <delete dir="${dir.dist}"/> </target> <target name="compile" depends="prepare" description="Compile all source code."> <javac srcdir="${dir.src}" destdir="${dir.build}"/> </target> <target name="jar" depends="compile" description="Generates oreilly.jar in the 'dist' directory."> <jar jarfile="${dir.dist}/oreilly.jar" basedir="${dir.build}"/> </target> </project>
Discussion
You generally call this file build.xml
, and can
put it anywhere you like. In our example, the buildfile is found in
the directory containing the src
directory. The
<project>
tag is
found in all buildfiles:
<project name="Template Buildfile" default="compile" basedir=".">
The project
name should be something descriptive,
as this may show up when you run Ant from other tools. The
default
attribute
specifies which target is invoked when the user types
ant
. Finally, the ...
Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.