|
Hi all,
Is it possible to use a convention property inside apply method? I've tried the code bellow but it prints null: class GreetingPlugin implements Plugin<Project> { def void apply(Project project) { project.convention.plugins.greet = new GreetingPluginConvention() println "Convention property " + project.convention.plugins.greet.message } } Any ideas? Thanks. Ricardo |
|
On 24/08/2011, at 9:45 PM, Ricardo Mayerhofer wrote: > Hi all, > Is it possible to use a convention property inside apply method? I've tried > the code bellow but it prints null: > > class GreetingPlugin implements Plugin<Project> { > def void apply(Project project) { > > project.convention.plugins.greet = new GreetingPluginConvention() > println "Convention property " + project.convention.plugins.greet.message > } > } Conventions are like mixins, so instead of: println "Convention property " + project.convention.plugins.greet.message Try: println "Convention property " + project.message -- Luke Daley Principal Engineer, Gradleware http://gradleware.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
Hi Luke,
Thanks for the prompt response. It didn't work. Am I doing something wrong? Here goes the complete source: apply plugin: GreetingPlugin class GreetingPlugin implements Plugin<Project> { def void apply(Project project) { project.convention.plugins.greet = new GreetingPluginConvention() println "Convention property in apply " + project.message project.task('hello') << { println "Convention property in task " + project.message } } } class GreetingPluginConvention { String message def greet(Closure closure) { closure.delegate = this closure() } } greet { message = 'Hi from Gradle' } It prints: Convention property in apply null Convention property in task Hi from Gradle |
|
On 25/08/2011, at 4:00 PM, Ricardo Mayerhofer wrote: > Hi Luke, > Thanks for the prompt response. It didn't work. Am I doing something wrong? Below is the output I would expect. Are you expecting “Convention property in apply null” to be something different? > Here goes the complete source: > > apply plugin: GreetingPlugin > > class GreetingPlugin implements Plugin<Project> { > def void apply(Project project) { > > project.convention.plugins.greet = new GreetingPluginConvention() > println "Convention property in apply " + project.message > project.task('hello') << { > println "Convention property in task " + project.message > } > } > } > > class GreetingPluginConvention { > String message > > def greet(Closure closure) { > closure.delegate = this > closure() > } > } > > greet { > message = 'Hi from Gradle' > } > > It prints: > Convention property in apply null > Convention property in task Hi from Gradle -- Luke Daley Principal Engineer, Gradleware http://gradleware.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
The point is I'd like to be able to read convention properties in apply method, currently it prints null, which means it wasn't set. Perhaps I'm not thinking the gradle way.
I need to configure some tasks in plugin that receive input from convention properties, e.g.: ... def void apply(Project project) { project.convention.plugins.deploy = new DeployPluginConvention() println "Configuring deploy for ${project.convention.plugins.deploy.applicationName}" def deploy = project.getTasks().add( "deploy", DeployTask.class ) deploy.earPath = project.convention.plugins.deploy.applicationName + ".ear" } What is the best way to achieve this? |
|
There appears to be some misunderstanding on your side. Perhaps this is what you want:
void apply(Project project) { // this makes the members of DeployPluginConvention available on 'project' (NOT on 'project.deploy') // the DeployPluginConvention object is internally stored under the key 'deploy', // which makes it possible to access its members even from tasks/plugins written in Java // (e.g. with 'project.getConvention().getPlugins().get("deploy").getApplicationName()') project.convention.plugins.deploy = new DeployPluginConvention() // printing out 'applicationName' doesn't make sense here // because it will only be set further down in the build script // println "Configuring deploy for $project.applicationName" def deploy = project.tasks.add("deploy", DeployTask) // need to set 'earPath' lazily (again because the value for 'applicationName' isn't available yet) // this is done using Gradle's 'convention mapping' mechanism deploy.conventionMapping.earPath = { project.applicationName + ".ear" } } Disclaimer: I didn't compile or run this code. Usage from build.gradle: apply plugin: DeployPlugin applicationName = "foo" If you want this to be 'deploy.applicationName = "foo"', you'll have to wrap the DeployPluginConvention object with an object that has a 'deploy' property, or change installation of the convention object to 'project.deploy = new DeployPluginConvention()', or use the 'extension' mechanism introduced in Gradle 1.0-milestone-4. From milestone 4 onwards, the latter is the best option. -- Peter Niederwieser Principal Engineer, Gradleware http://gradleware.com Creator, Spock Framework http://spockframework.org Twitter: @pniederw
|
|
Hi Peter,
The lazy thing worked. Thanks. Ricardo |
| Powered by Nabble | See how NAML generates this page |
