Gradle 7 (Groovy 3) - method in a closure executed/called immediately

139 Views Asked by At

I am updating Gradle from 5.6.3 to 7.5.1. I have a gradle custom plugin that accepts a closure. And in my main application's builg.gradle I am calling that custom plugin's task and assigning value to that closure.

main project -

task(type: MyCustomTask, 'custom') {
  myClosure = {
    filesMatching('**/my.yaml') {
      filter { it.replace('${test}', 'TEST') }
    }
  }
  ...
}

custom plugin -

@Input
@Optional
Closure myClosure = { }

private void copyFiles() {
  println "--------------------------Copying"
  project.copy {
    from "${project.projectDir}/src/main/resources"
    into "${buildDir}"
    final cl = myClosure.clone()
    cl.delegate = delegate
    cl()
  }
}

@TaskAction
def custom() {
  ...
  copyFiles()
  ...
}

This is working fine with gradle 5 (groovy 2.5), but with gradle 7 (groovy 3) I am getting

Execution failed for task 'custom'.

Error while evaluating property 'myClosure' of task 'custom' Could not find method filesMatching() for arguments [**/my.yaml, build_46x3acd2klzyjg008csx3dlg4$_run_closure1$_closure2$_closure5$_closure6@15088f00] on task 'custom' of type com.custom.gradle.plugin.tasks.TestTask.

Any suggestion here to fix the issue? Thank you!

1

There are 1 best solutions below

0
MKB On BEST ANSWER

Not able to find any solution. So, I added the following hack to fix this -

main project -

task(type: MyCustomTask, 'custom') {
    replaceText('**/my.yaml', '${test}', 'TEST')
    ...
}

custom plugin -

@Input
@Optional
Closure myClosure = { }

@Input
@Optional
Map fileArgs = [:]

void replaceText(String a, String b, String c) {
    fileArgs = [a: a, b: b, c: c]
}

private void copyFiles() {
    project.copy {
      from "${project.projectDir}/src/main/resources"
      into "${buildDir}"
      final cl = myClosure.clone()
      cl.delegate = delegate
      cl()

      if (fileArgs) {
        filesMatching(fileArgs.a) {
          filter { it.replace(fileArgs.b, fileArgs.c) }
        }
      }
    }
}

@TaskAction
def custom() {
  ...
  copyFiles()
  ...
}