Incremental task with output depending on input


(Thomas) #1

Hello, I have another question concerning incremental tasks.
I have a task that runs perfectly based on its inputs :

class GenerateTextTask extends DefaultTask {
    @InputFiles
    def File[] txtDir = new File(txtPath).listFiles(new FilenameFilter() {
        @Override
        boolean accept(File dir, String name) {
            return name.endsWith(".txt")
        }
    })

    @Input
    def langs = "en"

    @OutputFiles
    def List<File> xmlFiles = langs.split(" ").collect {lang ->
        new File("output/${lang}.xml")
    }

But the problem here is if I extends this tasks :

task hello(type: GenerateTextTask) {
    langs = "en fr"
}

the output is only output/en.xml (because langs as not be injected yet).

What can I do to solve this issue ?


(uklance) #2

Change it from a field to a getter

@OutputFiles 
List<File> getXmlFiles() { ... } 

Also, don’t use new File(...) use project.file(...) instead


(Thomas) #3

Thank you it works perfectly (after 1 hour of trial and error) !!