Delete missing files for custom incremental task with InputFiles and no InputDirectory

I have a custom incremental build task that signs all jar dependencies for my Java project. It looks like this:

task signJarDependencies(type: IncrementalSignJarTask) {
    inputFiles = configurations.deployment #<-- key line to notice
    outputDir = webstartLibDir
    inputProperty = 'original'
}

class IncrementalSignJarTask extends DefaultTask {

    @InputFiles
    def FileCollection inputFiles

    @OutputDirectory
    def File outputDir

    @Input
    def inputProperty

    @TaskAction
    void execute(IncrementalTaskInputs inputs) {
        logger.info inputs.incremental ? "CHANGED inputs considered out of date" : "ALL inputs considered out of date"
        List filesChanged = []
        inputs.outOfDate { change ->
            filesChanged << change.file
        }
        GParsPool.withPool {
            filesChanged.eachParallel { f ->
                project.ibSignJar(f,outputDir)
            }
        }
        inputs.removed { change ->
            logger.info "removed: ${change.file.name}"
            def targetFile = new File(outputDir, change.file.name)
            targetFile.delete()
        }
    }
}

I would like jars that are no longer in the dependency list to be considered “removed” by the incremental task and deleted. I know if I use @InputDirectory this will happen, but that won’t work well since I’m getting the list of jars from the configuration instead of a directory… and of course the jars will be coming from different directories anyway since they are defined by the dependency system and cached somewhere on the local computer.

Is there a clean idiomatic Gradle way to do this?

Thanks!

Not really. As I see it, your options are to a) derive which input jars map to which output jars and explicitly delete the ones in the output directory that don’t belong (if you aren’t renaming the files this should be simple) or b) create an intermediary Sync task that copies all your dependencies to a directory and use that directory as the input to the signing task.

1 Like