# Brand new in 2.3 pluginManager.withPlugin is broken

**URL:** <https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208>\
**Category:** Old Forum Archive\
**Created:** [February 27, 2015, 9:00pm UTC](https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208 "2015-02-27T21:00:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Oberstar](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Andrew\_Oberstar](https://discuss.gradle.org/u/Andrew_Oberstar)\
**Post date:** [February 27, 2015, 9:00pm UTC](https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208/1 "2015-02-27T21:00:00Z")

</div>

The new PluginManager’s ‘withPlugin’ method is completely broken. My understanding is that ‘pluginManager.withPlugin’ should be considered a replacement for ‘plugins.withId’. Unfortunately, while it waits until the plugin’s apply statement is called, it _does not_ wait until the ‘apply()’ method of the plugin exeutes.

You can see this in the following example:

```gradle
println 'Before withPlugin.'
pluginManager.withPlugin('java') {
 println 'Plugin was applied.'
 println project.tasks['jar'].name
}
println 'After withPlugin'
  println 'Before apply plugin'
pluginManager.apply('java')
println 'After apply plugin'

```

If/when you file a Jira, I have a zip of a project showing that the apply method is never called.

---

<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 1, 2015, 12:50am UTC](https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208/2 "2015-03-01T00:50:00Z")

</div>

This is the intended behavior. If you require that the plugin’s ‘apply()’ method be executed prior to your configuration closure then you should continue to use the ‘PluginContainer.withId()’ method. This new interface was primarily added to support new rules based plugins that are conditionally evaluated. Therefore it made sense to add a new plugin interface which could be used to add listeners that were called upon _application_ of a plugin, not necessarily _evaluation_ of that plugin.

---

<div class="post-metadata">

**Author:** ![ajoberstar](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ajoberstar/32/127_2.png) [@ajoberstar](https://discuss.gradle.org/u/ajoberstar)\
**Post date:** [March 1, 2015, 1:01am UTC](https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208/3 "2015-03-01T01:01:00Z")

</div>

Considering there was never a distinction between application and evaluation in the past, this should be more clear in the docs. Without that context the [release notes](https://gradle.org/docs/2.3/release-notes#new-pluginmanager-interface-for-applying-and-managing-plugins-by-id) and [DSL ref]([https://gradle.org/docs/current/dsl/org.gradle.api.plugins.PluginManager.html#org.gradle.api.plugins.PluginManager:withPlugin](https://gradle.org/docs/current/dsl/org.gradle.api.plugins.PluginManager.html#org.gradle.api.plugins.PluginManager:withPlugin)(java.lang.String, org.gradle.api.Action)) seem to merely imply a replacement (with some improved behavior) for the old ‘plugins.withId’.

Can you clarify the reason for the distinction? Not being familiar with the new rules plugins, I’m struggling to understand what you could even use ‘withPlugin’ for if you can’t access anything the plugin provides.

---

<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 1, 2015, 6:23am UTC](https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208/4 "2015-03-01T06:23:00Z")

</div>

Without going into too much detail, rule source plugins allow Gradle to evaluate only the parts of the configuration model necessary for any particular build. “Legacy” plugins on the other hand, have the entirety of the ‘apply()’ plugin evaluated for every project on every build during the configuration phase. The difference with rule plugins is that model elements are evaluated as they are queried. So when you say “can’t access anything the plugin provides” that is only the case for existing non-rules based plugins. Going forward, you would be able to access model elements defined by a rule source plugin using ‘withPlugin’ by placing configuration in a ‘model { }’ block.

The release notes are perhaps a bit vague and what is mean by using ‘PluginManager’ “when possible”. In this context it means when you are simply keying of the existence of the plugin.

---

<div class="post-metadata">

**Author:** ![ajoberstar](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ajoberstar/32/127_2.png) [@ajoberstar](https://discuss.gradle.org/u/ajoberstar)\
**Post date:** [March 1, 2015, 2:17pm UTC](https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208/5 "2015-03-01T14:17:00Z")

</div>

Alright. Thanks.

---

<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:** [March 1, 2015, 10:37pm UTC](https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208/6 "2015-03-01T22:37:00Z")

</div>

We should fix this. We can make this callback fire after the plugin has been applied.  
  
I’ve raised&nbsp;GRADLE-3249&nbsp;for this and will push to get it fixed in 2.4.

---

<div class="post-metadata">

**Author:** ![ajoberstar](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ajoberstar/32/127_2.png) [@ajoberstar](https://discuss.gradle.org/u/ajoberstar)\
**Post date:** [March 2, 2015, 12:10am UTC](https://discuss.gradle.org/t/brand-new-in-2-3-pluginmanager-withplugin-is-broken/2208/7 "2015-03-02T00:10:00Z")

</div>

Thanks!
