ANT – Usage of <Property> task in Ant Build

Properties

In any project, there will be set of properties which can be set pre-configured so that the project can be customized. This is just like assigning a value to the key so that the key can be used every where in the project.

In ANT, this is achieved using property task. The property task takes "name" and "value" as attributes.

We can define a name in the property and assign the value so that it can be used anywhere in the Ant build. The value once set cannot be changed. It’s immutable.

For example, in order to store the path in a variable, we can define the property task as below,

<property name="source" location="/project/src" />

The above property "source" can be used in the build xml as ${source}. Now the value "/project/src" will be substituted in the place of ${source}.

Let us take an example of the below Java program.

AntBuildTest Java Class

public class AntBuildTest
{
    public static void main(String[] args)
    {
        System.out.println("My First Java Program deployed using ANT Property task");  
    }
}

Let us use the Property task in the Ant build xml and deploy the Java application.

<!-- ANT Build.xml -->
<project name="My-First-Build-XML" default="run"  >

<!-- Declare Ant Build Properties-->
        <!-- For storing the path -->
<property name="sourceDir" location="src" />
<property name="classDir" location="classes" />

        <!-- For storing the value -->
<property name="className" value="AntBuildTest" />
       
<target name="run">
        <!-- Create a temporary directory for storing the class file -->
        <mkdir dir="${classDir}"/>

        <!-- Compile the Java class -->
        <javac srcdir="${sourceDir}" destdir="classes"/>
       
        <!-- Run the Java class -->
        <java classname="${className}" classpath="classes"/>
</target>
       
</project>

Output

Buildfile: E:\proj_drup\workspace\Ant_Proj_1\Build.xml
run:
[javac] Compiling 1 source file to E:\proj_drup\workspace\Ant_Proj_1\classes
[java] My First Java Program deployed using ANT Property task
BUILD SUCCESSFUL
Total time: 1 second

Technology: 

Search