Similar question about `MapProperty`: would you ra...
# plugin-development
t
Similar question about `MapProperty`: would you rather initialize it with
convention()
or
putAll()
? (my use-case is initializing it with
providers.gradlePropertiesPrefixedBy(…)
) I've found one instance of MapProperty in the Gradle codebase that's not left empty, and it's in the build logic (so not in a "public" plugin), and it uses a few calls to `put()`: https://github.com/gradle/gradle/blob/56ac845808133d19b7e03a3fcc1264f21122e37e/bui[…]/src/main/groovy/gradlebuild/docs/GradleReleaseNotesPlugin.java
m
I'm very mad that
MapProperty
is not unset by default unlike other properties... That being said, and to answer your question, I tend to avoid
convention()
altogether, makes the properties code a bit easier to follow IMO
+ when it comes to
ConfigurableFileCollection
, I have never really managed to wrap my head around it (cf your questions in the other message)
+ some computed defaults might take time and are better run in a task action.
The cases where
convention()
could be useful are • If the user code needs to read the default value • Or because it acts as a self-documenting place to put the default for people reading the plugin code
But overall I have never found a case where #1 was needed and #2 breaks as soon as you need to compute the default in a task action so might as well handle everything consistently 🤷
s
I wanted to use convention for maps but found that it is less confusing to just put things in there and tell the users there are some pre-existing entries that they can remove if needed.
👍 1
t
I'm using an extension to configure default values for tasks though, so I think I'll use putAll to set default values in the extension, and then use convention to link the tasks to the extension. I need to test using task.prop.set(extension.prop) though as I'm not sure how it works.
So
task.prop.set(extension.prop)
followed by
task.prop.put("foo", "bar")
will have
task.prop
contain both the
extension.prop
entries and the
foo: bar
entry. Keys can be added to the extension, the extension map can be emptied, this will be reflected in the task (entries configured on the task will stay on the task) I'll use
.set()
then (in both places: extension and tasks) as that's the behavior I want.
👍 1