2

I'm using the Ant LoadFile task to read the version from a file in the repository root.

<loadfile property="build.version" srcFile="VERSION"/>

The version file ends with a newline due to the tooling used to create and edit it. Ant is reading and using that newline as part of the property value. That's jamming me up when I use the value as a replacement for a file or output.

@build.version@ -> 99.0.0.0^M

Is there some way to strip that out of the property after loading or as part of the loadfile task?


I'm talking about this kind of Chomp.


The VERSION file literally contains only the version number and a newline (line numbers are mine for illustration).

1. 99.0.0.0
2. 
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188

1 Answers1

7

You can use a filterchain to remove such line breaks:

<loadfile property="build.version" srcFile="VERSION">
    <filterchain>
        <striplinebreaks/>
    </filterchain>
</loadfile>

See https://ant.apache.org/manual/Types/filterchain.html#striplinebreaks.

M A
  • 71,713
  • 13
  • 134
  • 174
  • `striplinebreaks` works well for single-line inputs, in general a combination of `trim` and `fixcrlf` may be more appropriate. – Stefan Bodewig Mar 18 '15 at 19:01
  • @StefanBodewig, I'd love to see an example of that... I'm not so familiar, are those filterchain options? – Anthony Mastrean Mar 19 '15 at 16:02
  • Oh, I just realized I was talking about the more general case of reading a property file with multiple lines rather than a single file read into a property at once. For your case @manouti's answer is the best solution. For an example of the other filterreaders see https://gist.github.com/bodewig/d416666c701c313616c1 – Stefan Bodewig Mar 20 '15 at 04:46