Plugin and delegation to the project for methods and properties


I’m looking for advices about plugins implementation.

Is it a good practice :

  1. 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

  2. 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) {
			} else {

	// access to the methods of the Project
	@Delegate private Project project
	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 !


  1. Those methods are already available on the Project instance. You can directly call project.file(...) or project.copy(). IMHO it’s a bad practice to use @Delegate here.
  2. The same applies here. You can just call project.getProperty(...).

I’d highly recommend reading the guides on plugin development if you haven’t yet. They give good guidance and examples on what to do and what not to do.

You can just use groovy’s with closure. Eg:

project.with {
   task foo {
      doLast { ... }