Can gradle extensions handle lazy evaluation of a property? -


i'm writing custom gradle plugin handle vaguely complicated work , have run frustrating problem while using properties configure of tasks plugin applies.

apply plugin: myplugin  //provide properties applied plugin mypluginprops {     message = "hello" }  //define task uses custom task directly task thistaskworksfine(type: mytask) {     input = mypluginprops.message }  //define plugin apply task of custom type class myplugin implements plugin<project> {     void apply(project project) {         project.extensions.create('mypluginprops', mypluginextension)          project.task(type: mytask, 'thistaskworksincorrectly') {             input = project.mypluginprops.message         }     } }  //the extension used custom plugin input class mypluginextension {     def string message }  //the task used both standard build section , plugin class mytask extends defaulttask {     def string input      @taskaction     def action() {         println "you gave me this: ${input}"     } } 

the results using file follows:

$ gradle thistaskworksfine thistaskworksincorrectly :thistaskworksfine gave me this: hello :thistaskworksincorrectly gave me this: null  build successful 

i consider unexpected. mind, applying task plugin , writing 1 directly should result in same output when given same input. in case, both tasks given mypluginprops.message input, task applied plugin greedy , evaluates null on. (during apply phase?)

the solution have found use closures in plugin task's configuration block so:

//define plugin apply task of custom type class myplugin implements plugin<project> {     void apply(project project) {         project.extensions.create('mypluginprops', mypluginextension)          project.task(type: mytask, 'thistaskworksincorrectly') {             input = { project.mypluginprops.message }         }     } } 

that solves greedy evaluation problem pretty nicely except custom task has modified expect , deal closure. it's not terrifically hard do, don't think should task's responsibility deal closure, since plugin "to blame".

am using extensions incorrectly here? or not adequate? official stance appears we should use extensions i've yet find examples extensions need. can move forward use of closures , writing bunch of boilerplate getters closure eval , setters can handle closures , normal types, seems against philosophy of groovy , therefore gradle. happy if there way can use extensions , lazy evaluation automatically.

the usual solution problem use convention mapping:

class myplugin implements plugin<project> {     void apply(project project) {        project.extensions.create('mypluginprops', mypluginextension)          project.task(type: mytask, 'thistaskworksincorrectly') {             conventionmapping.input = { project.mypluginprops.message }         }     } } 

and in task:

class mytask extends defaulttask {     def string input      @taskaction     def action() {         println "you gave me this: ${getinput()}"     } 

}

please note explicitly used getter input - convention mapping won't kick in if reference field directly.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -