首页 > 解决方案 > Groovy:如何加入地图值/为什么结果不一致?

问题描述

为什么以下 Groovy 脚本:

def d = ["foo":"lorem", "bar":"ipsum", "baz":"dolor"]
def j = d.inject([]) { result, e -> result += e.value }.join()

...导致所需的值jloremipsumdolorhttps://groovyconsole.appspot.com/但从我的 Jenkinsfile 运行时会导致以下错误:

java.lang.NullPointerException: Cannot invoke method join() on null object

我的 Jenkins 服务器正在运行 Groovy 2.4.12。
我不知道,也不清楚如何确定在https://groovyconsole.appspot.com/上运行的 Groovy 版本。

上面的代码灵感来自https://blog.mrhaki.com/2009/09/groovy-goodness-using-inject-method.html

为什么上述脚本在从 Jenkins 运行时会导致错误,以及实现所需功能的另一种优雅/Groovy 惯用方式是什么:加入所有地图值?

标签: jenkinsgroovyjenkins-groovylinkedhashmap

解决方案


推荐阅读