Using Property files in Ant and Property Task

Ant build file is a XML file so we can declare any variable as we could declare on our any programming language but we can use property element as declaring some value to parameter.

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

  • ant.file-> The full location of the build file.
  • ant.version->  The version of the Apache Ant installation.
  • basedir->  The basedir of the build, as specified in the basedir attribute of the project element.
  • ant.java.version->  The version of the JDK that is used by Ant.
  • ant.project.name->  The name of the project, as specified in the name attribute of the project element.
  • ant.project.default-target->  The default target of the current project.
  • ant.project.invoked-targets->  Comma separated list of the targets that were invoked in the current project.
  • ant.core.lib->  The full location of the Ant jar file.
  • ant.home->  The home directory of Ant installation.
  • ant.library.dir->  The home directory for Ant library files – typically ANT_HOME/lib folder.

Example-

Declaring some property element in the xml and using it. 

Like we declaring name of tutor.

<?xml version="1.0"?>
   <project name="Hello World" default="info" basedir=".">
   <property name="tutor" value="Dinesh Rajput"/>
   <target name="info">
      <echo>Hello World - Welcome to Apache Ant Tutorial by Tutor ${tutor}!!!</echo>
   </target>
   
</project>

Running Ant on the above build file produces the following output:

Ant Proprty Task


Using External Property into ANT Build-

We can also use external property file and use it into ANT build file. It allows you to reuse the same build file, with different property settings for different execution environment. For example, build properties file can be maintained separately for DEV, TEST, and PROD environments.

Here we are creating the property file is named build.properties and is placed along-side the build.xml file. You could create multiple build properties files based on the deployment environments – such as build.properties.dev and build.properties.test.

The following example shows a build.xml file and its associated build.properties file:
build.xml

<?xml version="1.0"?>
   <project name="Hello World" default="info" basedir=".">
   <property file="build.properties"/>
   <target name="info">
      <echo>Hello World - Welcome to Apache Ant Tutorial by Tutor ${tutor}!!!</echo>
   </target>
   
</project>

build.properties

tutor=Dinesh Rajput

Running Ant on the above build file produces the following output:

Ant Proprty Task

Output is similar as above.

 

Previous
Next