What is the difference between these two method to...
# plugin-development
t
What is the difference between these two method to apply plugin?
Copy code
project.pluginManager.apply(JavaPlugin::class.java)
        project.plugins.apply(JavaPlugin::class.java)
For example, I see that
java-library
plugin uses first one 🤔
g
I also don’t know what the underlying difference is, but the docs advise to use the first one
While not deprecated, it is preferred to use the methods of this interface or the
plugin manager
than use the plugin container.
https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/PluginAware.html#apply-groovy.lang.Closure-
👍 1
t
from the code I see it is mostly the same, just
PluginManager
calls
doApply
via
apply
and
PluginContainer
calls
PluginManager.addImperativePlugin
which also calls inside
doApply
So seems there is no difference 🤷‍♂️
🤷🏻‍♂️ 1
j
Looks like legacy code that will be deleted some day so I would use plugin manager
c
agreed. though that unfortunately leaves inconsistent code:
Copy code
pluginManager.apply(...)
plugins.withType<...>
…as there is no withType equivalent on pluginManager.
👀 1
1
@Tapchicoma importing
org.gradle.kotlin.dsl.apply
allows removal of ‘.java’, e.g.
project.pluginManager.apply(JavaPlugin::class)
…and this other neat Kotlin syntax allows removing ‘project.’:
Copy code
override fun apply(project: Project): Unit = project.run { }
…and some more DSL / extension functions on Project allow for:
Copy code
apply<BasePlugin>()
e
@Chris Lee why would you want to
plugins.withType(<type::class>)
vs
pluginManager.withPlugin("<id>")
? Well behaved plugins should set up some extensions or tasks or some other model and be done. You shouldnt need the actual plugin instance for anything (most of the time). Imo, its becoming more consistent by recommending to use the plugin manager together with ids 🤷