Gradle 具有 2 个 ant 目标的任务 - 尝试组合 ant 目标以加快执行时间

Gradle task with 2 ant targets - trying to combine ant targets to speed up execution time

我目前有一个 Gradle 任务使用 2 个 ant 目标(复制和移动)。这 2 个蚂蚁任务给了我正确的结果。但是,我想知道是否可以将它们加入一个 ant 目标以加快执行时间并减少代码量。

我曾尝试使用 2 个映射器来实现,但显然我只能使用一个映射器。我尝试过使用chainedmapper和compositemapper,但结果不是我所期望的。

我想要的

我有一些文件需要更改所有扩展名 (.icls -> .xml),并且我有一个文件想重命名 (ChroMATERIAL IntelliJ IDEA -> ChroMATERIAL)

Starting file name                    final file name
------------------                    ---------------
ChroMATERIAL - Darker.icls        ->  ChroMATERIAL - Darker.xml
ChroMATERIAL - Darcula.icls       ->  ChroMATERIAL - Darcula.xml
ChroMATERIAL IntelliJ IDEA.icls   ->  ChroMATERIAL.xml

下面的代码是我在收到错误消息之前最初尝试的代码。最后的代码是我目前使用的解决方法。

编辑:我更喜欢基于蚂蚁的评分任务。我看过纯基于 ant 的文件,这些文件似乎允许多个映射器,这就是我认为我需要的,但我无法获得类似的工作。

我试过的

task syncFiles {

    doLast {

        // Sync files from IntelliJ's color scheme and rename extension
        ant.copy(todir: sourceDir) {
            ant.fileset(dir: intelliJColorSchemeDir)

            // NOTE: This is an error message. Can only have one mapper!
            ant.mapper(type: "glob", from: "*.icls", to: "*.xml")
            ant.mapper(type: "glob", from: "ChroMATERIAL IntelliJ IDEA.xml", to: "ChroMATERIAL.xml")    
        }
    }
}

我当前的实现

这就是我想用 2 个映射器变成一个 ant 任务。我相信这可以变成更精简的代码,只调用 ant 一次。我也希望这段代码执行得更快一些。

task syncFiles {

    doLast {

        // Sync files from IntelliJ's color scheme and rename extension
        ant.copy(todir: sourceDir) {
            ant.fileset(dir: intelliJColorSchemeDir)
            ant.mapper(type: "glob", from: "*.icls", to: "*.xml")
        }

        // Rename one specific file. I want this mapper to be joined with the above mapper
        ant.move(todir: sourceDir) {
            ant.fileset(dir: sourceDir)
            ant.mapper(type: "glob", from: "ChroMATERIAL IntelliJ IDEA.xml", to: "ChroMATERIAL.xml")
        }
    }
}
task syncFiles(type: Copy) {
    into sourceDir
    with copySpec {
       from intelliJColorSchemeDir
       include '**/ChroMATERIAL IntelliJ IDEA.icls'
       rename '.*', 'ChroMATERIAL.xml'
    }
    with copySpec {
       from intelliJColorSchemeDir
       exclude '**/ChroMATERIAL IntelliJ IDEA.icls'
       rename '(.*)\.icls$', '.xls'
    }
}

在保留原始问题并提供基于 Ant 的解决方案的情况下,以下代码似乎是最干净的。可能还有其他类型的复合映射器性能更好,但这是我能想到的。

    ant.copy(todir: sourceDir) {
        ant.fileset(dir: intelliJColorSchemeDir)
        ant.firstmatchmapper {
            ant.globmapper(from: "ChroMATERIAL IntelliJ IDEA.icls", to: "ChroMATERIAL.xml")
            ant.globmapper(from: "*.icls", to: "*.xml")
        }
    }

我喜欢它的地方。代码非常干净,没有使用正则表达式等奇怪的语法。它还将映射器组合成一个块。最后,它像我最初想要的那样使用 Ant。

执行一些快速测试似乎显示执行时间的变化很小,但我已经用上面的代码报告了这个任务的一些最快执行时间。不知道是侥幸还是稍微快点。

编辑:

这里是仅使用 Gradle 的简洁版本...

into sourceDir
from intelliJColorSchemeDir, {
    rename 'ChroMATERIAL IntelliJ IDEA.icls','ChroMATERIAL.xml'  // Edit: moved to top and changed file extension.
    rename '(.*)\.icls$', '.xml'

}

这段代码实际上每 运行 出现一次就更快。