Lucas Holden
08/13/2025, 7:42 PMMapProperty<String, List<Object>>
in my plugin extension. I need another plugin to merge items into the map instead of overwriting it like put
does.
I thought this would work:
extMap.putAll(extMap.map { m ->
m.keys.forEach { k ->
m.merge(k, myValues(), { l1, l2 -> l1 + l2 })
}
return@map m
})
but instead it causes a stack overflow error due to the value depending on itself.
I also can't add elements in Project.afterEvaluate
because MapProperty#get
returns an ImmutableMap.
How do I add items to the lists in the map?Adam
08/13/2025, 8:04 PM// Holder for `List<Object>`
abstract class MyItemHolder(private val name: String) : Named {
abstract val items: ListProperty<Any>
override fun getName(): String = name
}
// create demo item holders
val items1 = objects.domainObjectContainer(MyItemHolder::class)
items1.create("id1") { items.set(listOf("foo")) }
val items2 = objects.domainObjectContainer(MyItemHolder::class)
items2.create("id1") { items.set(listOf("bar")) }
// merge items2 into items1
items2.all {
items1.maybeCreate(name).items.addAll(items)
}
println("items1: ${items1.map { "${it.name}=${it.items.orNull}" }}")
// items1: [id1=[foo, bar]]
Vampire
08/13/2025, 8:10 PMgetName
and name
explicitly, do you?
I think the Gradle-magic should do it automatically for you.Lucas Holden
08/14/2025, 3:09 AM