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

标签 java ant jenkins build jenkins-plugins

我有一个从 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/

相关文章:

java - 使用 JenkinsRule 类构建 Jenkins 插件测试

java - Redis PubSub 与 Jedis 重新连接

java - 如何仅针对 Map 的第二个值对 `ArrayList` s 的 `Map` 进行排序

java - 启动时崩溃

java - 是否必须在 Gson 处理的类上保留默认构造函数?

ant - 如何从ftp下载最新的文件夹

java - 将 "git describe"输出传递给 Java 代码

java - 使用 ant 文件运行 Selenium 独立服务器

python - 为什么 Jenkins 捕获的标准输出忽略控制台输出上的换行符?

java - Maven 3 尚未实现构建单个 Maven 模块