Hi,

Jay SenSharma

Jay SenSharma

Defining properties in ANT build file. It is not recommended that we hardcode everything in our ANT build file. Because we might be running the Same Ant Build file in different environments.
Defining Properties inside our ant build is the best way to make our Ant Script more readable and modifiable. We can achieve it by tow ways:

Option- 1). by declaring properties and their values inside the Build file.
Option-2). by declaring properties and their values in a separate properties file.
=====================

Example: Declaring properties inside the Build file

“build.xml”

<project name=”Test Properties” default=”run” basedir=”.”>
<description>Testing ANT Properties</description>
<property name=”username”     value=”Jay SenSharma”/>
<property name=”user.password”   value=”WebLogicPassword”/>

<target name=”run”>
<echo message=”Value of Property username :  ${username}”/>
<echo message=”Value of Property user.password :  ${user.password}”/>

<echo message=”———-System Properties Below———-“/>
<echo message=”System Properties SystemUser: ${user.name}”/>
<echo message=”System Properties OS Version: ${os.version}”/>
<echo message=”System Properties OS Name : ${os.name}”/>
</target>
</project>
Step1). Just run the “setDomainEnv.cmd” or Set the ANT_HOME manually in a command Window (add %ANT_HOME%bin in the PATH environment variable as well)and then run the above ant script you will find the below output:

C:JavaTestANTPropertiesDemo>ant
Buildfile: build.xml

run:
[echo] Value of Property username :  Jay SenSharma
[echo] Value of Property user.password :  WebLogicPassword

[echo] ———-System Properties Below———-
[echo] System Properties SystemUser: JAYKUMARS
[echo] System Properties OS Version: 5.1
[echo] System Properties OS Name : Windows XP

BUILD SUCCESSFUL
Total time: 0 seconds

============================================
Example: using Separate Properties files for Ant Build

Step1). Create a Properties file “mytest.properties” and provide the properties Key-Value pairs in it.

“mytest.properties”

username=Jay SenSharma
user.password=WebLogicPassword
user.java.home=C:/bea103/jdk160_05

Step2). Modify your Ant Build file like:

“build.xml”

<project name=”Test Properties” default=”run” basedir=”.”>
<description>Testing ANT Properties</description>

<property file=”mytest.properties”/>

<target name=”run”>
<echo message=”Value of Property username :  ${username}”/>
<echo message=”Value of Property user.password :  ${user.password}”/>
<echo message=”Value of Property user.java.home :  ${user.java.home}”/>

<echo message=”———-System Properties Below———-“/>
<echo message=”System Properties SystemUser: ${user.name}”/>
<echo message=”System Properties OS Version: ${os.version}”/>
<echo message=”System Properties OS Name : ${os.name}”/>
</target>
</project>

Step3). Run the Ant Script …

C:JavaTestANTPropertiesDemo>ant
Buildfile: build.xml

run:
[echo] Value of Property username :  Jay SenSharma
[echo] Value of Property user.password :  WebLogicPassword
[echo] Value of Property user.java.home :  C:/bea103/jdk160_05
[echo] ———-System Properties Below———-
[echo] System Properties SystemUser: JAYKUMARS
[echo] System Properties OS Version: 5.1
[echo] System Properties OS Name : Windows XP

BUILD SUCCESSFUL
Total time: 0 seconds
Thanks
Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.