简体   繁体   English

在Apache Ant中转义斜线

[英]Escaping of slashes in Apache Ant

Using Apache Ant, I want my propertyfile to output 使用Apache Ant,我希望输出我的属性文件

blurb=test\n\

But with this, the \\n\\ will escape the slashes during build 但是有了这个,\\ n \\将在构建过程中转义斜线

<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. 您可以使用内置的line.separator属性,用propertyfile任务回显文字字符串\\n However, this will produce different output such as \\r\\n if you run the script on a non-Unix system. 但是,如果您在非Unix系统上运行脚本,则会产生不同的输出,例如\\r\\n

    <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; 关于尾部的反斜杠,这是不可能的,因为propertyfile任务不仅只是盲目地将字符串回显到文件中,还包括将其添加到文件中。 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. 尽管脚本甚至对blurb属性没有任何作用,反斜杠也已删除。

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): 如果您确实确实出于某种原因想要将blurb=test\\n\\写入文件,则可以使用replaceregexp任务(或者,如果您确切知道现有值是什么,则可以使用replace任务):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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