How do you use objects in extensions?


The extension example in the “Writing Custom Plugins” chapter in the user guide shows how you can use an extension that contains String fields.

I’m trying to use an extension that contains a list of objects instead of simple fields, but I’m missing something.

My extension would be something like this:

class MyExtension {
  List<MyMessage> messages
  class MyMessage {
  String text
  String[] targets

How would the extensions’ closure in my build.gradle look like? How does the info from the closure end up in my extension?

Kind regards,


In case someone else is wondering about this, the Gradle Beyond the Basics book has a clear example. See the Extensions section in chapter 2 “Custom Plugins”.

Basically, you have to use NamedDomainObjectContainer to define collections of your own domain objects.

Kind regards, Glenn