Skip to content

Instantly share code, notes, and snippets.

@ghale
Last active August 3, 2021 18:23
Show Gist options
  • Save ghale/a7d8690ab0b824ee72ff845c4cef5802 to your computer and use it in GitHub Desktop.
Save ghale/a7d8690ab0b824ee72ff845c4cef5802 to your computer and use it in GitHub Desktop.
Removing empty kapt output directory created at configuration time
gradle.taskGraph.whenReady { graph ->
graph.beforeTask { task ->
if (task instanceof org.jetbrains.kotlin.gradle.internal.KaptTask) {
if (task.destinationDir.exists()) {
if (task.destinationDir.listFiles().length == 0) {
logger.warn("Cleaning empty output directory at ${task.destinationDir}")
assert task.destinationDir.delete()
} else {
logger.warn("Not cleaning output directory at ${task.destinationDir} as it exists and is not empty. See the '${task.path}.destinationDir' custom value to see contents.")
captureOutputDirContents(task)
}
} else {
logger.warn("Not cleaning output directory at ${task.destinationDir} as it does not exist.")
}
}
}
}
def captureOutputDirContents(Task task) {
def contents = []
rootProject.fileTree(task.destinationDir).visit { contents << it.relativePath }
rootProject.buildScan.value("${task.path}.destinationDir", contents.join("\n"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment