【问题标题】:Ant replace token from properties fileAnt 替换属性文件中的令牌
【发布时间】:2011-04-10 11:17:34
【问题描述】:

我想用 Ant 替换源文件中的标记:

some test ${foo} other text ...

令牌包含在属性文件中,例如:

foo=1

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

几年前我成功了,但这次我失败了......

谢谢

【问题讨论】:

  • 换句话说,做与 maven-resources-plugin 相同的工作,但使用 Ant 进行过滤
  • google "ant expandproperties" 这是过滤器链的一部分

标签: ant


【解决方案1】:

这有点类似于these

属性从文件properties.txt 加载。 这里的弱点是输入文本中所有出现的${ 都转换为{ 令牌替换之前 - 如果该字符串出现在文本的其他地方,这很可能会破坏事情。如果这是一个问题,仍然可以调整此解决方案。

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

【讨论】:

    猜你喜欢
    • 2011-05-29
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    相关资源
    最近更新 更多