【问题标题】:Is there anyway i can get value from groovy closure无论如何我可以从常规关闭中获得价值
【发布时间】:2021-07-11 09:24:11
【问题描述】:

我有一个如下的闭包,我想从 a、b、c 中获取价值。

#!/usr/bin/env groovy
entry {
    a = false
    b = "nodejs"
    c = "xxx"
}
println entry.a
println entry.b
println entry.c

有什么方法可以实现吗? 我总是收到如下错误

% groovy jenkinsfile.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: jenkinsfile.entry() is applicable for argument types: (jenkinsfile$_run_closure1) values: [jenkinsfile$_run_closure1@12aba8be]
Possible solutions: every(), every(groovy.lang.Closure), any(), any(groovy.lang.Closure), notify(), identity(groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: jenkinsfile.entry() is applicable for argument types: (jenkinsfile$_run_closure1) values: [jenkinsfile$_run_closure1@12aba8be]
Possible solutions: every(), every(groovy.lang.Closure), any(), any(groovy.lang.Closure), notify(), identity(groovy.lang.Closure)
    at jenkinsfile.run(jenkinsfile.groovy:2)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

我必须使用这种方式,因为我有很多以这种格式编写的 Jenkinsfile,我想从 Jenkinsfile 中获取值。这是来自 Jenkinsfile 的格式。

【问题讨论】:

  • 不,如果你不能或不会改变它是不可能的
  • entry {a=false ...} 不是闭包。它看起来像是对一个名为entry 的函数的调用,它接受一个参数是一个闭包体,它是{ 和} 之间的块。由于没有定义函数entry,因此您会收到该错误。

标签: groovy closures jenkins-groovy


【解决方案1】:

您如何使用地图文字:

entry = [
    a:false,
    b:"nodejs",
    c:"xxx"
]
println entry.a
println entry.b
println entry.c

【讨论】:

  • 我必须使用这种方式,因为我有很多以这种格式编写的 Jenkinsfile,我想从 Jenkinsfile 中获取值。这是来自 Jenkinsfile 的格式。
  • 将这些文件作为文本读取并使用一些正则表达式解析值可能有意义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 2013-09-27
相关资源
最近更新 更多