引用带有凹槽的字符串中的变量

发布于 2025-01-30 08:24:17 字数 518 浏览 4 评论 0原文

抱歉,如果还不够清晰。

我有以下groovy片段(来自詹金斯)

def jdk = jobConfig.java

jobConfig.env.JAVA_HOME="${tool '$jdk'}"
jobConfig.env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"

如何在此行上的单个引号内替换$ jdk变量的值?

jobConfig.env.JAVA_HOME="${tool '$jdk'}" 

例如,这无问题。工具的用法还可以:

jobConfig.env.JAVA_HOME="${tool 'openjdk_11.0.14'}"

”的硬编码值设置为来自def jdk = jobconfig.java的值

但是我​​想将“ OpenJDK_11.0.14 解决方案。

非常感谢

Sorry if it was not clear enough.

I have the following groovy snippet (from Jenkins)

def jdk = jobConfig.java

jobConfig.env.JAVA_HOME="${tool '$jdk'}"
jobConfig.env.PATH="${env.JAVA_HOME}/bin:${env.PATH}"

How can I substitute the value of $jdk variable inside the single quotes on this line?

jobConfig.env.JAVA_HOME="${tool '$jdk'}" 

For example, this works with no problems. The usage of tools is OK:

jobConfig.env.JAVA_HOME="${tool 'openjdk_11.0.14'}"

But I want to set the hardcoded value of 'openjdk_11.0.14' to the value coming from def jdk = jobConfig.java

I tried a few variations but couldn't find the solution.

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2025-02-06 08:24:17

如何引用单引号内的$ JDK变量?

目前尚不清楚您的意思是什么,但是您拥有的是:

jobConfig.env.JAVA_HOME="${tool '$jdk'}"

这是错误的,但要解决如何处理单个引号,其中1个问题是您已经嵌套了$ 将混淆情况。

如果您试图将jdk的值与单样相关联,则可以做到这一点:

def someJavaHomeRelatedVariable ="'${jdk}'"

请注意,引号不在{}表达式之外。

一个单独的问题是您在该表达式中使用工具,但是上面说明了如何用单引号处理jdk的价值。

编辑

这个问题不清楚,但也许您想做这样的事情:

def sout = new StringBuilder()
def jdk = jobConfig.java

def proc = ['tool', jdk].execute()
proc.consumeProcessOutput(sout, null)
proc.waitForOrKill(2000)

jobConfig.env.JAVA_HOME =  sout.toString()

How can I reference the $jdk variable inside the single quotes?

It isn't clear what you mean by that but what you have is this:

jobConfig.env.JAVA_HOME="${tool '$jdk'}"

There are a few things wrong with that but to address how to deal with the single quotes, 1 of the issues is you have nested $ which is going to confuse the situation.

if you are trying to surround the value of jdk with single quotes, you can do this:

def someJavaHomeRelatedVariable ="'${jdk}'"

Note that the quotes are outside of the {} expression.

A separate issue is your use of tool in that expression, but the above explains how to deal with surrounding the value of jdk with single quotes.

EDIT

The question isn't clear but maybe you are wanting to do something like this:

def sout = new StringBuilder()
def jdk = jobConfig.java

def proc = ['tool', jdk].execute()
proc.consumeProcessOutput(sout, null)
proc.waitForOrKill(2000)

jobConfig.env.JAVA_HOME =  sout.toString()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文