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

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

问题描述

我想用 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

实际上,如果源文件中的标记类似于@@foo@@"或foo",这很容易,但我无法替换整个标记:${foo}

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

谢谢

推荐答案

这有点类似于 这些.

属性从文件 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>

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

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