动态替换map中的变量 发表于 2019-11-02 | 更新于 2019-11-23 | 阅读次数: 12345678910111213141516171819202122232425262728def replaceVariableInMap(def oriValue, def key, def value) { if (oriValue instanceof String) { return oriValue.replace("\${${key}}", value.toString()) } else if (oriValue instanceof List) { def tmpList = [] oriValue.each { cf -> tmpList.add(replaceVariableInMap(cf, key, value)) } return tmpList } else if (oriValue instanceof Map) { def tmpMap = [:] oriValue.each { k, v-> tmpMap.put(k, replaceVariableInMap(v, key, value)) } return tmpMap } else { return oriValue }}def testMap = [general: [branch: "\${branch}"], stages: [tasks: [repos: [url: "\${url}", branch: "\${branch}", id: "domain"]]]]def params = [branch: "test_branch", url: "https://git.test.com"]params.each {k, v -> testMap = replaceVariableInMap(testMap, k, v)}println testMap 打赏 微信支付