FredSuvn
FredSuvn

Reputation: 2087

What is the named struct in gradle?

Look at the codes: https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html

publishing {
  publications {
    myPublicationName(MavenPublication) {
      // Configure the publication here
    }
  }
}

There are two script blocks: publishing, publications. Gradle is based on groovy so I guess block script is a type of method like:

def publishing(Closure closure)

But, what is the myPublicationName(MavenPublication)? myPublicationName is a random name (there is no possible a method named myPublicationName, its dynamic not prepared), and MavenPublication is a type.

Upvotes: 0

Views: 83

Answers (1)

cfrick
cfrick

Reputation: 37008

Going with strict Groovy logic (whether this is true for Gradle might be a different question), this is handling a missing method.

x(y) { ... } is syntactic sugar for x(y, { ... }) (Groovy allows to exclude a closure from the regular arguments, if it's last).

It works roughly like this:

class Publications {
    def publications = [:]
    def methodMissing(String name, args) {
        publications[name] = args 
    }
    def call(c) {
        c.delegate = this
        c.call()
    }
}


def publications = new Publications()
publications {
    test(42)
}

assert [test: [42]] == publications.publications

If you want to dig into this specifically for Gradle, you can start from here: https://github.com/gradle/gradle/blob/9b23f11cf260d5d945b34625849a369937d46f94/subprojects/publish/src/main/java/org/gradle/api/publish/PublishingExtension.java#L81-L108

Upvotes: 2

Related Questions