Publishing is not working after the upgrade to 5.0

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?

1 Like

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"
}

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.

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

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

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

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.

use this may help you

configure<PublishingExtension> {
repositories {
mavenLocal()
maven {
setUrl(“https://xxxx.xx.xxxx.com/repository/maven-snapshots/”)
}
}
publications {
register(“mavenJava”, MavenPublication::class) {
from(components[“java”])
}
}
}

Hello,

This is most likely caused by the changes in making the publications block eager instead of lazy in 5.0. See the second bullet point under this section.

In your case, moving the tasks creation to before the publishing block should be sufficient.

This also has been reported in a slightly different form as https://github.com/gradle/gradle/issues/8707 which links to a slightly different workaround based on the use case.