Creating dynamic task with custom inputs and outputs

Hi, I’d like to create a dynamic gradle task that has custom inputs and outputs.

For example if i create dynamic task as follows:

def taskBody = {
 println "I'm task body"
}
Task dynamicTask = task "$taskName" << taskBody
dynamicTask.doFirst {
 inputs.dir(new File(projectDir, "src")) // FIXME
  outputs.dir(new File(projectDir, "target")) // FIXME
  println 'doFirst '
 return this
}

then custom inputs and outputs are not taken into consideration and following warning is shown: “Calling TaskInputs.dir(Object) after task execution has started has been deprecated and is scheduled to be removed in Gradle 2.0.” How can i do it properly? -------------------------------------------- Here are some thoughts that didn’t quite do the trick either:

One thing i thought about was to create a custom Task, let’s say:

class MyTask extends DefaultTask {
 private MyTask (String someArg) {
  // do smth with the arg
 }
    public Task doFirst(Action<? super Task> action) {
  Task task = super.doFirst(action);
  inputs.dir(new File(projectDir, "src"))
  outputs.dir(new File(projectDir, "target"))
  println 'doFirst '
  return task
 }
   @TaskAction
 def executeMyTaskBody() {
  println "I'm task body"
 }
}

But i can’t figure out how to instanciate that custom task

new MyTask(someArg) //Task of type 'MyTask' has been instantiated directly which is not supported.
// (probably TaskFactory won't help me either if i want to set custom inputs?)

and add the instance to the project.

Probably i’m just unable to google for smth really trivial, but i don’t have almost any experience with Gradle.

Tnx in advance, Ats

Declaring inputs and outputs is an act of configuration and has to happen in the configuration phase, not in a task action. This not only holds for dynamic tasks, but also for “plain” tasks.

def dynamicTask = task(taskName) {
    println "I'm configuration and evaluated in the configuration phase"
    println "I'm always evaluated, no matter what"
    inputs.dir("src")
    outputs.dir("target")
        doLast {
        println "I'm a task action and evaluated in the execution phase"
        println "I'm only evaluated if and when the task executes"
    }
}

tnx for the answer, i got it working :slight_smile: