How can I use a Polymorphic Domain Object Container in a custom plugin extension?


I’m developing a plugin (using Gradle 4.6 but I can migrate) and would like to use a PolymorphicDomainObjectContainer in my custom plugin extension to name individual source files, or set of source files. For example

myExtension {
  sources {
    source1 SingleSource {
      file = "src/file1.txt"
      propertySpecificToSingleSource = true
    source2 SourceSet {
      files = ["src/file2.txt", "src/file3.txt"]
      propertySpecificToSourceSet = true

I saw that issue is marked Won’t Fix:

What is the easiest way to define, use, configure, a PolymorphicDomainObjectContainer ?


+1. I would like to do the very same thing in a custom plugin. I can create a NamedDomainObjectContainer no problem (Project#container). But I have no idea how to get a reference to a PolymorphicDomainObjectContainer. Is there some magic like all NamedDomainObjectContainer impls are also PolymorphicDomainObjectContainer impls?

1 Like

BTW, you’ve probably already found this but…

PolymorphicDomainObjectContainer myContainer = project.getObjects().polymorphicDomainObjectContainer( MyContainedThings.class )
// register factories..

I still have not figured out how to actually use these things “properly” as they are documented in the guides for Gradle’s own PolymorphicDomainObjectContainer (tasks, etc). The syntax is supposed to be:

myContainer {
   standard {...}
   custom( CustomType ) {...}

For me, the custom( CustomType ) reference always fails - it tries to find a method named custom. Ugh