On 31/07/2011, at 11:07 PM, Benjamin Muschko <[email protected]> wrote:

> Hi,
> 
> When writing a custom plugin I came across the following situation. I'd like
> to dynamically add a task based on the value of one of the fields in my
> convention object. However, when I try to identify the task implementation
> class the convention object's values haven't been initialized yet. Here's
> some sample code to illustrate this.
> 
> 1) We have a convention object with a field that determines the task I'd
> like to use.
> 
> class PluginConvention {
>    String taskId
> }
> 
> 2) We define two different task implementation classes.
> 
> def tasks = ['taskId1': Task1.class, 'taskId2': Task2.class]
> 
> 3) In my plugin class (implements Plugin) I'd now like to add a task based
> on the value of taskId.
> 
> def taskClass = tasks[pluginConvention.taskId] <--- will still be null
> 
> project.tasks.withType(taskClass).whenTaskAdded { task ->
>     // do convention mapping
> }
> 
> def task = project.tasks.add("someTaskName", taskClass)
> 
> Is there a way to make this work? Any recommendations would be highly
> appreciated.

You could either delay creation of the task to a later point, e.g. in an 
afterEvaluation listener or trigger something from the convention, or a 
combination thereof.

There's a missing piece in the Gradle mechanics as there is no general 
lifecycle for model elements. That would help you here. It is something we are 
looking to add.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to