ConfigurableFileCollection for @InputFileCollection value in custom task

plugins

(Dustin Chesterman) #1

I have a custom task and for one of the inputs I want a single file, but I would like a user to be able to specify anything that resolves to a file with the Project.files(Object o…) method. Here’s what I have at this point :

class MyTask extends DefaultTask {

    private Object cfc

    @TaskAction
    protected void upload() {
        // upload
    }

    @InputFile
    public File getCfcFile() { project.files(cfc).singleFile }

    public void setCfc(Object cfc) {
        this.cfc = cfc
    }
}

With the annotation on the getter it doesn’t find that MyTask depends on cfc if cfc is a task. If I annotate the field and remove the private modifier, it runs the dependency but fails the annotation validation to verify its a File.

Any suggestion here as to how to get this to work?


(Sterling Greene) #2

You could try using a TextResource instead:
https://docs.gradle.org/current/dsl/org.gradle.api.resources.TextResourceFactory.html

class MyTask extends DefaultTask {
   @Nested
   TextResource cfc

    @TaskAction
    protected void upload() {
        // upload using cfcFile instead of cfc
    }

    private File getCfcFile() { cfc.asFile() }
}

That let’s people do:

myTask.cfc = resources.text.fromFile("path/to/somewhere")
   or
myTask.cfc = resources.text.fromFile(someTask)
   or
myTask.cfc = resources.text.fromString("""
   content of this file
""")
   or
myTask.cfc = resources.text.fromArchiveEntry(configurations.sharedConfig, "path/inside/archive")