Interview Questions

ANT - Load Properties file

In the previous article we have seen how to use Property in ANT build xml for compiling and deploying the Java application.

Now, let us see how to group all these properties in a file and load them in the ANT build xml in order deploy the Java application (which is discussed earlier in the previous article).

Create a build.properties file as given below. This properties file has all the property object needed for the ANT build script.

build.properties

sourceDir=src
classDir=classes
className=AntBuildTest

The properties file is loaded using the below syntax. It takes the property file name as the input in order to load the properties.

Syntax

<property file="build.properties" />

Sample build.xml

<!-- ANT Build.xml -->
<project name="My-First-Build-XML" default="run"  >
 
<!-- Load the Properties file -->
<property file="build.properties" />
       
<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>