本文介绍了使用 ANT 替换 xml 文件中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to replace a version number in a build.xml file using an ANT script.

我尝试了各种方法,搜索并重新搜索 StackOverflow 以获得答案,但无法获得确切的查询.

I've tried various approaches, searched and re-searched StackOverflow for answers but could not get the exact query.

这里是我的 xml 文件:

so here is my xml file:

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

现在你可以看到版本有昨天的日期.我需要用当前日期替换它.

Now as u can see the Version has yesterday's date. I need to replace it with the current date.

这是我尝试过的:

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

推荐答案

我会在 filterchain 中使用 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>

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

If you want to replace the file, feel free to copy to a temp file and move it back.

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

这篇关于使用 ANT 替换 xml 文件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:48