# Task Rule Dependencies

**URL:** <https://discuss.gradle.org/t/task-rule-dependencies/7054>\
**Category:** Old Forum Archive\
**Created:** [January 11, 2013, 12:42am UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054 "2013-01-11T00:42:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Dow](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@Christopher\_Dow](https://discuss.gradle.org/u/Christopher_Dow)\
**Post date:** [January 11, 2013, 12:42am UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054/1 "2013-01-11T00:42:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tye\_Howard1](https://avatars.discourse-cdn.com/v4/letter/t/71c47a/32.png) [@Tye\_Howard1](https://discuss.gradle.org/u/Tye_Howard1)\
**Post date:** [January 11, 2013, 3:53pm UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054/2 "2013-01-11T15:53:00Z")

</div>

I think you’re asking about this:

```gradle
taskIWantToRunSecond.dependsOn taskIWantToRunFirst

```

So it would probably looks something like:

```gradle
grailsAppTask.dependsOn databaseConfigTask

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Dow](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@Christopher\_Dow](https://discuss.gradle.org/u/Christopher_Dow)\
**Post date:** [January 11, 2013, 5:29pm UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054/3 "2013-01-11T17:29:00Z")

</div>

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):

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

```

I get the exception:

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

```

---

<div class="post-metadata">

**Author:** ![Tye\_Howard1](https://avatars.discourse-cdn.com/v4/letter/t/71c47a/32.png) [@Tye\_Howard1](https://discuss.gradle.org/u/Tye_Howard1)\
**Post date:** [January 11, 2013, 5:55pm UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054/4 "2013-01-11T17:55:00Z")

</div>

Ah. Sounds like you want something like :

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

```

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [January 11, 2013, 6:05pm UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054/5 "2013-01-11T18:05:00Z")

</div>

What version of the Grails plugin are you using?

---

<div class="post-metadata">

**Author:** ![Christopher\_Dow](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@Christopher\_Dow](https://discuss.gradle.org/u/Christopher_Dow)\
**Post date:** [January 11, 2013, 6:59pm UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054/6 "2013-01-11T18:59:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christopher\_Dow](https://avatars.discourse-cdn.com/v4/letter/c/7ab992/32.png) [@Christopher\_Dow](https://discuss.gradle.org/u/Christopher_Dow)\
**Post date:** [January 15, 2013, 12:37am UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054/7 "2013-01-15T00:37:00Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [January 15, 2013, 9:30am UTC](https://discuss.gradle.org/t/task-rule-dependencies/7054/8 "2013-01-15T09:30:00Z")

</div>

@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.
