How can I change property values in a file using Ant?

21,059

Solution 1

Your filterchain is ok, but your source file should look like this:

SERVER_NAME=@SERVER_NAME@
PROFILE_NAME=@PROFILE_NAME@

This code (as provided by you)

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

replaces the tokens and gives you

SERVER_NAME=server2
PROFILE_NAME=profi

If you want to keep your original file as you have it now, one way would be to use 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>

This would replace every line starting with SERVER_NAME= by SERVER_NAME=server2 (same for PROFILE_NAME=). This will return get you the output you described.

[ \t]* is to ignore whitespace.

Solution 2

Cleaner solution is using "propertyfile" ant task - see 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>
Share:
21,059
Andrew
Author by

Andrew

Updated on February 14, 2020

Comments

  • Andrew
    Andrew over 4 years

    Example input:

    SERVER_NAME=server1
    PROFILE_NAME=profile1
    ...
    

    Example output:

    SERVER_NAME=server3
    PROFILE_NAME=profile3
    ...
    

    This file will use in applicationContext.xml. I've tried

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

    but it doesn't work.

  • Andrew
    Andrew about 14 years
    is it possible to change property value without tokens@@ ?
  • Mr Lou
    Mr Lou about 12 years
    How can i pass a or more arguments to ant,I do not want to use source files to set the var and value such you show SERVER_NAME=server2 I hope pass the args when i run the ant build file in eclipse.
  • Peter Lang
    Peter Lang about 12 years
    @janwen: This is a different question, so we should not mess up this one. Please ask a new question (a link to this question might be useful).
  • Sumit Ramteke
    Sumit Ramteke over 7 years
    This solution works perfectly for me. I wanted to make this happen for .properties file
  • plinyar
    plinyar about 7 years
    Great solution.Thanks
  • srinivas chaitanya
    srinivas chaitanya over 3 years
    WARNING:If the key is containing some special characters, It is appended with \ which is an issue