How to set Input and Output fields on a custom task after creating task with project.task?


(Paul C) #1

I’m trying to make my project more scalable by building “composite” tasks from other tasks. The problem is I cant seem to set fields on tasks created with project.task programatically (there must be a way).

The SimpleInstall task needs to configure the Download.srcUrl and Download.destFile from its constructor (see “TODO” below).

class Download extends DefaultTask {
    @Input
    String srcUrl
    @OutputFile
    File destFile
    @TaskAction
    void download() {
       ant.get(src: srcUrl, dest: destFile)
    }
}
  class SimpleInstall extends DefaultTask {
  SimpleInstall() {
    def download = project.task('Download'){
 //TODO - how to set the @Input Download.srcUrl property ???
        // this doesn't work
        srcUrl = "test"
    }
    this.dependsOn(download)
  }
   @TaskAction
  def install() {
  }
}

(Peter Niederwieser) #2

Tasks cannot be composed. Instead, you can write a plugin that introduces multiple tasks and adds the necessary task dependencies.


(Paul C) #3

Thanks Peter. I considered adding an extenstion that bundled the required input so the task defined from the plugin would have everything it needs to initialize the dependent tasks but I really need a way for users to call it from their build.gradle multiple times with different input and add their own customizations rather than just calling the task from the command line and having the plugin do everything. Is there a best practice here? I’m starting to think it might be better to compose gradle incremental tasks from ant tasks and use ant dependency management to track if input/outputs.

build.gradle

task simpleInstall1(type: SimpleInstall){
 srcUrl = "http://test1"
 destFile = file("xyz")
}
task simpleInstall2(type: SimpleInstall, dependsOn: simpleInstall1){
 srcUrl = "http://test2"
 destFile = file("123")
}