使用ANT替换xml文件中的属性

使用ANT替换xml文件中的属性,ant,replace,Ant,Replace,我正在尝试使用ANT脚本替换build.xml文件中的版本号 我尝试了各种方法,不断地搜索StackOverflow的答案,但无法得到准确的查询 这是我的xml文件: <?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.0"?> <project name="feature" default="main" basedir="."> <target name="init">

我正在尝试使用ANT脚本替换build.xml文件中的版本号

我尝试了各种方法,不断地搜索StackOverflow的答案,但无法得到准确的查询

这是我的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>

<project name="feature" default="main" basedir=".">
<target name="init">    
    <property name="Version" value="1.0.0.20120327"/>
</target>

    <target name="main" depends="init">
    <description>Main target</description>
</target>
</project>

主要目标
现在你可以看到版本已经是昨天的日期了。我需要用当前日期替换它

以下是我尝试过的:

<target name="replace">
    <tstamp >
    <format property="touch.time" pattern="yyyyMMdd"/>  
    </tstamp>   

<property name="Feature.dir" location="../feature" />

<!--Didnt Work-->       
 <copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"
filtering="yes" overwrite="yes">
<filterset>
    <filter token="Version" value="1.0.0.${touch.time}"/>
</filterset>
  </copy>

  <!--Didnt work

   <replacetoken><![CDATA[<property name="Version" value=""/>]]>  
   </replacetoken>  
   <replacevalue><![CDATA[<property name="Version"value="1.0.0.${touchtime}" />]]>
   </replacevalue>

   -->  


<!-- Didnt work 
    <copy file="${Feature.dir}/build.xml" tofile="${Feature.dir}/build1.xml" >
        <filterchain>
        <tokenfilter>
                <replaceregex pattern="^[ \t]*Version[ \t]*=.*$"
                              replace="Version=1.0.0.${touch.time}"/>
        </tokenfilter>
            </filterchain>
</copy>
-->
</target>

我会在
过滤器链中使用
replaceregex

例如:

<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"       
    filtering="yes" overwrite="yes">
    <filterchain>
        <tokenfilter>
            <replaceregex pattern="1.0.0.[0-9.]*" replace="1.0.0.${touch.time}"/>
        </tokenfilter>
    </filterchain>
</copy>

如果要替换该文件,请随意复制到临时文件并将其移回

<tempfile property="build.temp.file.name"/>
<copy file="${Feature.dir}\build.xml" tofile="${build.temp.file.name}" ... />
<move file="${build.temp.file.name}" tofile="${Feature.dir}\build.xml" />

工作正常,谢谢。但只有一个问题。如果我想对同一个文件本身进行此更改,该怎么办?我试过了:没用