1

我需要遍历一些 XML 文件,替换其中的环境特定值并创建一组新的 XML 文件。环境特定值将从属性文件中获取。我可以遍历目录以读取所有文件并使用 xmltask 替换某些特定值,如下所示。

    <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>

但我想通读一个属性文件并从中获取路径/值。我尝试使用属性选择器、属性、变量作为这种情况下的不同选项,并设法获取路径而不是值。下面是属性文件的片段和我正在使用的目标。

#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> 

如果你们能发光,那将非常有帮助。

谢谢,文卡特

4

1 回答 1

1

这会更好吗?

我认为您对“x”感到困惑。字首。

<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>
于 2014-01-22T15:00:40.193 回答