简体   繁体   English

在 Groovy 中指定一个属性后跟一个闭包是什么意思?

[英]What does it mean in Groovy to specify a property followed by a closure?

I am completely new to Groovy, trying to learn it, but stymied because I can't parse the syntax well enough to even know where to look in the documentation.我对 Groovy 完全陌生,试图学习它,但因为我无法很好地解析语法,甚至不知道在哪里查看文档而受到阻碍。 I am using Groovy in Gradle.我在 Gradle 中使用 Groovy。 There are many places where examples are given, but no explanation on what it means, so I just need a few pointers.有很多地方给出了例子,但没有解释它的含义,所以我只需要一些指针。

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = 'com.xxx.yyy'
            artifactId = 'zzz'
            from components.java
        }
    }
    repositories {
        mavenLocal();
    }
}

The main build code is referring to things on the project class.主要构建代码是指class项目上的东西。 On that class, I can find a property called publishing, and it is a class PublishingExtension .在那个 class 上,我可以找到一个名为 publishing 的属性,它是 class PublishingExtension It appears then that the curly brace starts a closure with code in it.然后看起来大括号开始了一个带有代码的闭包。 The documentation says this syntax:文档说这种语法:

publishing { }

configures the PublishingExtension .配置PublishingExtension What I want to understand is what it means (ie what is actually happening) when I specify what looks like a property and follow that with a Closure.我想了解的是当我指定看起来像一个属性的东西并使用闭包跟随它时它的含义(即实际发生的情况)。 In the Groovy documentation I could not find any syntax like this, nor explanation.在 Groovy 文档中,我找不到任何这样的语法,也找不到解释。 I sure it is something simple but I don't know enough to even know what to look for.我确定这很简单,但我知道的不够多,甚至不知道要寻找什么。

If I visit the Project Class API Docs there is no method there named publishing .如果我访问项目 Class API 文档,则没有名为publishing的方法。 Nor is there a property defined by the method getPublishing .方法getPublishing也没有定义属性。 Apparently this magic capability is enabled by the publishing plugin .显然, publishing plugin启用了这种神奇的功能。 If I visit the Publishing Plugin API Doc there is no description of this publishing property either or how it modifies the base project.如果我访问Publishing Plugin API Doc ,则没有关于此发布属性或它如何修改基础项目的描述。

Similarly, drilling down a little more, that closure starts with a symbol publications and in the documentation for the PublishingExtension I find a property which is of type PublicationContainer which is read only.同样,再深入一点,该闭包以符号publications开头,在PublishingExtension的文档中,我找到了一个PublicationContainer类型的属性,该属性是只读的。 I also find a method named publications which does not accept a closure, but instead a configuration of type Action<? super PublicationContainer>我还找到了一个名为publications的方法,它不接受闭包,而是configuration Action<? super PublicationContainer> Action<? super PublicationContainer> . Action<? super PublicationContainer> Again, I don't know how the contents of the curly braces are converted to an Action class instance.同样,我不知道花括号的内容如何转换为Action class 实例。 How does this object get constructed?这个 object 是如何构造的? Action is an interface, and the only method is execute however it is completely unclear how this action gets constructed. Action是一个接口,唯一的方法是execute ,但是完全不清楚这个动作是如何构造的。

The block that defines the Action starts with symbol mavenJava that looks like a method, but actually that first symbol is declaring a name of a new object of type MavenPublication named mavenJava .定义 Action 的块以符号mavenJava ,看起来像一个方法,但实际上第一个符号声明了一个名为mavenJavaMavenPublication类型的新 object 的名称。 Either this is magically constructed (I don't know the rules) or there is a method called, but which method?要么这是神奇构造的(我不知道规则),要么有一个方法被调用,但是哪个方法? What is it about PublicationContainer that allows it to know that an arbitrary mavenJava command is supposed to create an object instance. PublicationContainer是什么让它知道任意mavenJava命令应该创建一个 object 实例。 Then again, the curly braces that follow this, is that a closure, a configuration, or something even more exotic?再说一遍,紧随其后的花括号是闭包、配置还是更奇特的东西?

So as you can see, I am missing a little info on how Groovy works.如您所见,我缺少有关 Groovy 工作原理的一些信息。 So far I can't find documentation that explains this syntax, however it might be there if I knew what to look for.到目前为止,我找不到解释这种语法的文档,但是如果我知道要查找什么,它可能就在那里。 Can anyone explain what the syntax is really doing, or refer me to a site that can explain it?谁能解释一下语法的真正作用,或者让我去一个可以解释它的网站?

publishing is called to configure the PublishingExtension .调用发布来配置PublishingExtension

In PublishingExtension there is a publications method accepting an Action which is usually coerced from a Closure.PublishingExtension中有一个publications方法接受通常从闭包强制执行的Action In Groovy a Closure is automatically converted to an interface with a single method.在 Groovy 中,闭包会通过单一方法自动转换为接口。

mavenJava is a non-existent DSL method, which is passed by Gradle DSL builder to the create method of PublicationContainer : mavenJava是一个不存在的 DSL 方法,由 Gradle DSL builder 传递给PublicationContainercreate方法:

publishing.publications.create('mavenJava', MavenPublication) {
    // Configure the maven publication here
}

groupId and artifactId are properties of MavenPublication and are being set here. groupIdartifactIdMavenPublication的属性,并在此处设置。

from is the from(component) of MavenPublication and is written using Groovy simplified method call literal without brackets. fromMavenPublicationfrom(component) ,使用 Groovy 简化的方法调用文字(不带括号)编写。

In general Gradle uses a root DSL builder which calls the nested DSL builders provided by plugins.通常 Gradle 使用根 DSL 构建器,它调用插件提供的嵌套 DSL 构建器。 Hence sometimes it's difficuilt (also for the IDE) to find proper references of all parts of the build.gradle file.因此,有时很难找到 build.gradle 文件所有部分的正确引用(对于 IDE 也是如此)。

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

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