# Extensions on tasks

**URL:** <https://discuss.gradle.org/t/extensions-on-tasks/46095>\
**Category:** Help/Discuss\
**Created:** [July 22, 2023, 11:51pm UTC](https://discuss.gradle.org/t/extensions-on-tasks/46095 "2023-07-22T23:51:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelvSYC](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kelvSYC](https://discuss.gradle.org/u/kelvSYC)\
**Post date:** [July 22, 2023, 11:51pm UTC](https://discuss.gradle.org/t/extensions-on-tasks/46095/1 "2023-07-22T23:51:25Z")

</div>

Suppose I have a task of an existing third-party type for which you can’t supply all the inputs right away, but you can with an extension. Something like

Third-party:

```gradle
interface MyTaskExtension {
    val flag: Property<Boolean>
}

abstract class MyTask : DefaultTask() {
    abstract val parameter: Property<String>
}

tasks.register<MyTask>("myTask")

```

Somewhere in my code:

```gradle
tasks.named<MyTask>("myTask").configure {
    val extension = extensions.create<MyTaskExtension>("myExtension")
}

```

Elsewhere in my code:

```gradle
tasks.named<MyTask>("myTask").configure {
    parameter.set(the<MyTaskExtension>().flag.map { if (it) "foo" else "bar" })
}

```

Suppose that I have applied the third party plugin, and both of my custom configuration blocks have also been applied. Is there any guarantee that the first configuration block that defines the extension will be applied before the second configuration block, or will the second configuration block need to defer the extension lookup until the task is running? (Is that even configuration cache compatible?)

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [July 25, 2023, 8:18pm UTC](https://discuss.gradle.org/t/extensions-on-tasks/46095/2 "2023-07-25T20:18:00Z")

</div>

You should definitely not change the configuration at execution time. Even without configuration cache this is very bad and flaky at best.

The order of the blocks is the order you registered them. So your logic should make sure your first block is executed first, then you are fine. Or you try to drag the two together to one block if you can.

---

<div class="post-metadata">

**Author:** ![kelvSYC](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kelvSYC](https://discuss.gradle.org/u/kelvSYC)\
**Post date:** [July 26, 2023, 3:49am UTC](https://discuss.gradle.org/t/extensions-on-tasks/46095/3 "2023-07-26T03:49:35Z")

</div>

I meant the second block being something like

```gradle
tasks.named<MyTask>("myTask").configure {
    parameter.set(project.providers.provider {
       the<MyTaskExtension>().flag.map { if (it) "foo" else "bar" }
    })
}

```

Would that work in case the second block somehow gets added before the first, or would that be a bit redundant?

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [July 30, 2023, 2:22pm UTC](https://discuss.gradle.org/t/extensions-on-tasks/46095/4 "2023-07-30T14:22:10Z")

</div>

Maybe, I’m not sure. But maybe you should just register the extension if it is not registered yet.
