The accepted answer is great, but I want to add that if you want to actually use the reference created by created
/ registering
call later, then there will be a difference in API. Compare
create<MavenPublication>("main") {
…
val sourcesJar by tasks.creating(Jar::class) {
val sourceSets: SourceSetContainer by project
from(sourceSets["main"].allJava)
classifier = "sources"
}
artifact(sourcesJar)
}
and
create<MavenPublication>("main") {
…
val sourcesJar by tasks.registering(Jar::class) {
val sourceSets: SourceSetContainer by project
from(sourceSets["main"].allJava)
classifier = "sources"
}
artifact(sourcesJar.get())
}
In case of registering, because it is lazy, you'll need an additional .get()
call, or you'll get an exception:
* What went wrong:
Cannot convert the provided notation to an object of type MavenArtifact: task ':experiments:sourcesJar'.
The following types/formats are supported:
- Instances of MavenArtifact.
- Instances of AbstractArchiveTask, for example jar.
- Instances of PublishArtifact
- Maps containing a 'source' entry, for example [source: '/path/to/file', extension: 'zip'].
- Anything that can be converted to a file, as per Project.file()