当propertyMissing定义为返回null时,Groovy抛出MissingPropertyException

时间:2018-02-02 12:03:28

标签: jenkins groovy

两个不同的Jenkins工作正在使用特定的jenkinsfile。 Job1需要将PARAM1PARAM2传递给作业。这是通过在作业配置中使用字符串参数来完成的。 job2只需要通过PARAM1。这也是通过字符串参数完成的。

因此job1有2个字符串参数,PARAM1PARAM2

job2有1个字符串参数PARAM1

为避免使用try { ... } catch (MissingPropertyException) { ... },我尝试使用the workaround described here。我的代码如下:

def propertyMissing(name) {
  println "*** something could not be resolved to a parameter - will be set to NULL ***"
}

PARAM1 = PARAM1 ?: 'default value'
PARAM2 = PARAM2 ?: 'default value'

在job2上,这在PARAM2 = PARAM2 ?: 'default value'失败并引发异常:groovy.lang.MissingPropertyException: No such property: PARAM2 for class: groovy.lang.Binding

在job1上,当尝试设置环境变量(`env.SOMETHING ='value')时,这在另一个阶段失败,我得到以下堆栈跟踪:

*** env could not be resolved to a parameter - will be set to NULL ***
*** currentBuild could not be resolved to a parameter - will be set to NULL ***
[Pipeline] End of Pipeline
java.lang.NullPointerException: Cannot set property 'result' on null object

在这种情况下,有没有办法避免使用try / catch语句?

0 个答案:

没有答案