4

I originally had this:

<echo file="${my.file}"
     message="This is line #1"/>
<echo file="${my.file}"
     append="true"
     message="This is line #2"/>
<echo file="${my.file}"
     append="true"
     message="This is line #3"/>

And got this in my file:

 This is line#1This is line #2This is line#3

So, I tried:

 <echo file="${my.file}">
     This is line #1
     This is line #2
     This is line #3
 </echo>

And got:

 This is line#1This is line #2This is line#3

I did this:

 <echo file="${my.file}">
     This is line #1${line.separator}
     This is line #2${line.separator}
     This is line #3${line.separator}
 </echo>

And got:

 This is line#1This is line #2This is line#3

I did this:

  <echo file="${my.file}">
     This is line #1&#13;&#10;
     This is line #2&#13;&#10;
     This is line #3&#13;&#10;
 </echo>

And got:

 This is line#1This is line #2This is line#3

I even tried this:

 <concat destfile="${my.file}">
     This is line #1&#13;&#10;
     This is line #2&#13;&#10;
     This is line #3&#13;&#10;
 </concat>

And still got:

 This is line#1This is line #2This is line#3

If I do any of these to the console, it prints on separate lines. Saving them to a file, and the lines don't show up.

I'm on Win7, Ant 1.8 something.

Any ideas?


Windows Thing?

I've tried each of these on my Mac. The first one gave me the same results. However, the second way gave me three lines in the file. The third way with the ${line.separator} added skipped every other line. The fourth way using &#13;&10; gave me every other line with a ^M at the end (after all, the Mac is Unix and doesn't use the CRLF ending, but just LF).

And, using <concat> also created separate lines.


More information

I've traced down the issue to the following snippet from my build.xml file:

This works as advertised. That is, white space and NL show up in the resulting file:

<target name="package"
    depends="-resolve,compile"
    description="Packaging of the artifact">

    <!-- Build version.txt and let the user figure out how to use it -->
    <mkdir dir="${target.dir}"/>
    <echo file="${version.file}">
        Jenkins Project: ${env.JOB_NAME}
        Jenkins Build Number: ${env.BUILD_NUMBER}
        Build Date: ${build.date}
    </echo>

    <!--
    <antcall target="jar"/>
    <antcall target="war"/>
    <antcall target="ear"/>
    -->
</target>

This doesn't:

<target name="package"
    depends="-resolve,compile"
    description="Packaging of the artifact">

    <!-- Build version.txt and let the user figure out how to use it -->
    <mkdir dir="${target.dir}"/>
    <echo file="${version.file}">
        Jenkins Project: ${env.JOB_NAME}
        Jenkins Build Number: ${env.BUILD_NUMBER}
        Build Date: ${build.date}
    </echo>

    <antcall target="jar"/>
    <antcall target="war"/>
    <antcall target="ear"/>
</target>

The only difference between these snippets in the fact that I do three <antcall> tasks AFTER I write the file. The three antcall tasks all have an if clause, so they may or may not run based upon the properties that are set. This is a build template for our development teams, so I really would like to get this to work.

Why could the <antcall>'s be causing problems. By the way, sometimes it is so broken, I need to reopen a new console window in order to get the NL stuff to work.

I've ran into a similar issue before, and it was a Windows codepage issue.

Community
  • 1
  • 1
David W.
  • 105,218
  • 39
  • 216
  • 337

2 Answers2

2

The Ant project below creates four files using the echo task:

  • echo1.txt
  • echo2.txt
  • echo3.txt
  • echo4.txt

The hex view of each file is as follows when the project is run on Windows 7 using Ant 1.8.x:

// echo1.txt
// HEX                         // TEXT
6c 69 6e 65 20 23 31           line #1
6c 69 6e 65 20 23 32           line #2
6c 69 6e 65 20 23 33           line #3

// echo2.txt
// HEX                         // TEXT
6c 69 6e 65 20 23 31 0a        line #1  (0a => line feed)
6c 69 6e 65 20 23 32 0a        line #2
6c 69 6e 65 20 23 33 0a        line #3

// echo3.txt
// HEX                         // TEXT
6c 69 6e 65 20 23 31 0d 0a 0a  line #1  (0d => carriage return)
6c 69 6e 65 20 23 32 0d 0a 0a  line #2
6c 69 6e 65 20 23 33 0d 0a 0a  line #3

// echo4.txt
// HEX                         // TEXT
6c 69 6e 65 20 23 31 0d 0a     line #1
6c 69 6e 65 20 23 32 0d 0a     line #2
6c 69 6e 65 20 23 33 0d 0a     line #3

Only echo4.txt [which places all three lines in the message attribute separated by ${line.separator}] produces the correct line ending on Windows (carriage return + line feed). The output was the same when the project file was edited in Notepad (which uses carriage return + line feed) as well as editors that preserve the Unix line ending (a single line feed). It appears that when Ant parses XML files line endings are normalized to a single line feed character.

Ant Project

<?xml version="1.0" encoding="UTF-8"?>
<project name="echo-project" basedir="." default="echo-all">

  <target name="echo-test1">
    <property name="echo1.file" location="${basedir}/echo1.txt" />
    <echo file="${echo1.file}" message="line #1" />
    <echo file="${echo1.file}" message="line #2" append="true" />
    <echo file="${echo1.file}" message="line #3" append="true" />
  </target>

  <target name="echo-test2">
    <property name="echo2.file" location="${basedir}/echo2.txt" />
    <echo file="${echo2.file}">line #1
line #2
line #3
</echo>
  </target>

  <target name="echo-test3">
    <property name="echo3.file" location="${basedir}/echo3.txt" />
    <echo file="${echo3.file}">line #1${line.separator}
line #2${line.separator}
line #3${line.separator}
</echo>
  </target>

  <target name="echo-test4">
    <property name="echo4.file" location="${basedir}/echo4.txt" />
    <echo file="${echo4.file}" message=
"line #1${line.separator}line #2${line.separator}line #3${line.separator}" />
  </target>

  <target name="echo-all"
      depends="echo-test1, echo-test2, echo-test3, echo-test4" />
</project>
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • Thanks for the very thorough answer. I too looked at the octal dumps, and noticed it wasn't adding any line endings. In fact, when I looked at the file, the white space at the beginning of the lines also went missing. I added further information to the bottom of my post about other possible issues. I'll give you your _myEcho_ task a try. I am beginning to suspect the problem is with the environment and not with Ant or my build file itself. – David W. Aug 07 '12 at 13:57
1

Have you tried \n as a line breaker?

kgautron
  • 7,915
  • 9
  • 39
  • 60
  • I hadn't tried `\n`, but that doesn't [work with Ant](http://stackoverflow.com/questions/7102793/how-to-put-a-newline-in-ant-property). Then again, I'm desperate enough to try anything. – David W. Aug 07 '12 at 02:47
  • Just tried with `\n`. It doesn't work. Just puts `\n` at the end of each line on the Mac, and just interspaces `\n` on Windows. – David W. Aug 07 '12 at 03:02
  • curious.. I just tried option 2 and 3 on WinXP + ANT 1.6.5 and both work. Option 2 gives the desired output, while 3 adds one more extra line after each. – Pulak Agrawal Aug 07 '12 at 03:34
  • Only things I can think of (usual suspects).. try downgrading ANT (`echo` task has been updated in ANT 1.8), check with `ant -d` or `ant -v`. – Pulak Agrawal Aug 07 '12 at 03:41