18

Does anyone know how to output a message using echo with a new line character, in the ant world i used ${line.seperator}, but i dont see any related property in Nant, nor any of the functions provided this info. I also tried the \n escape character but that was printed as it is. An examaple is below

<echo message="spool \n off \n quit" file="${scripts.list}" append="true"/>
Dinesh Manne
  • 1,824
  • 6
  • 25
  • 32

8 Answers8

20

I don't know Nant, but the XML entity for newline is usually &#xa;

eduffy
  • 39,140
  • 13
  • 95
  • 92
  • Thanks, i was able to get it after the substitution, did not get the idea to use it even thought i knew it, however i would like to wait to see if anyone has any answers specific to Nant – Dinesh Manne Jun 09 '09 at 13:32
  • 3
    that's what you get for programming in XML! – Matthew Lock Sep 09 '11 at 03:18
  • This solution is over complicated. With [the other echo syntax](https://stackoverflow.com/a/970076/116327), you don't need any XML code, new lines are natural. – Laurent Simon Aug 23 '18 at 22:43
11

In NAnt 0.90, I used the environment::newline() function to put in newlines where I wanted them. But since it's a little unwieldy to use ${environment::newline()}, I assigned it to a shortened property name that I could use throughout my script.

Unfortunately for 0.86 users, the environment::newline() function wasn't added till 0.90-alpha1.

MVH
  • 111
  • 1
  • 2
6

Use ${environment::newline()}

Earlier echo used to add a newline by default on append which has been fixed in latest release. So, now this function has been introduced so you may get the environment variable for newline and generate log as formatted as you like.

I had to go through release notes to find it, so I thought it may save someone else some effort to have it answered here.

Adam Eberlin
  • 14,005
  • 5
  • 37
  • 49
Sudhanshu
  • 61
  • 1
  • 1
6

It will be more convienient if you use echo as shown below:

<echo append="true" file="${scripts.list}">
spool
off
quit
</echo>

Nant will maintain newlines out of the box:)

head_thrash
  • 1,623
  • 1
  • 21
  • 26
1

you can just use newlines in the xml:

<echo message="Line1
Line2"/>
user324008
  • 19
  • 1
0

Maybe you also made a mistake, because in your post you wrote line.seperator instead of line.separator

j0k
  • 22,600
  • 28
  • 79
  • 90
luckyluke
  • 491
  • 7
  • 16
0

If you edit your NAnt script in Visual Studio and like to auto-format (Ctrl-E D) your XML document, you'll see that if you use the <echo> message </echo> form to output multiple lines, Visual Studio will auto-indent your text as well. To prevent this, you need to use an XML CDATA section, like this:

   <echo file="${file}"
         append="true">
     <![CDATA[
cmd1 "${arg1}"
cmd2 "${arg2}"
]]>
   </echo>

This will survive the auto-format process. You will still end up with a leading newline unless you start your text right after the <![CDATA[ tag on that line.

Erhhung
  • 967
  • 9
  • 14
0

In my case:

header('Content-Type: application/xml;');       
header('Content-Disposition: attachment; filename="strings.xml"');

//for example
echo '<?xml version="1.0" encoding="utf-8"?>';          
echo '<!-- ********************** -->';         

generates <?xml version="1.0" encoding="utf-8"?><!-- ********************** -->


With &#xa; :

header('Content-Type: application/xml;');       
header('Content-Disposition: attachment; filename="strings.xml"');

//for example
echo '<?xml version="1.0" encoding="utf-8"?>&#xa;';         
echo '<!-- ********************** -->';

generates <?xml version="1.0" encoding="utf-8"?>&#xa;<!-- ********************** -->


Simply with the enter command it works:

    header('Content-Type: application/xml;');       
    header('Content-Disposition: attachment; filename="strings.xml"');

    //for example
    echo '<?xml version="1.0" encoding="utf-8"?>
';          
    echo '<!-- ********************** -->';

generates

<?xml version="1.0" encoding="utf-8"?> <!-- ********************** -->

Leonardo Rignanese
  • 865
  • 11
  • 22