gpt4 book ai didi

java - Ant 作业为字符串密码字段添加转义字符

转载 作者:行者123 更新时间:2023-12-02 05:00:30 29 4
gpt4 key购买 nike

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

将密码写入属性文件的 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.

现在可以有条件转义 - 换句话说,只有当它充当注释字符时 - 但最简单的方法是始终转义它。

关于java - Ant 作业为字符串密码字段添加转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28332729/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com