Thursday, February 18, 2010

Reading a config XML file from Ant Build

      Yesterday I was breaking my head to figure out how can I access the properties defined in and XML file. We used to define a config.properies file and access this property file in ANT. But these days writing configuration file in XML is useful we need to communicate with other applications.

       I was working on developing a SeleniumTestFrameWork in java, so I defined an XML file "testconfig.xml" to store my properties for the test framework. But for the ANT build to execute the test cases, I need to get the property of the testNG group "testgroup" which is defiend in the testconfig.xml file. Finally I found the solution which as simple as accessing config.properties file in ANT.

Here the example and sample build file for it.

Sample XML File: samplconfig.xml

<root>
<parent1>
<child1>child value </child1>
</parent1>
<parent2>parent2 value</parent2>
<parent3>parent3 value</parent3>
<root>

Now Here the ANT Build is,

<project basedir="." name="XML Property Test">
<xmlproperty collapseattributes="true" file="${basedir}/config/samplconfig.xml">
<target name="Sample xml properties">
<echo>Parent2 Value is: ${root.parent2} </echo>
<echo>chile1 Value is: ${root.parent1.child1} </echo>
</target>
</project>

When you run the build the output will be like this...

Buildfile: E:\workspace\mytest\build.xml
Sample xml properties:
[echo] Parent2 Value is: Parent2 value
BUILD SUCCESSFUL
Total time: 141 milliseconds

Now start defining your property files as XMLs, and start enjoying.... :)