Pregunta

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

¿Fue útil?

Solución

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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top