ANT Property Files



Using ANT property file :

First question in our mind is that why we really need to use the property file if we already have a way to define and declare the properties in the build file -

  1. Suppose we are working on complex project which has descent number of properties then it is recommended to use separate property file
  2. Suppose we need to keep build file same on all environments such as DEV,PROD or Testing then we can use separate property file on each environment by keeping build file same.

Example Build File :

<?xml version="1.0"?>
<project name="Hello World Project" default="info">
   <property file="build.properties"/>
   <target name="info">
     <echo>${website}</echo>
     <echo>${year}</echo>
   </target>
</project>

Output :

After running ant on the above build file should produce the following output:

C:\example>ant
Buildfile: C:\example\build.xml
info:
     [echo] www.c4learn.com
     [echo] 2014
BUILD SUCCESSFUL
Total time: 0 seconds
C:\example>

Explanation :

  1. We use file attribute to define the name of the property file to be used.
  2. Above example shows that build.xml is associated with build.properties file
  3. Consider the content of the build.properties file -
    website=www.c4learn.com
    year=2014
  4. Each property will be represented by the name and value pair. In the above file we have defined two properties website and year