在使用xml文件替换ANT财产 [英] Replace property in a xml file using ANT

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

问题描述

我试图使用Ant脚本替换build.xml文件的版本号。

我已经试过各种方法,搜索并重新搜索StackOverflow的答案,但无法得到确切的查询。

所以这里是我的xml文件:

 <?XML版本=1.0编码=UTF-8&GT?;
< Eclipse版本=3.0&GT?;<,项目名称=功能默认为主BASEDIR =>
<目标名称=初始化>
    <属性名=版本值=1.0.0.20120327/>
< /目标与GT;    <目标名称=主取决于=初始化>
    <描述>主要目标和LT; /描述>
< /目标与GT;
< /项目>

现在就像你看到的版本有昨天的日期。我需要用当前日期来替换它。

下面是我已经试过:

 <目标名称=替换>
    <&TSTAMP GT;
    <格式属性=touch.time模式=年月日/>
    < / TSTAMP><属性名=Feature.dir位置=../功能/><! - 没有工作 - >
 <复制文件=$ {Feature.dir} \\ build.xml文件TOFILE =$ {Feature.dir} \\ build1.xml
过滤=是覆盖=YES>
< filterset>
    <过滤器令牌=版本值=。1.0.0 $ {} touch.time/>
< / filterset>
  < /复制>  <! - 没有工作   < replacetoken><![CDATA [<属性名=版本值=/>]]>
   < / replacetoken>
   < replacevalue><![CDATA [<属性名=版本值=。1.0.0 $ {} touchtime/>]]>
   < / replacevalue>    - >
<! - 没有工作
    <拷贝文件=$ {} Feature.dir /build.xmlTOFILE =$ {} Feature.dir /build1.xml>
        < filterchain>
        < tokenfilter>
                < replaceregex模式=^ [\\ t] *版本[\\ t] * = * $。
                              取代=版本= 1.0.0 $ {} touch.time/>
        < / tokenfilter>
            < / filterchain>
< /复制>
- >
< /目标与GT;


解决方案

我会使用 replaceregex ​​ filterchain

例如:

 <复制文件=$ {Feature.dir} \\ build.xml文件TOFILE =$ {Feature.dir} \\ build1.xml
    过滤=是覆盖=YES>
    < filterchain>
        < tokenfilter>
            < replaceregex模式=1.0.0 [0-9] *代替=1.0.0 $ {} touch.time/>
        < / tokenfilter>
    < / filterchain>
< /复制>

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

 <临时文件属性=build.temp.file.name/>
<拷贝文件=$ {Feature.dir} \\ build.xml文件TOFILE =$ {} build.temp.file.name... />
<移动文件=$ {} build.temp.file.nameTOFILE =$ {Feature.dir} \\ build.xml文件/>

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

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

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.

Here is what I've tried:

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

解决方案

I would use replaceregex inside a filterchain.

For example:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆