gradle的新插件dsl语法 [英] gradle's new plugins dsl syntax
问题描述
我目前正在学习gradle构建工具,特别是新插件DSL语法,在文档中.在链接中,作者提到了
I'm currently learning gradle build tool, specifically the new plugins DSL syntax, in the documentation. In the link, the author mentioned
我了解到,对于每个build.gradle
文件,gradle都会隐式创建一个Project
类型的对象供程序员使用,并且build.gradle
文件中声明的每个变量/方法都隐式地附加到Project
对象,所以我查找了项目类API文档,用于名为plugins
的方法,该方法接受闭包作为第一个参数,但不存在闭包.
I understand that for every build.gradle
file, gradle implicitly create an object of type Project
for the programmers to use and every variable/methods declared in the build.gradle
file is implicitly attached to the Project
object, so I looked up the Project class API documentation for a method called plugins
that accept an closure as its first argument, but none exist.
我了解较新的插件dsl语法旨在替代传统的apply()
方法机制.但是,为什么在Project
类的文档中找不到plugins
方法?
I understand the newer plugins dsl syntax is intend to replace the traditional apply()
method mechanism. But why I cant find the plugins
method in the Project
class's documentation?
推荐答案
Project 接口是从 PluginAware 接口继承的,这是处理插件"功能的地方:请参见方法规范 https://docs. gradle.org/current/javadoc/org/gradle/api/plugins/PluginAware.html#getPlugins--
Project interface inherits from PluginAware interface, and this is where 'plugins' feature is handled : see method specification https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/PluginAware.html#getPlugins--
但实际上, plugins {} 语法与Project类中的属性或方法无关:它是一种 script块,由Gradle在一种特殊的方式,例如 buildScript 块.您可以在以下官方文档中找到有关此内容的详细信息: https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html
But in fact, the plugins { } syntax is not related to a property or methods from Project class: its a kind of script block which is handled by Gradle in a special way, like buildScript block. You will find details about that in the official documentation there: https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html
注意:我建议您检查DSL文档而不是Javadoc(或Javadoc之外):我认为它提供了有关编写构建脚本时可以使用的示例的更好描述.例如,在此处记录了"plugins"属性: https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:plugins
Note: I recommend that you to check the DSL documentation instead of the Javadoc (or in addition to the Javadoc): I think it provides better description of what you can use when writting build scripts, with examples. The 'plugins' property for example is documented there : https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:plugins
致谢.
这篇关于gradle的新插件dsl语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!