1

I got a requirement to loop through some XML files, replace the environment specific values in it and create new set of XML files. The environment specific values are to be taken from property file. I am able to loop through a directory to read all the files and replace some specific value using xmltask as below.

    <target name="updateConfig" description="update the configuration" depends="init">      
<xmltask todir="${ConfigDestDirectory}" report="false" failwithoutmatch="true">
       <fileset dir="${ConfigSourceDirectory}">
            <include name="*.xml"/>    
       </fileset>               
      <replace path="/:application/:NVPairs/:NameValuePair[:name='Connections/HTTP/HostName']/:value/text()" withXml="localhost"/>          
        </xmltask>
     <echo>Replaced Successfully</echo>
    </target>

But I would like to read through a property file and get the path/value from it. I tried using property selector,property,var as different options for this case and manage to get the path but not the value. Below are the snippet of property file and the target that I am using.

#DEV.properties
HostName.xpath=/:application/:NVPairs/:NameValuePair[:name='Connections/HTTP/HostName']/:value/text()
HostName.value=localhost

<project name="TestBuild" default="ReadPropertyFile" basedir=".">
    <target name="init">        
            <property file="DEV.properties"/>
            <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="${xmltaskPath}"/>
            <taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${antcontribPath}"/>
            <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
        </target>

    <target name="ReadPropertyFile" description="update the configuration" depends="init">      
                <property file="DEV.properties" prefix="x"/>        
                <propertyselector property="propertyList" delimiter="," select="\0" match="([^\.]*)\.xpath" casesensitive="true" distinct="true"/>      
                <for list="${propertyList}" param="sequence">               
                <sequential>
                    <propertyregex property="destproperty" input="@{sequence}" regexp="([^\.]*)\." select="\1" />                   
                    <property name="tempname" value="${destproperty}.value" />      
                    <var name="localprop" value="${tempname}"/>
                    <echo> @{sequence} </echo>  
                    <echo> ${x.@{sequence}} </echo>
                    <echo>destproperty --> ${destproperty}</echo>
                    <echo>tempname --> ${tempname}</echo>
                    <echo> localprop --> ${localprop}</echo>    
                    <echo>${x.${localprop}} </echo> <!--This is not working -->
                </sequential>               
                </for>          
            </target> 

It would be really helpful if you guys can throw some light.

Thanks, Venkat

1 Answer 1

1

Would this work better ?

I think you got yourself confused with the "x." prefix.

<project name="TestBuild" default="ReadPropertyFile" basedir=".">
    <target name="init">        
        <property file="DEV.properties"/>
        <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="${xmltaskPath}"/>
        <taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${antcontribPath}"/>
        <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
    </target>

    <target name="ReadPropertyFile" description="update the configuration" depends="init">      
          <property file="DEV.properties" prefix="x"/>        
          <local name="propertyList"/>
          <propertyselector property="propertyList" delimiter="," select="\1" match="x\.([^\.]*)\.xpath" casesensitive="true" distinct="true"/>      
          <for list="${propertyList}" param="sequence">               
            <sequential>
                <echo> @{sequence} </echo>  
                <echo> @{sequence}.xpath = ${x.@{sequence}.xpath} </echo>
                <echo> @{sequence}.value = ${x.@{sequence}.value} </echo>
            </sequential>               
          </for>          
    </target> 
</project>
Sign up to request clarification or add additional context in comments.

3 Comments

I adjusted the script below to use select="\1" (select the first match) instead of select="\0" select the whole string).
Hi Patrice,Thanks a ton. Its working perfect. You are right, I got confused with prefix x and not so sure about the syntax. Also the local property was not working till I change the version of ant. I will proceed with this now.
Great! The <local is not really required, it's just cleaner if you have access to a version of ant that affords it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.