Ant Custom Properties

Setting properties in the build file is the first method of providing custom properties with element in an ant build file.

Ant Custom Properties

Ant Custom Properties

     

 Setting properties in the build file is the first method of providing custom properties with <property> element in an ant build file. Unlike the <project> and <target> elements, the <property> element is defined as a task. This means that you can include <property> elements inside a target depending on certain conditions or depending on which target has been selected. You can also set properties at the beginning of a build file so that they apply to the entire build file. This means that you can set important constant values in a central location so that they are easy to find and change. You should remember that properties set inside a target override any properties set at the project level. Naming again comes into this and you should consider whether your target level properties should be identified as such by using a prefix to avoid confusion and possible namespaces clashes.

The simplest and most obvious use of the <property> task is to set a property using a name value pair, as shown below. 

You can set the value of a property to the value of another property. This can be useful if you will be referencing a verbose built-in property multiple times. This is as simple as placing a property marker in the value attribute of a <property> task as shown below in source code:

build.xml:

<project name="Properties" default="custom.echo" basedir=".">

  <property name="custom.value" value="1.0"/>
  <property name="fileseperator" value="${file.separator}"/>
  <property name="pathseperator" value="${path.separator}"/>
  
  <target name="custom">
  <echo message="custom.value = ${custom.value}"/>
  </target>

  <target name="custom.echo" depends="custom">
  <echo message="File: ${basedir}${fileseperator}build.xml"/>
  <echo message="Path: ${basedir}${fileseperator}build.xml${pathseperator}${basedir}
   ${fileseperator}build.properties"/>
  </target>

</project>


Run this program on the appropriate path, then the following output will be displayed.

Download Source Code