Gradle plugin: agreement against extension

I am writing a Gradle plugin and learning Gradle reading the user manual and source code of the plugins inside the Gradle project.

In the source code, I found two ways to add properties to the project:

  • Convention (installed by JavaBasePlugin and used by JavaPlugin)
  • Extension (installed by AnnoncePlugin and used by BuildAnnouncementsPlugin).

I do not understand the difference between them and which one to use for which situation. Can someone explain?

PS: Can someone add the "gradle-plugin" tag to SO, please?

+7
plugins gradle
source share
1 answer

I found the answer on the Gradle forum:

Extensions and conventions are similar (but not identical) to the methods of dynamically expanding the construction model. Extensions are a newer concept and largely replace conventions. In short, use only extensions, do not use conventions.

[...]

An extension is an instance of an arbitrary (usually user-defined) class that is bound to an assembly model under a user-defined name. An extension class can define arbitrary methods. Assuming it's attached to a Project object, the extension allows you to add project.foo.someMethod, but not project.someMethod. Since each extension has its own namespace (in this case, foo), the likelihood of name collisions is significantly reduced (compared to the legend).

+7
source share

All Articles