Publishing is not working after the upgrade to 5.0


(Siarhei) #1

I’m publishing some Gradle-built JARs in Bintray. Here is my build.gradle.kts for 4.x:

plugins {
    `maven-publish`
}

publishing {
    publications {
        val main by creating(MavenPublication::class) {
            from(components["java"])

            val sourcesJar by tasks
            val javadocJar by tasks

            artifact(sourcesJar)
            artifact(javadocJar)
        }
    }
}

bintray {
    publish = true
    setPublications("main")
    …
}

tasks {
    val sourcesJar by creating(Jar::class) {
        from(sourceSets["main"].allJava)
        classifier = "sources"
    }

    val javadocJar by creating(Jar::class) {
        val javadoc by tasks

        from(javadoc)
        classifier = "javadoc"
    }
}

It is a working config for 4.x. However, after upgrade to 5.0 I got error:

Task with name 'sourcesJar' not found in project ':model'.

I guess it was caused by some kind of a lazy initialization in publishing block: val sourcesJar by tasks must be working lazy, because actually the tasks were created below in the tasks block. Seems that in Gradle 5.x this is not the case and by tasks delegate became eager and now it cannot found a task before it was created. Is this a correct guess? Is there any lazy API for getting and configuring tasks in 5.x?


Painfully UX: Simple use-case overly hard (Gradle 5, Kotlin DSL, multi-modules, java-library with Maven release)
(Ben) #2

Struggling with Gradle 5 Kotlin DSL on my own. But according to https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:complete_example the currently proposed way is:

task<Jar>("sourcesJar") {
    from(sourceSets.main.get().allJava)
    classifier = "sources"
}

task<Jar>("javadocJar") {
    from(tasks.javadoc)
    classifier = "javadoc"
}

(Siarhei) #3

Nope, that didn’t work. And, I guess, that DSL existed before. The benefit of using a delegate was having a task object that can be referenced later (e.g. in dependsOn). I want to avoid strings as much as possible.


(Ben) #4

I think this should work. Did you also add / enable the Java Plugin you are using here?

plugins {
    `maven-publish`,
    `java-library`
}

(Siarhei) #5

Yes, I have those plugins. In some projects java, in others — java-library. It’s not an issue. The issue is that in Gradle 5.0 you need to declare tasks { } before referencing a task in a plugin configuration closure, while in Gradle 4.x you could have a plugin configuration block before above the tasks block. So it was kind of a lazy resolution then, and now it works eagerly (and fails, unless you reorder the blocks).


(Ben) #6

Thanks for sharing your insights! Seems to be more or less very related to my issues in failing to get a javadocJar or sourceJar running for a multi-module project.