Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indenting a heredoc in a Groovy source file

Tags:

groovy

I'd like to preserve the expected indentation in my source file when using a heredoc but, at the same time, have the contents of the here document not indented when rendered. E.g. the following

// myscript.groovy
if ( someCondition ) {
  println """
  some multi-line
  content
  """
}

will print an indented output e.g.

$ groovy myscript.groovy

    some multi-line
    content

but instead I'd like it to just print the content unindented as follows

$ groovy myscript.groovy

some multi-line
content

how can this be accomplished in Groovy?

Bash has the <<-STRING heredoc definition to accomplish this but I've not been able to find anything similar for Groovy.

like image 594
Friedrich 'Fred' Clausen Avatar asked Jun 20 '16 04:06

Friedrich 'Fred' Clausen


1 Answers

You need stripMargin

if ( someCondition ) {
  println """
            |some multi-line
            |content
            |""". stripMargin()
}
like image 144
tim_yates Avatar answered Nov 18 '22 10:11

tim_yates