简体   繁体   English

groovy中的这种闭包查找代码意味着什么?

[英]what does this closure looking code in groovy means?

I am experimenting with some gradle at a new project, and in its settings.gradle , file I see these few lines that I am unable to make sense of as to what groovy structure or a language feature it is and what it does and how it works: 我正在尝试在一个新项目中进行一些测试,并在其settings.gradle文件中看到了以下几行内容,这些行我无法理解它是什么常规的结构或语言功能,它的功能以及工作方式。作品:

plugins {
  id "com.gradle.build-scan" version "1.12.1"
  id "cz.malohlava"     version "1.0.3"
}

buildScan {
  server = "some.host.com"
  publishAlways()
}

I was suspecting it was either aa closure or an interface of some sort, but could not make head or tail of it. 我怀疑它要么是封闭的 ,要么是某种形式的接口 ,但无法制成它的头或尾。

Any help in understanding following will be a great help: 对理解以下内容的任何帮助都会有很大帮助:

  • what it does? 它能做什么?
  • How plugins and buildScan works here from the language's perspective? 从语言的角度来看,插件和buildScan如何在这里工作?

From the language perspective, the closures are executed in the context of another objects than the build script. 从语言角度来看,闭包是在除构建脚本之外的其他对象的上下文中执行的。 This is called delegation in Groovy. 在Groovy中这称为委派。

http://groovy-lang.org/closures.html#_delegation_strategy http://groovy-lang.org/closures.html#_delegation_strategy

plugin delegates to https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html plugin委托给https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html

buildScan delegates to Build Scan Plugin's extension object which configures the plugin. buildScan委托用于配置插件的Build Scan Plugin的扩展对象。

There may be some trickery here that I don't understand, particularly as I can't find either plugins() or buildScan() in the API docs. 这里可能有一些我不理解的buildScan() ,特别是因为我在API文档中找不到plugins()buildScan() Nonetheless, the following is a reasonable reading of what the syntax means. 尽管如此,以下内容是对该语法含义的合理理解。

  1. plugins {} and buildScan {} are both methods that take a closure (see other answers for explanation of this) as an argument. plugins {}buildScan {}是采取封闭(见这种解释其他答案)作为参数两种方法

  2. Each closure has a delegate object of a particular type that's different depending on the method using the closure, ie the delegate of plugins() will be of a different type to the delegate of buildScan() 每个闭包都有一个特定类型的委托对象,该对象根据使用该闭包的方法而有所不同,即, plugins()的委托与buildScan()的委托将具有不同的类型。

  3. Within the closure, unqualified methods and properties will be executed against the delegate object. 在闭包内,将对委托对象执行不合格的方法和属性。 So for the plugins {} block, id(...).version(...) will be called against its delegate. 因此,对于plugins {}块,将针对其委托调用id(...).version(...) For buildScan {} , you're setting the property server on the delegate and calling its publishAlways() method. 对于buildScan {} ,您需要在委托上设置属性server并调用其publishAlways()方法。

Honestly, I don't know how useful the above information is for using and understanding Gradle, but I think it's what you're asking for. 老实说,我不知道上述信息对使用和理解Gradle有多有用,但我认为这正是您所要的。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM