如何使用Ant更改文件中的属性值?

如何使用Ant更改文件中的属性值?,ant,properties,Ant,Properties,输入示例: SERVER_NAME=server1 PROFILE_NAME=profile1 ... 示例输出: SERVER_NAME=server3 PROFILE_NAME=profile3 ... 此文件将在applicationContext.xml中使用。我试过了 <copy file="${web.dir}/jexamples.css_tpl" tofile="${web.dir}/jexamples.css" > <filterc

输入示例:

SERVER_NAME=server1
PROFILE_NAME=profile1
...
示例输出:

SERVER_NAME=server3
PROFILE_NAME=profile3
...
此文件将在
applicationContext.xml
中使用。我试过了

<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=@SERVER_NAME@
PROFILE_NAME=@PROFILE_NAME@
此代码(由您提供)


如果您希望保持原始文件的现有状态,一种方法是使用:


这将用
SERVER\u NAME=
替换以
SERVER\u NAME=server2
开头的每一行(与
PROFILE\u NAME=
相同)。这将返回您所描述的输出

[\t]*
将忽略空白。

Cleaner解决方案正在使用“propertyfile”ant任务-请参阅



是否可以在没有标记的情况下更改属性值@?如何将一个或多个参数传递给ant,我不想使用源文件来设置var和值,如您所示
SERVER\u NAME=server2
我希望在eclipse中运行ant构建文件时传递参数。@janwen:这是另一个问题,所以我们不应该搞乱这个问题。请问一个新问题(这个问题的链接可能有用)。这个解决方案对我来说非常有效。我想在
.properties
文件中实现这一点。伟大的解决方案。ThanksWARNING:如果密钥包含一些特殊字符,则会在其后面附加\这是一个问题
<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
<filterchain>
  <tokenfilter>
    <replaceregex pattern="^[ \t]*SERVER_NAME[ \t]*=.*$"
                  replace="SERVER_NAME=server2"/>
    <replaceregex pattern="^[ \t]*PROFILE_NAME[ \t]*=.*$"
                  replace="PROFILE_NAME=profi"/>
  </tokenfilter>
</filterchain>
<copy file="${web.dir}/jexamples.css_tpl"
     tofile="${web.dir}/jexamples.css" />
<propertyfile file="${web.dir}/jexamples.css">
    <entry key="SERVER_NAME" value="server2"/>
</propertyfile>