# Adding stuff to tasks when plugin order is unknown

**URL:** <https://discuss.gradle.org/t/adding-stuff-to-tasks-when-plugin-order-is-unknown/9236>\
**Category:** Help/Discuss\
**Created:** [April 20, 2015, 8:47pm UTC](https://discuss.gradle.org/t/adding-stuff-to-tasks-when-plugin-order-is-unknown/9236 "2015-04-20T20:47:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schalk\_Cronje](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/schalk_cronje/32/6511_2.png) [@Schalk\_Cronje](https://discuss.gradle.org/u/Schalk_Cronje)\
**Post date:** [April 20, 2015, 8:47pm UTC](https://discuss.gradle.org/t/adding-stuff-to-tasks-when-plugin-order-is-unknown/9236/1 "2015-04-20T20:47:23Z")

</div>

This is something that I mentioned in my talk at Greach this year. I said that this is the only way I know of doing in this and if someone knows a shorter way I would love to hear about it. René might have mentioned something, but if he did, I forgot. Therefore the question is now on the forum.

This is for the case where when I want to perform an additional action on a task in another plugin, but I do not know in which order plugins will be applied by a script author or whether it even will be applied.

```gradle
// Let's say I want to do add something to 'jar' task, 
// but only if the 'java' plugin is applied.
// If is not applied, I don't want to do anything i.e. 
// I don't want my plugint to force-apply 'java'.

 try {
  Task t = project.tasks.getByName('jar')
  // Handle the case where task already exist
  if( t instanceof Jar) {
    do_something_with(t)   
}
} catch(UnknownTaskException) {
  // Handle case for when task is added at a later stage
  project.tasks.whenTaskAdded { Task t ->
  if (t.name == 'jar' && t instanceof Jar) {
    do_something_with(t)
  }
}

```

Is there any shorter way of writing the above?

---

<div class="post-metadata">

**Author:** ![Rene](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rene/32/8082_2.png) [@Rene](https://discuss.gradle.org/u/Rene)\
**Post date:** [April 20, 2015, 9:00pm UTC](https://discuss.gradle.org/t/adding-stuff-to-tasks-when-plugin-order-is-unknown/9236/2 "2015-04-20T21:00:23Z")

</div>

Hey Schalk,

you could do something like this:

```
class MyPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.plugins.withType(JavaPlugin) {
	     // add logic only if JavaPlugin is applied (lazy evaluated)
             // no matter if applied after or before my own plugin
        }
     }
 }

```

cheers,  
René

---

<div class="post-metadata">

**Author:** ![Schalk\_Cronje](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/schalk_cronje/32/6511_2.png) [@Schalk\_Cronje](https://discuss.gradle.org/u/Schalk_Cronje)\
**Post date:** [April 20, 2015, 10:44pm UTC](https://discuss.gradle.org/t/adding-stuff-to-tasks-when-plugin-order-is-unknown/9236/3 "2015-04-20T22:44:26Z")

</div>

Hey René,

That’s find for something that ships woth Gradle, but what if I want to use ‘some.foo.bar.otherPlugin’ without creating a direct dependency on between my plugin and anotehr plugin.

For instance if I wanted to do something with John Engelman’s shadowJar, I can do

```groovy
  project.tasks.whenTaskAdded { Task t ->
    if(t.class.superclass.name == 'com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar' && t.name=='shadowJar') {
      // Handle case for when shadowJar is added
    }
  }

```

which gets me around the dependency issue.

However I’m, not sure how I would achieve something similar using your example. I see there is a `matching` method, but it does not provide an option to provide an action as with `withType`.

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [April 21, 2015, 6:00am UTC](https://discuss.gradle.org/t/adding-stuff-to-tasks-when-plugin-order-is-unknown/9236/4 "2015-04-21T06:00:49Z")

</div>

You could use something like `Class.forName()` to check for the existence of the plugin on the classpath and conditionally then use `plugins.withType()` to register some configuration if the class exists.

---

<div class="post-metadata">

**Author:** ![Rene](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rene/32/8082_2.png) [@Rene](https://discuss.gradle.org/u/Rene)\
**Post date:** [April 21, 2015, 11:03am UTC](https://discuss.gradle.org/t/adding-stuff-to-tasks-when-plugin-order-is-unknown/9236/5 "2015-04-21T11:03:06Z")

</div>

> [@Schalk\_Cronje](#):
>
> That’s find for something that ships woth Gradle, but what if I want to use ‘some.foo.bar.otherPlugin’ without creating a direct dependency on between my plugin and anotehr plugin.

You could use

```
project.plugins.withId("com.github.johnrengelman.shadow") {
    //add your custom logic here
}

```

to avoid a direct compile dependency
