You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jenkins Active Choices响应式参数中用Groovy自动读取属性文件值

Jenkins Active Choices 参数自动读取属性文件所有值的解决方案

你当前通过Groovy脚本为Jenkins Active Choices响应式参数从属性文件取值,但每次属性文件新增值都要手动修改脚本添加数组元素,想要实现自动读取所有值。

你尝试的properties.getProperties(test1)写法有误,Java的Properties类并没有getProperties()方法,正确的取值方式是properties.getProperty('test1')或者直接使用properties.test1(前者更推荐,避免属性名与类方法名冲突)。

以下是修正后的完整脚本,可自动读取属性文件中test1的所有逗号分隔值:

Properties properties = new Properties()
File propertiesFile = new File('Properties file Path')
propertiesFile.withInputStream {
    properties.load(it)
}

if (ACP.equals('Test1')) {
    // 读取test1属性值,不存在则返回空字符串
    def value = properties.getProperty('test1', '')
    if (value.isEmpty()) {
        return [] // 属性为空时返回空列表
    }
    // 按逗号分割后去除每个元素的前后空格(可根据属性文件格式调整)
    return value.split(',').collect { it.trim() }
}
// 其他ACP分支可在此补充
return []

关键说明

  • getProperty('test1', '')避免了属性不存在时抛出空指针异常,保证脚本稳定性。
  • collect { it.trim() }用于清理每个值的前后空格,如果你的属性文件中值没有多余空格,可直接返回value.split(',')。
  • 修改后,属性文件中test1新增逗号分隔项时,脚本会自动返回所有项,无需手动修改数组索引。

内容的提问来源于stack exchange,提问作者Kira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:05:59