Expand() behaviors with regex

task configure (type: Copy) {
    from ("${resources_dir}") {
        include "**/file.groovy"
        expand(props)
    }
    into "${dest}"
}

in my .groovy file it uses regex like so:

regex = '^.*\\.(htm|html|...|mp4)$'

and the expand gives me an error at the index where the $ chars are because it is thinking it is a tokenization of some kind.

Is there a way to:

  1. ignore these kinds of scenarios
    or
  2. only expand for existing variables in my .properties file and leave others be

moreover, it looks like expand() also takes into account the commented out lines, is there also a way to ignore these lines?

expand treats the file as Groovy template. So you for example have to escape backslashes and escape dollars if they should be literal. On the other hand you can also do calculations in that file that are evaluated during the expand ${new Date()}.

The expansion mechanism also is not aware of syntax rules or that it is a Groovy for, so you cannot ignore comments as they are not comments, they are just text like the whole file.

If you don’t want the Groovy template behavior, you must not use expand but one of the other replacement features that does what you want.

Yep, I just ended up using filter() instead which fixed the issue. Thanks for the reply

1 Like