在 Apache Ant 中转义斜杠

Escaping of slashes in Apache Ant

使用 Apache Ant,我希望我的属性文件输出

blurb=test\n\

但是有了这个,\n\ 将在构建过程中转义斜线

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

所以输出将是

blurb=test\n\

不协调

您可以使用内置 line.separator 属性 将文字字符串 \npropertyfile 任务回显。但是,如果您在非 Unix 系统上 运行 脚本,这将产生不同的输出,例如 \r\n

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

结果:

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

blurb=test\n

关于结尾的反斜杠,这是不可能的,因为 propertyfile 任务不只是盲目地将字符串回显到文件中;它主动维护一个 属性 文件并应用自动格式化。尾随的转义字符只是被格式化为空,因为它后面没有任何内容可以转义。

例如,如果您手动创建了以下属性文件:

blurb=test\n\

...然后运行下面的代码:

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

你会得到这样的结果:

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

blurb=test\n
anotherProperty=anotherValue

尽管脚本甚至没有对 blurb 属性.

执行任何操作,但反斜杠已被删除

如果你真的,真的出于某种原因想要将 blurb=test\n\ 写入你的文件,你可以使用 replaceregexp 任务(或者只是replace 任务,如果您确切知道现有值是多少):

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