Gradle publication block - Demystify the DSL


(Vijay Vepakomma) #1

I am trying to understand how a Gradle pro would read this block.

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            artifact sourceJar {
                classifier "sources"
            }
        }
    }
}

What is the call stack if we convert this to java or groovy code?
Based on intellisense from IntelliJ idea, I tried this one:

publishing{
    publications{ pubContainer ->

        pubContainer.create('mavenJava').asType(MavenPublication.class){

        }

    }
}

Getting this error:

Cannot create a Publication named 'mavenJava' because this container
 does not support creating elements by name alone.
 Please specify which subtype of Publication to create. Known subtypes are: MavenPublication

(Vijay Vepakomma) #2

Got it working as below. This code can be introspected better in IntelliJ:

publishing{
    publications{ pubContainer ->

        pubContainer.create('mavenJava', MavenPublication.class){ mavenPublication ->

            mavenPublication.from(components.getByName('java'))
            mavenPublication.artifact(sourceJar, { a->
                a.classifier('sources')
            })
        }

    }
}