# Extension initialization vs task configuration

**URL:** <https://discuss.gradle.org/t/extension-initialization-vs-task-configuration/36118>\
**Category:** Help/Discuss\
**Created:** [May 16, 2020, 2:04pm UTC](https://discuss.gradle.org/t/extension-initialization-vs-task-configuration/36118 "2020-05-16T14:04:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vboriesazeau](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vboriesazeau](https://discuss.gradle.org/u/vboriesazeau)\
**Post date:** [May 16, 2020, 2:04pm UTC](https://discuss.gradle.org/t/extension-initialization-vs-task-configuration/36118/1 "2020-05-16T14:04:28Z")

</div>

Hi,

I’m working on a plugin with its own DSL extension. When the plugin is applied to the project (`Plugin.apply` method), the extension is created, and some tasks are registered. Each task is registered with the `project.register(<taskName>, <taskClass>, <configurationAction>)` method. Each `<configurationAction>` configures the task based on the extension. Since configuration applies after evaluation, I was expecting the extension to be initialized at this moment.

The problem: depending on the content in the `build.gradle` file, each `<configurationAction>` may receive an instance of the extension that is not yet initialized. The value of each property providers in the extension may not reflect the value of the property configured in the `build.gradle` file. Sometimes, the extension providers are initialized, sometimes they are not.

Q1 - When exactly is an extension initialized based on the content of the `build.gradle` file (referring to evaluation/configuration/execution phases and project/plugin/tasks lifecycle) ?  
Q2 - What can I do to perform additional configuration actions on tasks with an initialized extension, in a reliable way?

Thanks in advance for your help!  
BR,  
Vincent

---

<div class="post-metadata">

**Author:** ![jjustinic](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jjustinic/32/9005_2.png) [@jjustinic](https://discuss.gradle.org/u/jjustinic)\
**Post date:** [May 17, 2020, 5:35am UTC](https://discuss.gradle.org/t/extension-initialization-vs-task-configuration/36118/2 "2020-05-17T05:35:54Z")

</div>

> [@vboriesazeau](#):
>
> depending on the content in the `build.gradle` file, each `<configurationAction>` may receive an instance of the extension that is not yet initialized. The value of each property providers in the extension may not reflect the value of the property configured in the `build.gradle` file.

This should not matter in a properly designed implementation. The `configurationAction` that runs should be wiring things up, but it should not be looking for the exact values to be known yet. For example, take the following plugin code:

```groovy
class MyExtension {
    Property<String> someProperty
}

class MyTask extends DefaultTask {
    Property<String> someProperty

    @TaskAction
    void printProperty() {
        println someProperty.get()
    }
}

class MyPlugin implements Plugin<Project> {
    void apply(Project project) {
        MyExtension extension = project.extensions.create("myExtension", MyExtension)

        project.register("myTask", MyTask) { task ->
            task.someProperty.set(extension.someProperty)
        }
    }
}

```

The configuration action wires up the extension property to the task property, but it does not cause the value to be evaluated yet. Now let’s use the plugin:

```groovy
apply plugin: MyPlugin

myTask.get().group = "eager" // configuration action will run here due to myTask.get()

myExtension {
    someProperty = "Hello world!"
}

```

This will still work (prints `Hello world!` when running `gradle myTask`) because even though the `configurationAction` runs on the second line of code before the extension’s `someProperty` has been set, the calculation of the value does not occur until execution time in the `TaskAction`.

What you don’t want to do is force the calculation of the value in the `configurationAction`. i.e. if the task property was declared a `String` and not a `Property<String>`:

```groovy
project.register("myTask", MyTask) { task ->
    task.someProperty = extension.someProperty.get()
}

```

This would throw an `IllegalStateExeception` with the build shown.

---

<div class="post-metadata">

**Author:** ![vboriesazeau](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vboriesazeau](https://discuss.gradle.org/u/vboriesazeau)\
**Post date:** [May 17, 2020, 9:14am UTC](https://discuss.gradle.org/t/extension-initialization-vs-task-configuration/36118/3 "2020-05-17T09:14:18Z")

</div>

Hi James,

> The `configurationAction` that runs should be wiring things up, but it should not be looking for the exact values to be known yet.

This is exactly the detail I was missing, and I could not find it neither in the documentation on lazy configuration, nor in the API. I would suggest at least the API mentions what can/shall be done in the configuration action of the `TaskContainer.register` method.

Thanks a lot for your help!  
BR,  
Vincent
