Tuesday, December 7, 2004

Escaping ${} in NAnt Scripts

Today I was writing a NAnt script that calls an XSLT, passing in some regular expression replacement arguments so I could use replace occurrences of certain strings in an XML file. The problem I ran into is that the syntax for a regex replacement is ${group}, which is exactly the same as the syntax for a property in a NAnt script. I figured there must be an escape sequence in NAnt that lets you say, “No, I really want a dollar sign and a curly brace - I'm not specifying a property.“ But I tried backslash and the other usual suspects, then searched the web for a while. Nothing.

I banged my head against the problem for a while, and finally came up with this:

<property name="replacement" value="http://localhost/${product.name}-${websafe.version}${'${path}'}" />

It's the ${'${path}'} that actually does the trick: basically, I'm saying, “Use the string literal ${path}.“

I'm pretty sure this will only work in NAnt 0.85 and later, as that's when string literal expressions were introduced.


  1. Thank you for posting this. Like you I tried the usual suspects. A quick google turned up your answer. I appreciate the fact that you took the time to document this, thus saving others time.

    Quick Plug: The book 'Expert .NET Delivery Using NAnt and CruiseControl.NET' by APress is very good... but it does not have this info... thanks Craig.

  2. Ditto. That helped.

  3. Glad to be of service.

  4. Thanks Craig.

    Keep up the great work!!

  5. Thanks Craig It helped me.

    But one issue i have is i am trying to invoke few services which has "-" in this servicename.When i try to run the NET START Servicename from NAnt script it is not able to reconginize the service. Does this Escaping will introduce any additional spaces in the service name.? I have checked the length of the working service name and the one from the NAnt script. Both shows same.

  6. I'm not sure. It's been a long time since I've done this: almost five years.

    You could try shelling out to sc instead of net start

  7. Thanks... This helped me xmlpoke a nlog setting.. of course nlog layout renderes have to be formatted as '${shortdate}'.

  8. Thanks.
    Doing the same thing as Russ, but for log4net.

    The tips that keep on giving, hey?