1

I'm trying to do this in Ant:

<echo message="&#27;[44;1;37mSuccess!&#27;[m" />

But it doesn't work:

BUILD FAILED
./build.xml:92: Character reference "&#

How to do it?

Jarekczek
  • 7,456
  • 3
  • 46
  • 66
yegor256
  • 102,010
  • 123
  • 446
  • 597

3 Answers3

5

The 0x1B character is invalid in XML contents (Invalid Characters in XML). So you need some workaround. I would go with a javascript workaround, but I give also 2 additional solutions:

javascript

<script language="javascript">
  project.setNewProperty("esc", "\u001b");
</script>
<echo>${esc}</echo>

native2ascii

If you want the output in a file, then you could first output it using java escape \u001b, then convert it using reverse Native2Ascii routine. Regardless of the selected encoding it always decodes \u sequences.

<echo file="a.enc">\u001b</echo>
<native2ascii includes="a.enc" ext=".txt" dest="${basedir}"
              encoding="iso-8859-1" reverse="true" />

property file

Finally you may have the unfortunate string constant in a file:

<property file="prop.txt" />
<echo>myEsc:${myEsc}</echo>

while the prop.txt contents is:

myEsc=\u001b
Community
  • 1
  • 1
Jarekczek
  • 7,456
  • 3
  • 46
  • 66
  • The `Character reference "` error occurs for `` (backspace) too. So the linked list of valid XML characters seems to be incomplete for ANT files. – MattTT Jun 14 '23 at 13:19
2

Simply use CDATA :

<project>

 <echo><![CDATA[
  &#27;[44;1;37mSuccess!&#27;[m
  ]]>
 </echo>

</project>
Rebse
  • 10,307
  • 2
  • 38
  • 66
0

& is a special character in ant, so you should replace it with '

<echo message="&amp;#27;[44;1;37mSuccess!&amp;#27;[m" />
magodiez
  • 741
  • 2
  • 10
  • 23
  • 1
    You meant `&` probably. But in this case I will see an ampersand in console, while I'm expecting `\x1b` (ESC symbol) – yegor256 Oct 29 '12 at 14:27