Hi everyone,
I’m working on our custom gradle plugins and I’d like to be able to use a rich(er) DSL for configuring aspects of the task like so:
myTask {
taskProperty = "value"
feature1.enabled = false
feature2 {
enabled = true
featureProperty = "someOtherValue"
}
}
The Plugin/Task development documentation is quite fine, but it lacks some details, so I’m having trouble sorting out the APIs that I should use to achieve this. I’ll just write down my thoughts and the question would be: am I right, is this as it’s meant to be used (in the current 1.0-milestone-9 state)? Maybe this topic could meanwhile serve as input for enhancing the documentation.
If I understand the documentation and API right, extensions would be the way to go to add the “feature1” and “feature2” containers to the task. So in the constructor of my task (which extends from AbstractTask), I’d use something like:
this.getExtensions().add("feature1", new Feature1Container());
.
So far so good. Now, all of the task’s properties actually have reasonable defaults, so for each property, I’d like to define a convention mapping so that projects don’t have to configure each property, in the spirit of convention-over-configuration.
For “direct” task properties, I can write the following in the plugin:
myTask.conventionMapping("taskProperty", new Callable<PropertyType>() {
public PropertyType call() throws Exception {
// This might even be dynamic or computed, not necessarily static.
return someDefaultValue;
}
});
However, for the nested containers, this seems more complicated, as the “Feature1Container” and “Feature2Container” classes are only plain java-beans at this point. To be able to define convention mappings on them, they’d have to implement IConventionAware. However, there are 2 problems here: - First, IConventionAware is a Gradle internal API, so I guess I shouldn’t rely on it - Second, if I decorate my container javabeans with IConventionAware by hand, I’d have to write all special getters, which first check if the property is set and use the convention value as fallback. Writing that by hand just seems horribly wrong.
I must have taken a wrong turn somewhere I guess? What would be the correct path to success here?
Thanks in advance, Mike