You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins Pipeline中Groovy语法如何返回NullObject而非字符串?

在Jenkins Pipeline Groovy中返回NullObject而非字符串"null"

问题原因

你当前的代码因使用字符串插值,导致"null"被解析为字符串类型,而非Groovy的NullObject:

BRANCH = "develop" 
BRANCH = "${BRANCH=="develop"?"null":"${BRANCH}"}"
print (BRANCH.getClass()) // 输出 class org.codehaus.groovy.runtime.GStringImpl

正确语法

去掉字符串插值包裹,直接在三元表达式中返回null关键字(不要加引号):

BRANCH = "develop" 
BRANCH = BRANCH == "develop" ? null : BRANCH
print (BRANCH.getClass()) // 输出 class org.codehaus.groovy.runtime.NullObject

说明

  • Groovy中,null是关键字,表示空对象,类型为NullObject;而"null"是普通字符串,类型为GStringImpl或String。
  • 无需对变量做字符串插值包裹,直接使用原生Groovy三元表达式逻辑,即可正确返回null对象。

内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 19:05:18