# Access task generated by new component model to add "dependsOn"

**URL:** <https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966>\
**Category:** Old Forum Archive\
**Created:** [March 15, 2015, 9:42pm UTC](https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966 "2015-03-15T21:42:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Herter](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Simon\_Herter](https://discuss.gradle.org/u/Simon_Herter)\
**Post date:** [March 15, 2015, 9:42pm UTC](https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966/1 "2015-03-15T21:42:00Z")

</div>

How can I access a task that is generated by the new component model?

Example.

```gradle
plugins {
  id 'cpp'
}
  model {
  components {
         'native'(NativeLibrarySpec) {
    }
  }
}
  task test << {
  println "hello"
}
// doesn't work: Could not find property 'nativeSharedLibrary' on root project 'test'
nativeSharedLibrary.dependsOn test

```

---

<div class="post-metadata">

**Author:** ![Simon\_Herter](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Simon\_Herter](https://discuss.gradle.org/u/Simon_Herter)\
**Post date:** [March 16, 2015, 5:10pm UTC](https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966/2 "2015-03-16T17:10:00Z")

</div>

I found a workaround by delaying the creation of the ‘depends-on’ link:

```gradle
tasks.matching{ it.name.contains('compileNativeSharedLibrary') }
  .whenTaskAdded { it.dependsOn('test') }

```

However, this solution is kind of ugly. Isn’t there a cleaner way to do it?

---

<div class="post-metadata">

**Author:** ![Mark\_Viera](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_viera/32/1543_2.png) [@Mark\_Viera](https://discuss.gradle.org/u/Mark_Viera)\
**Post date:** [March 16, 2015, 8:31pm UTC](https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966/3 "2015-03-16T20:31:00Z")

</div>

You’ll have to put that configuration inside the ‘model { }’ block. Take a look at the [release notes](http://gradle.org/docs/2.2/release-notes#publishing-plugins-and-native-language-support-plugins-changes) for Gradle 2.2 for details.

model {

tasks.compileNativeSharedLibrary {

dependsOn test

}

}

---

<div class="post-metadata">

**Author:** ![Simon\_Herter](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Simon\_Herter](https://discuss.gradle.org/u/Simon_Herter)\
**Post date:** [March 17, 2015, 9:30am UTC](https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966/4 "2015-03-17T09:30:00Z")

</div>

Thank you (it works with _taskS_).

Is there somewhere general documentation about the whole ‘model { } block’ thing? I can’t find an entry point like the ‘Project’ class in the old model.

---

<div class="post-metadata">

**Author:** ![Simon\_Herter](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Simon\_Herter](https://discuss.gradle.org/u/Simon_Herter)\
**Post date:** [March 17, 2015, 11:58am UTC](https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966/5 "2015-03-17T11:58:00Z")

</div>

Next problem: as I need to configure multiple tasks I tried to put everything inside the ‘tasks’ block. However, inside the tasks block the task name does not exist:

```gradle
model {
  // this works
  tasks.compileNativeSharedLibraryNativeCpp { dependsOn generateHeader }
  tasks {
    // this doesn't: Task with name 'compileNativeSharedLibraryNativeCpp' not found in root project
    getByName 'compileNativeSharedLibraryNativeCpp', { dependsOn generateHeader }
      // this _does_ work for top-level task
    getByName 'nativeSharedLibrary', { dependsOn generateHeader }
}

```

---

<div class="post-metadata">

**Author:** ![Mark\_Viera](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_viera/32/1543_2.png) [@Mark\_Viera](https://discuss.gradle.org/u/Mark_Viera)\
**Post date:** [March 17, 2015, 3:33pm UTC](https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966/6 "2015-03-17T15:33:00Z")

</div>

Simon, these particularities are due to the fact that the native build support is using the new Gradle configuration model. Details and better documentation is forthcoming on this front.

---

<div class="post-metadata">

**Author:** ![Simon\_Herter](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Simon\_Herter](https://discuss.gradle.org/u/Simon_Herter)\
**Post date:** [March 17, 2015, 3:52pm UTC](https://discuss.gradle.org/t/access-task-generated-by-new-component-model-to-add-dependson/6966/7 "2015-03-17T15:52:00Z")

</div>

Thanks, I’m looking forward to it 🙂 Keep up the good work!
