Is Gradle supposed to create the container configuration method?


(Dimitar Dimitrov) #1

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?


(Stefan Oehme) #2

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

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

(Schalk Cronjé) #3

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


(Dimitar Dimitrov) #4

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?)


(Stefan Oehme) #5

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

foo {
  barBaz { dux=1 } 
}

(Dimitar Dimitrov) #6

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


(Stefan Oehme) #7

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


(Dimitar Dimitrov) #8

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.