tags:

views:

102

answers:

2

I am writing a build script for my project. In one of the configuration files I need to append some text. So I am looking for some options in ant to do this.

I have got one option to find something and replace that text with all of your 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 tool 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?

+2  A: 

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>
Michael Pilat
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
@Michael Pilat: 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?
Vijay Shanker
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.
Michael Pilat
+3  A: 

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