# Is Gradle supposed to create the container configuration method?

**URL:** <https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853>\
**Category:** Help/Discuss\
**Created:** [November 20, 2017, 3:27pm UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853 "2017-11-20T15:27:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddimitrov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ddimitrov/32/141_2.png) [@ddimitrov](https://discuss.gradle.org/u/ddimitrov)\
**Post date:** [November 20, 2017, 3:27pm UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853/1 "2017-11-20T15:27:03Z")

</div>

I am under the impression that for objects instantiated by the Gradle instantiator (tasks, extensions, etc), whenever we declare a property like this:

```
NamedDomainObjectContainer<T> foo

```

Gradle would automatically create a method like this

```
void foo(Closure c) { foo.confiugure(c) }

```

I tried that and as of Gradle 4.3.1 it seems there is no such method so I wrote my own.

Am I doing something wrong? Do you need sample code?

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [November 20, 2017, 3:56pm UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853/2 "2017-11-20T15:56:29Z")

</div>

This is not auto-generated from the property, but from a method that takes an `Action`

```gradle
void foo(Action config) {
  config.execute(foo)
}

```

---

<div class="post-metadata">

**Author:** ![Schalk\_Cronje](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/schalk_cronje/32/6511_2.png) [@Schalk\_Cronje](https://discuss.gradle.org/u/Schalk_Cronje)\
**Post date:** [November 21, 2017, 11:27am UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853/3 "2017-11-21T11:27:54Z")

</div>

Do you always have to coerce a `Closure` into an `Action` when using the Groovy DSL in the above context?

---

<div class="post-metadata">

**Author:** ![ddimitrov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ddimitrov/32/141_2.png) [@ddimitrov](https://discuss.gradle.org/u/ddimitrov)\
**Post date:** [November 21, 2017, 1:47pm UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853/4 "2017-11-21T13:47:00Z")

</div>

Thanks, I see that the closure gets coerced to action and receives the domain object as argument, but this is not the same as using the `configure {...}` method. In particular, it appears that the closure delegate is not set or at least IDEA does not recognize the container DSL inside the closure.

In other words, instead of doing:

```
foo.configure {
   barBaz { dux=1 } 
   bazQux { dux=1 }
}

```

I need to do:

```
foo { c ->
   c.create('barBaz') { dux=1 } 
   c.create('bazQux') { dux=1 }
}

```

Is that right?

Also, where can I see what kind of decorations does the Gradle instantiator add to my objects?  
(If there is no documentation, could you point me to the code that does that?)

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [November 21, 2017, 5:09pm UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853/5 "2017-11-21T17:09:13Z")

</div>

The object that has the container method needs to be created through the `ObjectFactory`, otherwise the Closure-taking method will not be generated. Then you can simply do

```gradle
foo {
  barBaz { dux=1 } 
}

```

---

<div class="post-metadata">

**Author:** ![ddimitrov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ddimitrov/32/141_2.png) [@ddimitrov](https://discuss.gradle.org/u/ddimitrov)\
**Post date:** [November 23, 2017, 2:13pm UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853/6 "2017-11-23T14:13:32Z")

</div>

The object is being created as an extension, doesn’t that use the `ObjectFactory` internally?

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [November 24, 2017, 9:44am UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853/7 "2017-11-24T09:44:14Z")

</div>

Yes, so that should just work. Do you have some example code showing that the above script doesn’t work?

---

<div class="post-metadata">

**Author:** ![ddimitrov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ddimitrov/32/141_2.png) [@ddimitrov](https://discuss.gradle.org/u/ddimitrov)\
**Post date:** [November 25, 2017, 2:40pm UTC](https://discuss.gradle.org/t/is-gradle-supposed-to-create-the-container-configuration-method/24853/8 "2017-11-25T14:40:52Z")

</div>

Right now I am using the non-dsl style in my build, so I don’t have an example handy. I didn’t really try whether the DSL worked as at that time there were other problems in the code, but it was red in IntelliJ editor - there is a chance that it was an IDE problem.

Will try again later and if necessary post example code here or a link to YouTrack issue.
