Gradle 7(Groovy 3)-立即执行/调用闭包中的方法

mzsu5hc0  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(64)

我正在将Gradle从5.6.3更新到7.5.1。我有一个接受闭包的gradle自定义插件。在我的主应用程序的build.gradle中,我调用了自定义插件的任务,并为该闭包赋值。
主要项目-

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

自定义插件-

@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()
  ...
}

这在gradle 5(groovy 2.5)中工作得很好,但在gradle 7(groovy 3)中,
任务“custom”执行失败。
计算任务“custom”的属性“myClosure”时出错无法找到com. custom. gradle. plugin. tasks. TestTask类型的任务“custom”上参数[**/my.yaml,build_46x3acd2klzyjg008csx3dlg4$_run_closure1$_closure2$_closure5$_closure6@15088f00]的方法filesMatching()。
有什么建议来解决这个问题吗?谢谢你,谢谢!

mlmc2os5

mlmc2os51#

找不到任何解决办法。所以,我添加了以下黑客来解决这个问题-
主要项目-

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

自定义插件-

@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()
  ...
}

相关问题