In previous tutorial, we have set several paths hardcoded in buildConfig configuration file. However Ant provides capability to either declare those values as properties in configuration file, load values from resource file or pass resource key-value pairs from command line. We will use same buildConfig file to modify it.
1.Declare Values as Properties in configuration file:
We have cleanLogs target in buildConfig file, which is receiving directory path to delete files within. Lets declare that path as property value in beginning of path, like below
<property name="log.dir" value="d:/logs"/>
and target definition would become
<target name="deleteLogs" depends="stopOracle"
description="Clean Logs directory">
<delete>
<fileset dir="${log.dir}">
<include name="*"/>
</fileset>
</delete>
</target>
Lets run the ant target

2. Read values from build.properties
Lets prepare a file named build.properties, which would host key value pairs. We have compileSource target, which receives several paths as hardcoded
<target name="compileSource">
<javac
srcdir="D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src"
destdir="D:/Learning/11JAVA XML/build/classes"/>
<jar destfile="D:/deployModule/lib/util.jar"
basedir="D:/Learning/11JAVA XML/build/classes"
includes="*"
>
</jar>
</target>
Lets move all paths in build.properties in form of key-value pairs.
src.dir=D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src
build.class=D:/Learning/11JAVA XML/build/classes
jar.file=D:/deployModule/lib/util.jar
build.properties has to be declared at beginning as property file like below
<property file="build.properties"/>
Target definition “compileSource” would become like below
<target name="compileSource">
<javac
srcdir="${src.dir}"
destdir="${build.class}"/>
<jar destfile="${jar.file}"
basedir="${build.class}"
includes="*"
>
</jar>
</target>

3.Property passed from command line :
We have target definition to start the oracle database service “startOracle”, wherein database instance service name is hardcoded.
<target name="startOracle"> <exec executable="cmd"> <arg value="/c"/> <arg value="net start OracleServiceORCLDB"/> </exec> </target>
Here we can configure such that we receive service name from command line. Hence target definition would become like this
<target name="startOracle">
<exec executable="cmd">
<arg value="/c"/>
<arg value="net start ${service.name}"/>
</exec>
</target>
Now we can pass service.name value as command line parameter like below
cmd>ant -buildfile buildConfig.xml -Dservice.name=OracleServiceORCLDB startOracle
Hence we will receive output as

Now we have completely modified file buildConfig.xml as below
<?xml version="1.0"?>
<project name="AntDemo" default="hello">
<property name="log.dir" value="d:/logs"/>
<property file="build.properties"/>
<target name="hello">
<echo>Hello World - Welcome to Apache Ant!</echo>
</target>
<target name="stopOracle">
<exec executable="cmd">
<arg value="/c"/>
<arg value="net stop ${service.name}"/>
</exec>
</target>
<target name="startOracle">
<exec executable="cmd">
<arg value="/c"/>
<arg value="net start ${service.name}"/>
</exec>
</target>
<target name="deleteLogs" depends="stopOracle"
description="Clean Logs directory">
<delete>
<fileset dir="${log.dir}">
<include name="*"/>
</fileset>
</delete>
</target>
<target name="compileSource">
<javac
srcdir="${src.dir}"
destdir="${build.class}"/>
<jar destfile="${jar.file}"
basedir="${build.class}"
includes="*"
>
</jar>
</target>
</project>
we have additional build.properties file below
src.dir=D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src
build.class=D:/Learning/11JAVA XML/build/classes
jar.file=D:/deployModule/lib/util.jar