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