将groovy字符串转换为jenkins管道中的映射 [英] Convert groovy string to map in jenkins pipeline

查看:86
本文介绍了将groovy字符串转换为jenkins管道中的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Groovy和Jenkins管道还很陌生,我需要将字符串转换为地图以获取一些可行的数据,但到目前为止我还没有找到解决方案.

I'm fairly new to Groovy and Jenkins pipelines and i need a string converted to a map to get some workable data i haven't been able to find a solution so far.

[fileListJs:file1.js,file2.js,file3.js, fileListHtml:file4.html,file5.html,file6.html, fileListCss:file6.css,file7.css,file8.html]

上面的字符串是某个詹金斯选择参数返回给我的groovy变量.现在,我需要它是一个正确的映射,以便能够解析其中的数据.

The string above is what a certain jenkins choice parameter is returning me in a groovy variable. Now i need this to be a proper map to be able to parse the data in there.

所以我尝试这种方法: 要映射的Groovy字符串

So i tries this approach: Groovy String To map

执行以下代码:

def fileMap =
    // Take the String value between
    // the [ and ] brackets.
    fileList[1..-2]
        // Split on , to get a List.
        .split(', ')
        // Each list item is transformed
        // to a Map entry with key/value.
        .collectEntries { entry ->
            def pair = entry.split(':')
            [(pair.first()): pair.last()]
        }

fileList是已返回先前给定字符串的变量.

Where fileList is the variable that has returned the previous given string.

但这不起作用.

非常感谢您的帮助.

谢谢

推荐答案

我相信这就是您想要的.

I believe this is what you are looking for.

String fileList = "[fileListJs:file1.js,file2.js,file3.js, fileListHtml:file4.html,file5.html,file6.html, fileListCss:file6.css,file7.css,file8.css]"

fileList[1..-2]
       .split(/, /)
       *.tokenize(/:/)
       .collectEntries { [ it[0], it[1].tokenize(/,/) ] }

应将结果Map设置为

[
  fileListJs:  ['file1.js', 'file2.js', 'file3.js'], 
  fileListHtml:['file4.html', 'file5.html', 'file6.html'], 
  fileListCss: ['file6.css', 'file7.css', 'file8.css']
]

这篇关于将groovy字符串转换为jenkins管道中的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆