简体   繁体   中英

Escaping of slashes in Apache Ant

Using Apache Ant, I want my propertyfile to output

blurb=test\n\

But with this, the \\n\\ will escape the slashes during build

<propertyfile file="about.properties">
    <entry key="blurb" value="test\n\"/>
</propertyfile>

So the output will be

blurb=test\\n\\

which is incoorect

You can echo the literal string \\n with the propertyfile task by using the built-in line.separator property. However, this will produce different output such as \\r\\n if you run the script on a non-Unix system.

    <propertyfile file="about.properties">
        <entry key="blurb" value="test${line.separator}" />
    </propertyfile>

Result:

#Thu, 07 Mar 2019 10:33:16 -0800

blurb=test\n

Regarding the trailing backslash, this isn't possible because the propertyfile task doesn't just blindly echo strings into a file; it actively maintains a property file and applies automatic formatting. A trailing escape character just gets formatted into nothing, since nothing comes after it for it to escape.

For example, if you manually created the following properties file:

blurb=test\n\

...And then ran the following code:

    <propertyfile file="buildNumber.properties">
        <entry key="anotherProperty" value="anotherValue" />
    </propertyfile>

You'd end up with this:

#Thu, 07 Mar 2019 10:42:43 -0800

blurb=test\n
anotherProperty=anotherValue

The backslash is removed despite the fact that the script didn't even do anything to the blurb property.

If you really, really want to write blurb=test\\n\\ into your file for some reason, you can do so with the replaceregexp task (or just the replace task, if you know exactly what the existing value will be):

    <replaceregexp
        file="about.properties"
        match="blurb=.*"
        replace="blurb=test\\\\n\\\"
    />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM