# Extension configuration time vs. Task creation time - parameterizing a custom task's instance with extension properties

**URL:** <https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188>\
**Category:** Old Forum Archive\
**Created:** [November 9, 2012, 12:29pm UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188 "2012-11-09T12:29:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaus\_illusioni](https://avatars.discourse-cdn.com/v4/letter/k/f05b48/32.png) [@klaus\_illusioni](https://discuss.gradle.org/u/klaus_illusioni)\
**Post date:** [November 9, 2012, 12:29pm UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/1 "2012-11-09T12:29:00Z")

</div>

Hi,

Writing custom gradle plugins for my company’s project builds I have come across the same issue several times now and never found an answer searching:

I have a plugin that adds stuff to the project: + an extension + an instance of a custom task

The custom task is defined in a dedicated class. It’s built to be reusable. For use in my plugin I want to set some of its properties based on how the extension is configured. Unfortunately the task initialization closure gets executed with applyPlugin(), which is before the extension could be configured by the user.

What is a good (concise, readable) way to parameterize the task with the extension’s properties?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![klaus\_illusioni](https://avatars.discourse-cdn.com/v4/letter/k/f05b48/32.png) [@klaus\_illusioni](https://discuss.gradle.org/u/klaus_illusioni)\
**Post date:** [November 9, 2012, 12:32pm UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/2 "2012-11-09T12:32:00Z")

</div>

Here’s an example buildscript:

```gradle
apply plugin: 'tomcat'
  tomcat {
    //...
    startupValidationUrl = "http://localhost:8080/status"
    startupValidationRegex = /(?i).*overall result:.*ok.*/
    startupValidationTimeout = 30
}

```

---

<div class="post-metadata">

**Author:** ![klaus\_illusioni](https://avatars.discourse-cdn.com/v4/letter/k/f05b48/32.png) [@klaus\_illusioni](https://discuss.gradle.org/u/klaus_illusioni)\
**Post date:** [November 9, 2012, 12:43pm UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/3 "2012-11-09T12:43:00Z")

</div>

Here’s an excerpt from my custom plugin:

```gradle
void apply(Project project) {
        this.project = project
        project.extensions.create('tomcat', TomcatExtension, project)
        //...
        project.task ([type: HttpStatusTask, dependsOn: 'startTomcat'], 'validateStartup') {
            url = project.extensions.tomcat.startupValidationUrl
            timeout = project.extensions.tomcat.startupValidationTimeout
            validationRegex = project.extensions.tomcat.startupValidationRegex
        }

```

At the time when the plugin is applied the extension has not yet configured by the buildscript.

---

<div class="post-metadata">

**Author:** ![strindberg](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@strindberg](https://discuss.gradle.org/u/strindberg)\
**Post date:** [November 9, 2012, 3:46pm UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/4 "2012-11-09T15:46:00Z")

</div>

As I understand it, the easiest way to configure this is to use a closure. Change the lines in your plugin to something like this:

```gradle
url = { project.extensions.tomcat.startupValidationUrl }
 timeout = { project.extensions.tomcat.startupValidationTimeout }
 validationRegex = { project.extensions.tomcat.startupValidationRegex }

```

Then you rewrite your custom task to receive a closure in it’s setter method and resolve the closure in the execution phase, i.e. the @TaskAction method.

---

<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:** [November 9, 2012, 5:29pm UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/5 "2012-11-09T17:29:00Z")

</div>

It’s an often discussed topic in the forum. For example: [http://forums.gradle.org/gradle/topics/custom\_task\_with\_fields\_assign\_directly\_or\_via\_conventionmapping](http://forums.gradle.org/gradle/topics/custom_task_with_fields_assign_directly_or_via_conventionmapping)

---

<div class="post-metadata">

**Author:** ![Todd\_Ogin](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Todd\_Ogin](https://discuss.gradle.org/u/Todd_Ogin)\
**Post date:** [January 31, 2013, 5:41am UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/6 "2013-01-31T05:41:00Z")

</div>

I realize this is an old thread, but do you think you can point to an example of the closure resolution?

---

<div class="post-metadata">

**Author:** ![Todd\_Ogin](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Todd\_Ogin](https://discuss.gradle.org/u/Todd_Ogin)\
**Post date:** [January 31, 2013, 5:58am UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/7 "2013-01-31T05:58:00Z")

</div>

Say I wanted to add a plugin with a custom task that extended Exec:

[http://www.gradle.org/docs/current/javadoc/org/gradle/api/tasks/Exec.html](http://www.gradle.org/docs/current/javadoc/org/gradle/api/tasks/Exec.html)

The default @TaskAction for Exec is a private method from what I can tell from the source:

[https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/groovy/org/gradle/api/tasks/Exec.java](https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/groovy/org/gradle/api/tasks/Exec.java)

‘’’ @TaskAction

void exec() {

execResult = execAction.execute();

} ‘’’

Since it is private, creating a new TaskAction and calling it is not possible. The execAction object is private as well, so the method is not able to just be rewritten in the extending class.

I’m not exactly sure how the solution posted above helps me since I can’t put additional code in the TaskAction and still execute the intended code without reimplementing large parts of the Exec class.

---

<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:** [February 1, 2013, 1:18am UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/8 "2013-02-01T01:18:00Z")

</div>

Most Gradle tasks aren’t designed to be subclassed. Why do you want to subclass ‘Exec’? Do you really want your task to expose all of ‘Exec’'s properties, or do you just want to make your task execute something? In the latter case, you should use ‘Project.exec’.

---

<div class="post-metadata">

**Author:** ![Todd\_Ogin](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Todd\_Ogin](https://discuss.gradle.org/u/Todd_Ogin)\
**Post date:** [February 2, 2013, 2:54pm UTC](https://discuss.gradle.org/t/extension-configuration-time-vs-task-creation-time-parameterizing-a-custom-tasks-instance-with-extension-properties/7188/9 "2013-02-02T14:54:00Z")

</div>

I didn’t realize Project.exec existed and that certainly seems like it would be an easier alternative to what I was trying to do. Thanks for the pointer.
