蚂蚁作业为字符串密码字段添加转义字符

Ant job adding escape character for String Password field

我有一个从 Jenkins 调用的 Ant 构建。 Jenkins 作业有参数,其中一个是密码字符串(Jenkins 中的"Password Parameter")。

在属性文件中写入密码的Ant目标指定为:

<target name="pwd-properties">
        <echo>Password is: ${password}</echo>   
       <propertyfile file="data.properties" comment="property file">
          <entry key="Pwd" value="${password}" />                 
       </propertyfile>
    </target>

密码是

I am password!

然而,在构建机器中它显示为

I am password\!

在属性文件中。然而回声显示正确。

谁能告诉我它是如何在密码字符串中获取额外的转义字符的?

它与 Ant 无关 - 这只是 Properties.store:

的记录行为

Then every entry in this Properties table is written out, one per line. For each entry the key string is written, then an ASCII =, then the associated element string. For the key, all space characters are written with a preceding \ character. For the element, leading space characters, but not embedded or trailing space characters, are written with a preceding \ character. The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.

示例代码:

import java.io.*;
import java.util.*;

public class Test {
    public static void main(String[] args) throws Exception {
        StringWriter writer = new StringWriter();
        Properties props = new Properties();
        props.setProperty("key", "value!");
        props.store(writer, "Demo");
        System.out.println(writer);
    }
}

输出:

#Demo
#Wed Feb 04 22:38:55 GMT 2015
key=value\!

换句话说,一切都很好。

转义的原因是因为!用于注释。来自 Properties.load:

A comment line has an ASCII '#' or '!' as its first non-white space character; comment lines are also ignored and do not encode key-element information.

现在它可以有条件地转义 - 换句话说,只有当它以其他方式充当注释字符时 - 但最简单的方法就是一直转义它。