Ant Properties

Jakob Jenkov
Last update: 2015-08-18

Ant properties are key, value pairs you can configure inside your Ant build script. Typically you use properties for values that you need to refer to multiple times in your Ant build script. For instance, the source directory of your Java code, or the build output directory.

Defining An Ant Property

You define an Ant property using the property element. Here is an Ant property example:

<project name="MyProject">
    <property name="dir.src" value="src"/>
</project>

Notice the property element inside the project element. The property element runs the Ant property task which sets a global property that can be referenced throughout the Ant build script.

The name attribute of the Ant property element contains the name of the property. The value attribute contains the value of the Ant property.

You can also specify the value of the Ant property inside the body of the property element. Here is another Ant property example showing that method:

<property name="dir.src">src</property>

Which version you use is up to you.

Referencing Ant Properties

You can reference an Ant property using the ${propertyName} code. The propertyName part of the code should be the name of the Ant property you want to reference. The value of that Ant property will be inserted at the place of the ${...} code. Here is an Ant property reference example:

<project name="MyProject">
    <property name="dir.build" value="build"/>
    <property name="dir.build.classes" value="${dir.build}/classes"/>
</project>

This example first defines a property named dir.build which has its value set to build. Imagine that build is the name of the root build output directory used by this Ant build script.

Second, this example defines a property named dir.build.classes which has its value set to ${dir.build}/classes . When Ant defines this property it will resolve the value of the referenced property dir.build, and insert its value where the property is reference. Thus, the second Ant property defined will have its final value set to build/classes because build is the value of the dir.build property referenced.

Ant Properties are Immutable

Once an Ant property has been defined it cannot be changed (as a general rule of thumb). Ant properties are immutable so to speak.

Ant Properties Are Global

Once an Ant property has been defined it is accessible throughout the Ant build script. An Ant property is global so to speak.

Where To Put The Ant Properties

In the example shown earlier the property was defined inside the project element, as an immediate child of the project element (at the "top level" in the build script). This makes the Ant property be defined for all targets in the build script.

You can also put property definitions inside a target definition. Then the properties will only be defined if that target is executed. Here is an example of placing Ant properties inside an Ant target:

<project name="MyProject">
    
  <target name="init">
    <property name="dir.build" value="build"/>
    <property name="dir.build.classes" value="${dir.build}/classes"/>
  </target>

</project>

This example first defines a target named init. Inside this target two properties are defined. Only if the init target is executed will these properties be defined. Even if defined inside a target, these Ant properties will be available globally in the build script once created.

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next