ANT Build Files
Sample Build File :
<?xml version="1.0"?> <project name="Hello World" default="info"> <target name="info"> <echo>Hello World - Welcome to Apache Ant!</echo> </target> </project>
Output :
Open command prompt and navigate to the folder where we have put build.xml and type
ant [target name]
We have specified default target name that’s why we can simply use ant command for building project. See below steps -
C:\>ant Buildfile: C:\build.xml info: [echo] Hello World - Welcome to Apache Ant! BUILD SUCCESSFUL Total time: 0 seconds
The echo task in the above example is a trivial task that prints a message. In our example, it prints the Hello World message.
Structure of Build File :
- Build file require the project element and at least one target element.
- Build file should not contain any whitespace before the XML declaration.
- Build file generally present in the project’s base directory but it can be present anywhere in the system
The XML element project has three attributes :
Attributes | Description | Usage |
---|---|---|
name | The Name of the project | Optional |
default | The default target of the build script. A project may have any number of targets. default attribute specifies which target should be considered as the default | Mandatory |
basedir | Specifies the base directory for the project | Optional |
The XML element target has following attributes :
Attributes | Description | Usage |
---|---|---|
name | Specifies the name of target | Required |
depends | Specifies the list of targets separated by comma on which the current target depends on | Optional |
description | Specifies the short description of the target | Optional |
if | Specifies that the target would be executed based on condition | Optional |
unless | It adds the target to the dependency list of the specified Extension Point. An Extension Point is similar to a target but it does not have any tasks | Optional |
Examples :
Consider the following build.xml -
<project> <target name="clean" > .... </target> <target name="compile" > .... </target> <target name="package" depends="clean,compile"> .... </target> <target name="deploy" depends="package"> .... </target> </project>
Explanation :
- In the above example we have multiple targets in a build file that we wish to execute in particular sequence.
- Dependencies are denoted using the depends attribute
- In the example, depends attribute states that deploy target may have dependency on package target.
- Similarly package target have dependency on the compile and clean targets.