gradle - plugins {} (script block) vs pluginManager.apply()

Viewed 826

I am applying plugins to a Gradle build script. But there happen to be many ways to doing this.

I found the following two methods for applying plugins to a Gradle project:

  1. The DSL for declaring plugins to use in a script i.e. plugins {} script block, which I think configures a PluginDependenciesSpec object. (Read this)

    • Why isn't the plugins {} script block shown in the Script blocks section of the Project DSL documentation?
  2. Calling pluginManager.apply() (which applies plugins to a PluginManager instance) or calling plugins.apply() (which applies plugins to a PluginContainer instance) (Read this)

So for applying, say org.gradle.java plugin, I could do the following in a build.gradle file:

>> pluginManager.apply 'org.gradle.java'

>> plugins.apply 'org.gradle.java'

>> plugins {
    id 'org.gradle.java'
}

What are the differences between each?

PS: I found this in the documentation for PluginDependenciesSpec, but then why have so many different methods?

The plugins {} block serves a similar purpose to the PluginAware.apply(java.util.Map) method that can be used to apply a plugin directly to a Project object or similar. A key difference is that plugins applied via the plugins {} block are conceptually applied to the script, and by extension the script target. At this time there is no observable practical difference between the two approaches with regard to the end result.

0 Answers
Related