蚂蚁从属性文件替换令牌 [英] Ant replace token from properties file

查看:118
本文介绍了蚂蚁从属性文件替换令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Ant来代替在源文件中的标记:

i would like to replace tokens in source files with Ant:

some test ${foo} other text ...

令牌都包含在一个属性文件如:

Tokens are contained in a properties file eg.:

foo=1

其实这是容易的,如果在源文件中的令牌就像'@@富@@'或'富',但我不能代替整个令牌:$ {}富

Actually this is easy if tokens in source files were like '@@foo@@' or 'foo' but i'm not able to replace whole token : ${foo}

我已经成功年前,但这个时候,我已经失败了...

I've succeed years ago but this time i've failed ...

感谢

推荐答案

这是有点类似<一个href=\"http://stackoverflow.com/questions/1232532/simulating-the-maven2-filter-mechanism-using-ant\">these.

属性从文件的properties.txt 加载。
这里的弱点是,在您输入文本的 $ {所有出现被转换为 {令牌替换 - 如果该字符串出现在文本其他地方这很可能打破东西。如果这是一个问题,它仍然奥特可以适应该溶液中。

Properties are loaded from the file properties.txt. The weakness here is that all occurrences of ${ in your input text are converted to { before the token substitution - which may well break things if that string appears elsewhere in the text. If that is a problem, it still aught to be possible to adapt this solution.

<copy file="input.txt" tofile="output.txt">
    <filterchain>
    <replaceregex pattern="\$\{" replace="{" />
    <filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
            <param type="propertiesfile" value="properties.txt"/>
            <param type="tokenchar" name="begintoken" value="{"/>
            <param type="tokenchar" name="endtoken" value="}"/>
    </filterreader>
    </filterchain>
</copy>

这篇关于蚂蚁从属性文件替换令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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