How do I append some text at the end of a file using Ant?

后端 未结 4 2273
抹茶落季
抹茶落季 2021-02-18 14:11

In one of the configuration files for my project I need to append some text. I am looking for some options to do this using Ant.

I have found one option - to find someth

相关标签:
4条回答
  • 2021-02-18 14:27

    I found the other answers useful, but not giving the flexibility I needed. Below is an example of writing echos to temp file that can be used as a header and footer, then using concatenation to wrap an xml document.

        <!-- Make header and footer for concatenation -->
        <echo file="header.txt"  append="true">
            <![CDATA[
                <?xml version='1.0' encoding='UTF-8'?>
                <!DOCTYPE foo ...>
            ]]>
        </echo>
        <echo file="footer.txt"  append="true">
            <![CDATA[
                </foo>
            ]]>
        </echo>
    
        <concat destfile="bigxml.xml">
            <fileset file="header.txt" />
            <fileset file="bigxml-without-wrap.xml" />
            <fileset file="footer.txt" />
        </concat>
        <delete file="header.txt"/>
        <delete file="footer.txt"/>
    
    0 讨论(0)
  • 2021-02-18 14:31

    The concat task would look to do it as well. See http://ant.apache.org/manual/Tasks/concat.html for examples, but the pertinent one is:

    <concat destfile="README" append="true">Hello, World!</concat>
    
    0 讨论(0)
  • 2021-02-18 14:37

    Another option would be to use a filterchain.

    For example, the following will append file input2.txt to input1.txt and write the result to output.txt. The line separators for the current operating system (from the java properties available in ant) are used in the output file. Before using this you would have to create output2.txt on the fly I guess.

    <copy file="input1.txt" tofile="output.txt" >
        <filterchain>
            <concatfilter append="input2.txt" />
            <tokenfilter delimoutput="${line.separator}" />
        </filterchain>
    </copy>
    
    0 讨论(0)
  • 2021-02-18 14:49

    Use the echo task:

    <echo file="file.txt" append="true">Hello World</echo>
    

    EDIT: If you have HTML (or other arbitrary XML) you should escape it with CDATA:

    <echo file="file.txt" append="true">
    <![CDATA[
      <h1>Hello World</h1>
    ]]>
    </echo>
    
    0 讨论(0)
提交回复
热议问题