Correct way to define a Gradle plugin property extension with Java? -


i'm trying create gradle plugin in java has property extensions (not conventions, apparently old, wrong way). record, i'm working gradle 1.6 on linux machine (ubuntu 12.04).

i've gotten far figuring out should done in plugin class definition. here way of adding extension. create extension class contains properties:

public class mypluginextensions {      file sourcedir;     file outputdir;      public mypluginextensions(project project) {         this.project = project;         sourcedir = new file(project.getprojectdir(), "src");         outputdir = new file(project.getbuilddir(), "out");     }  } 

now add these extensions project in main plugin class:

public class myplugin implements plugin<project> {      @override     public void apply(project project) {          map<string,object> taskinfo = new hashmap<string,object>();         taskinfo.put("type", myplugintask.class);         taskinfo.put("description", "generates blah blah.");         taskinfo.put("group", "blah");         task myplugintask = project.task(taskinfo, "myplugintask");          // define conventions , attach them tasks         mypluginextensions extensions = new mypluginextensions(project);         myplugintask.getextensions().add(                 "sourcedir",                  extensions.sourcedir);         myplugintask.getextensions().add(                 "outputdir",                  extensions.outputdir);     } } 

this approach, however, doesn't seem correct. new project property shows in project.ext namespace. expect able address plugin extensions as:

in build.gradle: myplugintask.sourcedir = file('the/main/src') myplugintask.outputdir = file('the/output') 

however, when put such things in gradle script uses plugin , try set property, gradle tells me can't set it:

* went wrong: problem occurred evaluating script. > there's extension registered name 'sourcedir'. should not reassign via property setter. 

so, what's right way add property extensions task in java-based gradle plugin?

edit:

based on other posts, tried adding extensions object in 1 shot:

// first attempt: //myplugintask.getextensions().add("sourcedir", extensions.sourcedir); //myplugintask.getextensions().add("outputdir",extensions.outputdir);  // second attempt myplugintask.getextensions().add("myplugintask", extensions); 

this appears work. however, gradle complaining i've added dynamic property:

deprecated dynamic property: "sourcedir" on "task ':myplugintask'", value: "/users/jfer...". 

so, again, what's right way add plugin extension property?

edit 2

so, taking yet shot @ this, i'm adding extension project object , using create method instead:

// first attempt: //myplugintask.getextensions().add("sourcedir", extensions.sourcedir); //myplugintask.getextensions().add("outputdir",extensions.outputdir);  // second attempt // myplugintask.getextensions().add("myplugintask", extensions);  // third attempt project.getextensions().create("myplugintask", mypluginextensions.class, project); 

however, fails couple of reasons:

  1. creating properties extension same name ("myplugintask") task creates collision between task name , extension name, causing task disappear gradle's perspective (and throw oblique errors, such "no such property: dependson class ...mypluginextensions").
  2. if provide name not collide task name (e.g., "mypluginpropext"), create() method works, not add extension in own namespace expected (e.g., project.mypluginpropext.propertyname , instead adds in project namespace (e.g., project.propertyname) not correct , causes gradle throw bunch of "deprecated dynamic property" warnings.

so here a solution problem:

public class myplugin implements plugin<project> {      @override     public void apply(project project) {          map<string,object> taskinfo = new hashmap<string,object>();         taskinfo.put("type", myplugintask.class);         taskinfo.put("description", "generates blah blah.");         taskinfo.put("group", "blah");         task myplugintask = project.task(taskinfo, "myplugintask");          // define conventions , attach them tasks         mypluginextensions extensions = new mypluginextensions(project);          // magic extension code:         project.getextensions().add("mypluginname", extensions);     } } 

now can set value 1 of extension properties in gradle.build file (and don't warning adding deprecated dynamic properties):

 mypluginname.sourcedir = file('the/main/src') 

the final trick value in plugin's task:

public class myplugintask extends defaulttask {     @taskaction     public void action() {         mypluginextensions extensions = (mypluginextensions) getproject()                 .getextensions().findbyname("mypluginname");          system.out.println("sourcedir value: " + extensions.sourcedir);     } } 

this works, annoys me solution want able put extension properties in same namespace task (e.g., myplugintask.sourcedir) have seen in groovy-based plugins, apparently not supported or doesn't work.

in meantime, hope helps else.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -