Ant - Property Task


Ant build files are written in XML, which does not cater for declaring variables as you do in your favourite programming language. However, as you may have imagined, it would be useful if Ant allowed declaring variables such as project name, project source directory etc.

Ant uses the property element which allows you to specify properties. This allows the properties to be changed from one build to another. or from one environment to another.

By default, Ant provides the following pre-defined properties that can be used in the build file

ant.fileThe full location of the build file.
ant.versionThe version of the Apache Ant installation.
basedirThe basedir of the build, as specified in the basedir attribute of the project element. version of the JDK that is used by Ant.
ant.project.nameThe name of the project, as specified in the name atrribute of the project element
ant.project.default-targetThe default target of the current project
ant.project.invoked-targetsComma separated list of the targets that were invoked in the current project
ant.core.libThe full location of the ant jar file
ant.homeThe home directory of Ant installation
ant.library.dirThe home directory for Ant library files - typically ANT_HOME/lib folder.

Ant also makes the system properties (Example: file.separator) available to the build file.

In addition to the above, the user can define additional properties using the property element. An example is presented below which shows how to define a property called sitename:

<?xml version="1.0"?>
<project name="Hello World Project" default="info">
  <property name="sitename" value=""/>
   <target name="info">
	  <echo>Apache Ant version is ${ant.version} - You are 
	  at ${sitename} </echo>

Running ant on the above build file should produce the following output:

Buildfile: C:\build.xml

     [echo] Apache Ant version is Apache Ant(TM) version 1.8.2 
     compiled on December 20 2010 - You are at

Total time: 0 seconds