I have developed a small plugin for our group, and everything works well. However, I found the configuration of the plugin a bit cumbersome, and I am thinking perhaps I have missed some best practice.
In my “build.gradle” I have something like this:
apply plugin: 'sonic'
...
sonic {
containerName = 'OurContainer'
}
Then, in my plugin code, developed as a separate project, I have the following:
project.extensions.sonic = new SonicPluginExtension()
class SonicPluginExtension {
def containerName = null
def sonic(Closure closure) {
closure.delegate = this
closure()
}
}
...
project.task('sonicimportservicetypes', type: ServiceTypeImportTask) {
sonicContainerName = { project.sonic.sonicContainerName }
}
What bothers me with the above is the closure around the “project.sonic.sonicContainerName” in the task configuration. It took me a while to figure out that it was required. I have found no easier way to make sure the configuration is delayed to the right moment, is this really the best way of doing it? It seems a little bit too indirect.
At first I naively thought that the build.gradle would have been read when the plugin code was read, but that doesn’t seem to be the case, hence my indirect reading of the settings via a closure.
Is there an easier/better solution to what I try to accomplish?