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

And*_*rew 10 ant properties

输入示例:

SERVER_NAME=server1
PROFILE_NAME=profile1
...
Run Code Online (Sandbox Code Playgroud)

示例输出:

SERVER_NAME=server3
PROFILE_NAME=profile3
...
Run Code Online (Sandbox Code Playgroud)

这个文件将用于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>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

Pet*_*ang 15

filterchain还可以,但你的源文件应如下所示:

SERVER_NAME=@SERVER_NAME@
PROFILE_NAME=@PROFILE_NAME@
Run Code Online (Sandbox Code Playgroud)

此代码(由您提供)

<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>
Run Code Online (Sandbox Code Playgroud)

替换令牌并给你

SERVER_NAME=server2
PROFILE_NAME=profi
Run Code Online (Sandbox Code Playgroud)

如果您想保留原始文件,可以使用以下方法replaceregex:

<filterchain>
  <tokenfilter>
    <replaceregex pattern="^[ \t]*SERVER_NAME[ \t]*=.*$"
                  replace="SERVER_NAME=server2"/>
    <replaceregex pattern="^[ \t]*PROFILE_NAME[ \t]*=.*$"
                  replace="PROFILE_NAME=profi"/>
  </tokenfilter>
</filterchain>
Run Code Online (Sandbox Code Playgroud)

这将替换SERVER_NAME=SERVER_NAME=server2(以下相同PROFILE_NAME=)开头的每一行.这将返回获得您描述的输出.

[ \t]* 是忽略空格.


Jak*_*kub 7

Cleaner解决方案使用"propertyfile"ant任务 - 请参阅http://ant.apache.org/manual/Tasks/propertyfile.html

<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>
Run Code Online (Sandbox Code Playgroud)