Groovy: 在这些情况下如何替换左 Shift 运算符?

Groovy: How to replace leftShift operator in these cases?

我的 build.gradle 中有一个插件的下一个调用:

apply plugin: 'com.company.gradleplugins.plugin'

当我编译时,Jenkins 警告我有关 Gradle 5.0 及其弃用的信息:

The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.

at build_c4218hywg.run(/Users/user/Documents/project/projectfolder/app/build.gradle:12)

标准替换很清楚(使用doLast代替<<),但我发现了一些我不知道如何更新的操作(整个项目不仅是我的) .

所以,在插件中,我有一些:

configJSON = mainProperties.getConfig() << buildProperties.getConfig()

imageNames << image.getFileName()

for (int i = 0; i < m.groupCount(); i++) {
    list << m[i][1]
}

等等。目前执行此操作的方法是什么?

Task.leftShift(Closure) - 这是来自 Gradle lib.

的任务 class 的 leftShift()

imageNames << image.getFileName() - 这是集合 class 的 leftShift(),它是 Groovy 语言的一部分。

如果您检查 org.codehaus.groovy.runtime.DefaultGroovyMethods,您会看到:

imageNames << image.getFileName()

相同

imageNames.add(image.getFileName())

好的,我终于在插件项目中找到了这一行。

有下一个代码块:

if(it.hasProperty("android")) { 
    project.task('mainTask') << {
        ...
    }
}

所以解决方案是:

if(it.hasProperty("android")) {
    project.task('mainTask') {
        doLast {
            ...
        }
    }
}

我的错。