Hello,
I’m looking for advices about plugins implementation.
Is it a good practice :
-
to delegate to the project (with the annotation
groovy.lang.Delegate
), in order to be able to call directly methods like file(), copy(), … in the plugin like in the build.gradle -
to override the
Plugin.metaclass.getProperty
for access to standard extensions of the project from the plugin
The skeleton of the Plugin would be the following:
public class MyPlugin implements Plugin<Project> {
// if the property is not defined in the plugin, delegate to the project
static {
MyPlugin.metaClass.getProperty = { String propName ->
def meta = MyPlugin.metaClass.getMetaProperty(propName)
if (meta) {
meta.getProperty(delegate)
} else {
delegate.project.getProperty(propName)
}
}
}
// access to the methods of the Project
@Delegate private Project project
@Override
public void apply(Project project) {
this.project = project
// method file() of the project is directly accessible
File configFile = file("${projectDir}/config.txt")
// idem for property group of the project (via getter/setter)
group = 'com.myplugin'
// standard extention of the project is also directly accessible
ext.MY_VAR = 'xxxx'
}
}
Is there another way more recommended to achieve the same thing ?
Thank you for your opinion !
Eric