The right way to provide derived defaults for Gradle task properties -
i writing gradle task generate configuration file needed application deployment. let users customise name , location of generated file.
a rough sketch of task class be:
class configurationfiletask extends defaulttask { string configfilename = 'config.xml' file configfilepath = new file(project.builddir, configfilename) } the idea let users override either filename, or whole file location, wish.
i realise code above wouldn't work, because project not defined when initialiser configfile runs; problem working out right way this.
now, task citizen in wider build, means other tasks should able refer properties. example, users able write:
task createconfigfile(type: configurationfiletask) { configfilename = 'app-config.xml' } jar { from(createconfigfile.configfilepath) } jar.dependson createconfigfile and have add generated config file jar. note in case, user setting configfilename, referring configfilepath. means path must computed time after name set.
so, how can provide property can set user, has default based on property, , property usable other tasks?
make method then, works
task overrideboth(type: configurationfiletask) { _configfilename = 'app-configb.xml' _dir = '../' } task overridefilename(type: configurationfiletask) { _configfilename = 'app-confign.xml' } task overridepath(type: configurationfiletask) { _dir = '../../' } task defaultbehavior(type: configurationfiletask) { } class configurationfiletask extends defaulttask { def _configfilename = 'config.xml' def _dir = project.builddir def getfile() { return new file(_dir,_configfilename) } } task othertask(type: defaulttask) { println("defaultvalues ${defaultbehavior.getfile().canonicalpath}") println("overridefilename ${overridefilename.getfile().canonicalpath}") println("overridepath ${overridepath.getfile().canonicalpath}") println("overrideboth ${overrideboth.getfile().canonicalpath}") } configfilename gets overriden
Comments
Post a Comment