如何使用 Ant 更改文件中的属性值? [英] How can I change property values in a file using Ant?

查看:31
本文介绍了如何使用 Ant 更改文件中的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例输入:

SERVER_NAME=server1
PROFILE_NAME=profile1
...

示例输出:

SERVER_NAME=server3
PROFILE_NAME=profile3
...

此文件将在 applicationContext.xml 中使用.我试过了

This file will use in applicationContext.xml. I've tried

<copy file="${web.dir}/jexamples.css_tpl"
         tofile="${web.dir}/jexamples.css" >
    <filterchain>
       <replacetokens>
            <token key="SERVER_NAME" value="server2"/>
            <token key="PROFILE_NAME" value="profi"/>

        </replacetokens>
    </filterchain>
</copy>

但它不起作用.

推荐答案

你的 filterchain 没问题,但你的源文件应该是这样的:

Your filterchain is ok, but your source file should look like this:

SERVER_NAME=@SERVER_NAME@
PROFILE_NAME=@PROFILE_NAME@

此代码(由您提供)

<copy file="${web.dir}/jexamples.css_tpl"
         tofile="${web.dir}/jexamples.css" >
    <filterchain>
       <replacetokens>
            <token key="SERVER_NAME" value="server2"/>
            <token key="PROFILE_NAME" value="profi"/>
        </replacetokens>
    </filterchain>
</copy>

替换令牌并给你

SERVER_NAME=server2
PROFILE_NAME=profi

<小时>

如果你想保留你现在拥有的原始文件,一种方法是使用 replaceregex:

<filterchain>
  <tokenfilter>
    <replaceregex pattern="^[ \t]*SERVER_NAME[ \t]*=.*$"
                  replace="SERVER_NAME=server2"/>
    <replaceregex pattern="^[ \t]*PROFILE_NAME[ \t]*=.*$"
                  replace="PROFILE_NAME=profi"/>
  </tokenfilter>
</filterchain>

这会将以 SERVER_NAME= 开头的每一行替换为 SERVER_NAME=server2(对于 PROFILE_NAME= 也是如此).这将返回您所描述的输出.

This would replace every line starting with SERVER_NAME= by SERVER_NAME=server2 (same for PROFILE_NAME=). This will return get you the output you described.

[ \t]* 是忽略空格.

这篇关于如何使用 Ant 更改文件中的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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