gradletaskincremental-build

Gradle: task with variable inputs


Is it a good idea for a task to get values from project properties or variables that can be changed. For example,

task mergeProperties {

    def propertiesFile = new File(project.ext.file1)
    def propertiesFile2 = new File(project.ext.file2)
    def outputFile = new File(project.ext.file3)

    inputs.files propertiesFile, propertiesFile2
    outputs.file outputFile

    doLast {
            // merge properties
    }
}

This method can be used to merge any two files and write to any file by changing property.ext properties. Is this a good idea? How UP_TO_DATE check works in this case?


Solution

  • Simple answer: No

    As long as you do not provide an example use case or scenario that needs the behaviour described above, I think it is a bad idea to let property files or command line values decide on execution logic. This is what code (in your build.gradle) should be about.

    Also, doLast (and doFirst) closures are for minor preparation and cleanup jobs, the main task actions should be defined by a @TaskAction in the custom task type definition:

    class Merge extends DefaultTask {
        @TaskAction
        def merge() {
            // merge properties
        }
    }
    
    task mergeProperties(type: Merge) {
        inputs.files 'myInputFile1', 'myInputFile2'
        outputs.file 'myOutputFile'
    }
    

    Now, a special scenario (I can't imagine right now) could use project properties to define the input and output files.