0

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!

MKB
  • 7,587
  • 9
  • 45
  • 71
  • 1
    sounds like you getting wrong `delegate` – daggett Dec 22 '22 at 08:14
  • i guess the closure resolve strategy has been changed between versions... https://docs.groovy-lang.org/latest/html/api/groovy/lang/Closure.html#setResolveStrategy(int) – daggett Dec 22 '22 at 08:19
  • 1
    FYI you should probably use the new way of adding tasks, like `tasks.register('name', MyCustomTask) { ... }`. That probably won't help you much but it may make your config phase faster. – Renato Dec 22 '22 at 22:58
  • yah, I trie that also (got the same issue). Thanks for sharing. – MKB Dec 23 '22 at 04:23
  • How can I fix the delegate issue? Any suggestion. – MKB Dec 23 '22 at 13:04

0 Answers0