Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

Groovy string interpolation #3980

tardis4500 ·

I've been trying to get groovy string interpolation to work with a script step where the script is
groovy:
var1 = 'val1'
var2 = 'val2'
logger.info("${var1}_${var2}")

but this always fails. I've tried escaping brackets with no success. Is this possible somehow or do I have to use: var1 + '_' + var2

  • replies 4
  • views 1552
  • stars 0
robinshen ADMIN ·

${} syntax is no longer necessary as you already inside a groovy script. Below should work:

groovy:
var1 = 'val1'
var2 = 'val2'
logger.info(var1 + "_" + var2)
narwhal ·

The interpolation is most useful if you had something like this robin

some_template = """
This is a really long string with ${interpolatedvar}.

Making a bunch of concatenates is not desirable because we want ${var1} to be interchangeable

and also not make our ${var2} completely useless
"""

But it can also be used for trivial cases as well as in the original post.

Also this @OP it is probably possible and you just need an escape on either the $ or {. here is the docs: http://docs.groovy-lang.org/2.5.1/html/documentation/#_string_interpolation

The reasoning for the escape is probably due to the parser already having interpolated or escaped it.

Makerino ·

Out of curiosity, what would you use a string like that for?

narwhal ·

writing out a file is one typical use. say you wanted to write out some artifact based on build options or status that later you publish or include in an email template.