Table of Contents
Ant is a Java tool that automates various tasks typically associated with the development, compilation, distribution, installation and cleaning.
Ant does what is dictated by a construction file (build file), written in XML format. By default, the file is called "build.xml".
Build.xml example:
<project name="MyProject" default="compile" basedir="."> <description> simple example build file </ description> <property name="src.dir" location="src"/> <property name="build.dir" location="build"/> <target name="init"> <mkdir dir="${build.dir}"/> </ Target> <target name="compile" depends="init" description="Compiles the source code"> <javac srcdir="${src.dir}" destdir="${build.dir}"/> </ Target> <target name="show" description="Shows property values"> <echo message="src.dir = ${src.dir}" /> <echo message="build.dir = ${build.dir}" /> </ Target> </ Project>
These are the targets you may call:
A project (project) defines several targets (targets) that perform tasks (tasks), parameterized by properties (properties).
A target (target) defines a set of tasks to be performed. The field 'depends' indicates the targets that must be executed before the current one. Each target is only executed once.
Dependencies of a target:
<target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C,B,A"/> ... > ant D
A task (task) is a procedure that can be run within a target. Each one has several specific attributes that act as arguments for its implementation. Some examples of basic tasks are included in the Ant: javac, java, mkdir, copy, replace, tstamp. There is also the possibility to extend Ant with new tasks.
A property (property) is a pair name value. When you refer ${name} in text, it is replaced by a value. For example, if the property 'dir' has the value 'c :/ proj', then '${dir}/classes' is replaced by 'c:/proj/classes'.
The properties can be defined in file construction, read from a file or set on the command line:
<property name="dir" value="c:/proj" /> ... <property file="build.properties" /> ... > Ant-DNAME = value target
The value of a property can be set only once. After that, the value can only be changed temporarily.
A directory structure typically associated with an Ant project looks as follows:
src config lib build dist
Example file full of construction:
<project name="MyProject" default="run" basedir="."> <description> simple example build file </ description> <property name="src.dir" location="src"/> <property name="build.dir" location="build"/> <property name="run.mainclass" value="pacote.Programa"/> <property name="run.args" value="etc\input_file.txt etc\output_file.txt"/> <target name="init"> <mkdir dir="${build.dir}"/> </ Target> <target name="clean" description="Delete the build directory"> <delete dir="${build.dir}"/> </ Target> <target name="compile" depends="init" description="Compiles the source code"> <javac srcdir="${src.dir}" destdir="${build.dir}"/> </ Target> <target name="run" depends="compile" description="Runs the program"> <java classname="${run.mainclass}" fork="true"> <arg line="${run.args}" /> <classpath> <pathelement location="${build.dir}"/> </ Classpath> </ Java> </ Target> <target name="show" description="Shows property values"> <echo message="src.dir = ${src.dir}" /> <echo message="build.dir = ${build.dir}" /> <echo message="run.mainclass = ${run.mainclass}" /> <echo message="run.args = ${run.args}" /> </ Target> </ Project>