Task Rule Dependencies

How do you give a task rule another task as a dependency (either a specific task generated by the rule or for all of the tasks generated by the rule)?

I am using the Grails plugin and it defines all of it’s tasks via a task rule. I have a task that I use to generate a database config file. I would like it to run before that task to start the Grails app.

I think you’re asking about this:

taskIWantToRunSecond.dependsOn taskIWantToRunFirst

So it would probably looks something like:

grailsAppTask.dependsOn databaseConfigTask

That only seems to work for explicitly defined tasks. I can’t get it to work for for tasks defined as a task rule. For instance if I try the following (I use “tasks[‘grails-run-app’]” because ‘grails-run-app’ is not a valid groovy identifier):

tasks['grails-run-app'].dependsOn configProperties

I get the exception:

org.gradle.api.UnknownTaskException: Task with name 'grails-run-app' not found in project ':internal-tools'.

Ah. Sounds like you want something like :

tasks.addRule("Pattern: grails-run-app") { String taskName ->
        task(taskName).dependsOn(configProperties)
    }
}

What version of the Grails plugin are you using?

Tye:

Yeah, something like that. Unfortunately, I don’t control the definition of the rule so I can’t add that.

Luke:

I’m using version 1.1.0 of the grails-gradle-plugin. Also, for completeness, I have tried running it both on Gradle 1.2 and 1.3.

I worked out a solution to this problem. Basically I have to add a listener for when the tasks are created by the rule. So I have to do something like:

tasks.whenTaskAdded { newTask ->
    if(newTask.name == 'grails-run-app') {
        newTask.configure {
            dependsOn('configProperties')
        }
    }
}

@Christopher: sorry for the late reply.

There’s a bug in version 1.1 that prevents this from working as it should. The workaround you have in place is the only way I can see to solve this unfortunately.