Gradle插件:约定与扩展 [英] Gradle plugin: Convention vs. Extension

查看:213
本文介绍了Gradle插件:约定与扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Gradle插件,并且通过阅读Gradle项目中的插件的用户指南和源代码来学习Gradle。



在源代码,我找到了两种向项目添加属性的方法:


  • 惯例(由JavaBasePlugin设置并由JavaPlugin)

  • 扩展(由AnnoncePlugin设置并由BuildAnnouncementsPlugin使用)



我不明白他们之间的区别以及在哪种情况下使用哪一种。有人可以解释一下吗?



PS:有人可以在SO上添加标签gradle-plugin吗?

解决方案

我在 Gradle论坛上找到了答案:


扩展和约定与动态扩展构建模型类似(但不完全相同)。扩展是更新的概念,并且已经在很大程度上取代了惯例。总之,只有使用扩展名,不要使用约定。

[...]

是以用户定义的名称连接到构建模型的任意(通常是用户定义的)类的实例。扩展类可以定义任意方法。假设它附加到一个Project对象,扩展允许您添加project.foo.someMethod,但不是project.someMethod。由于每个扩展名都有自己的名称空间(本例中为foo),名称冲突的可能性大大降低(与约定相比)。



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?

解决方案

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).

这篇关于Gradle插件:约定与扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆