# Creating dynamic task with custom inputs and outputs

**URL:** <https://discuss.gradle.org/t/creating-dynamic-task-with-custom-inputs-and-outputs/4910>\
**Category:** Old Forum Archive\
**Created:** [December 20, 2013, 1:11am UTC](https://discuss.gradle.org/t/creating-dynamic-task-with-custom-inputs-and-outputs/4910 "2013-12-20T01:11:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ats\_Uiboupin](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Ats\_Uiboupin](https://discuss.gradle.org/u/Ats_Uiboupin)\
**Post date:** [December 20, 2013, 1:11am UTC](https://discuss.gradle.org/t/creating-dynamic-task-with-custom-inputs-and-outputs/4910/1 "2013-12-20T01:11:00Z")

</div>

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:

```gradle
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:

```gradle
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

```gradle
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

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [December 20, 2013, 1:55am UTC](https://discuss.gradle.org/t/creating-dynamic-task-with-custom-inputs-and-outputs/4910/2 "2013-12-20T01:55:00Z")

</div>

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.

```gradle
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"
    }
}

```

---

<div class="post-metadata">

**Author:** ![Ats\_Uiboupin](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Ats\_Uiboupin](https://discuss.gradle.org/u/Ats_Uiboupin)\
**Post date:** [December 20, 2013, 6:33pm UTC](https://discuss.gradle.org/t/creating-dynamic-task-with-custom-inputs-and-outputs/4910/3 "2013-12-20T18:33:00Z")

</div>

tnx for the answer, i got it working 🙂
