Incremental Build

Incremental build task (gradle doc).

Allows to execute for only changed files within the task without having to re-execute the whole task.

abstract class IncrementalReverseTask : DefaultTask() {
  @get:Incremental
  @get:PathSensitive(PathSensitivity.NAME_ONLY)
  @get:InputDirectory
  abstract val inputDir: DirectoryProperty

  @get:OutputDirectory
  abstract val outputDir: DirectoryProperty

  @get:Input
  abstract val inputProperty: Property<String>

  @TaskAction
  fun execute(inputChanges: InputChanges) {
    println(
      if (inputChanges.isIncremental) {
        "Executing incrementally"
      } else {
        "Executing non-incrementally"
      }
    )

    inputChanges.getFileChanges(inputDir).forEach { change ->
      if (change.fileType == FileType.DIRECTORY) return@forEach

      println("${change.changeType}: ${change.normalizedPath}")
      val targetFile = outputDir.file(change.normalizedPath).get().asFile
      if (change.changeType == ChangeType.REMOVED) {
        targetFile.delete()
      } else {
        targetFile.writeText(change.file.readText().reversed())
      }
    }
  }
}

tasks.register<IncrementalReverseTask>("incrementalReverse") {
  inputDir = file("src/inputs")
  outputDir = layout.buildDirectory.dir("outputs")
  inputProperty = project.findProperty("taskInputProperty") as String? ?: "original"
}
Glass thought Sandbox Snapshot

Command to reproduce:

gt.sandbox.checkout.commit 47df08c \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew incrementalReverse"

Recorded output of command:


> Task :lib:incrementalReverse
Executing incrementally
MODIFIED: hi2

BUILD SUCCESSFUL in 361ms
1 actionable task: 1 executed

Backlinks