Gradle - 为自定义javaexec任务添加增量行为

时间:2017-02-22 07:39:04

标签: build.gradle

我的build.gradle文件中有一个javaexec任务,顺序处理超过100个文件。我想为此任务添加增量构建行为。 请告诉我如何为自定义JavaExec任务实施此增量功能。

1 个答案:

答案 0 :(得分:0)

 I have tried the following:

class IncrementalReverseTask extends DefaultTask {
    @InputDirectory
    def File inputDir

    @OutputDirectory
    def File outputDir

    @Input
    def inputProperty

    @TaskAction
    void execute(IncrementalTaskInputs inputs) {
        println inputs.incremental ? "CHANGED inputs considered out of date"
                                   : "ALL inputs considered out of date"
        if (!inputs.incremental)
            project.delete(outputDir.listFiles())

        inputs.outOfDate { change ->
            println "Modified: ${change.file.name}"
            def targetFile = new File(outputDir, change.file.name)
            targetFile.text = change.file.text.reverse()
        }

        inputs.removed { change ->
            println "removed: ${change.file.name}"
            def targetFile = new File(outputDir, change.file.name)
            targetFile.delete()
        }
    }
}

then create a task like this

task incrementalReverse(type: IncrementalReverseTask) {
    inputDir = file('resource/inputs')
    outputDir = file('resource/outputs')
    inputProperty = project.properties['taskInputProperty'] ?: "original"
}

The above incremental task checks for changes in files in inputs folder.