Ant从属性文件中替换令牌

Ben*_*out 7 ant

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

some test ${foo} other text ...
Run Code Online (Sandbox Code Playgroud)

标记包含在属性文件中,例如:

foo=1
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

mar*_*ton 4

这和这些有些相似。

属性是从文件加载的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>
Run Code Online (Sandbox Code Playgroud)