简体   繁体   中英

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. I am using Groovy in Gradle. 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. On that class, I can find a property called publishing, and it is a 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 . 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. 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 . Nor is there a property defined by the method getPublishing . Apparently this magic capability is enabled by the 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.

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. I also find a method named publications which does not accept a closure, but instead a configuration of type 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. How does this object get constructed? Action is an interface, and the only method is execute however it is completely unclear how this action gets constructed.

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

In PublishingExtension there is a publications method accepting an Action which is usually coerced from a Closure. In Groovy a Closure is automatically converted to an interface with a single method.

mavenJava is a non-existent DSL method, which is passed by Gradle DSL builder to the create method of PublicationContainer :

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

groupId and artifactId are properties of MavenPublication and are being set here.

from is the from(component) of MavenPublication and is written using Groovy simplified method call literal without brackets.

In general Gradle uses a root DSL builder which calls the nested DSL builders provided by plugins. Hence sometimes it's difficuilt (also for the IDE) to find proper references of all parts of the build.gradle file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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