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

35,968

Solution 1

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>

Solution 2

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>

Solution 3

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>

Solution 4

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"/>
Share:
35,968

Related videos on Youtube

Vijay Shanker Dubey
Author by

Vijay Shanker Dubey

An avid reader and learner of Software Architecture. Entrepreneur at heart, Software Architect in Making Currently a Principal Software Engineer at Oracle India, Bengaluru(India). A Sun Cetified Java Programmer. Sites: Twitter Updates Blogs Send Queries at - [email protected]

Updated on February 25, 2020

Comments

  • Vijay Shanker Dubey
    Vijay Shanker Dubey about 4 years

    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 something and replace that text with the new text, and the old values. But it does not seems to be promising, as if in future someone changes the original file the build will fail.

    So, I would like my script to add the text at the end of the file.

    What options do I have for such a requirement?

  • Vijay Shanker Dubey
    Vijay Shanker Dubey over 13 years
    Hi, My text is a HTML template, and this task does not seems to be working with the template text. :( . What to do?
  • Vijay Shanker Dubey
    Vijay Shanker Dubey over 13 years
    one problem, after replacing the text, encoding gets changed and all the changed content is placed in a single line, with some spacial characters in between. I am not able to view the file correctly in notepad, editplus. But looks okay in wordpad. Will this be okay with java programs?
  • Michael Pilat
    Michael Pilat over 13 years
    That probably has to do with some combination of the character encoding of the ant (build.xml) file, the default system encoding, and whatever you pass to the encoding attribute of the <echo> task. Most likely it's writing a single carriage return (CR) as newlines instead of CR+LF which is the standard on Windows. Line endings are ignored in HTML anyway, so hopefully it doesn't actually matter, but it all depends on what your Java program does with the file in question.
  • Francis Upton IV
    Francis Upton IV about 13 years
    This is helpful when you want to append using a filter which is not addressed by the accepted solution.
  • Rhubarb
    Rhubarb almost 10 years
    +1 for concat, much more flexible with many options, and doesn't even touch the file if the text is empty- which allows you to make the concatenation conditional using properties without ant-contrib or targets (echo will always add to the file - even an empty string)