Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle plugin: Convention vs. Extension

Tags:

plugins

gradle

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

In the source code, I found 2 ways of adding properties to the project:

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

I don't understand the difference between them and which one to use for which situation. Could someone explain?

PS: Could someone add the tag "gradle-plugin" in SO please?

like image 637
Vincent Cantin Avatar asked Jul 11 '13 08:07

Vincent Cantin


1 Answers

I found an answer on the forum of Gradle:

Extensions and conventions are similar (but not identical) ways to dynamically extend the build model. Extensions are the newer concept and have largely replaced conventions. In short, only use extensions, don't use conventions.

[...]

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

like image 142
Vincent Cantin Avatar answered Sep 23 '22 14:09

Vincent Cantin