This question asks how to build a SourceJar with Gradle. How can I do the same with the Gradle Kotlin DSL?
The gradle code would be:
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
With Gradle 5.3.1, this is a little nicer and avoids deprecated API:
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}
val javadocJar by creating(Jar::class) {
dependsOn.add(javadoc)
archiveClassifier.set("javadoc")
from(javadoc)
}
artifacts {
archives(sourcesJar)
archives(javadocJar)
archives(jar)
}
}
Task assemble
will create all the artifacts.
As of Gradle 6.0 this is a much easier and cleaner. All you need to do is:
java {
withSourcesJar()
withJavadocJar()
}
Check out the documentation on the java extension, and its functions, withSourcesJar() and withJavadocJar()
All described methods fail with error on Gradle 6.6:SourceSet with name 'main' not found
I've found a solution that works:
tasks {
val sourcesJar by creating(Jar::class) {
archiveClassifier.set("sources")
from(android.sourceSets.getByName("main").java.srcDirs)
}
artifacts {
archives(sourcesJar)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With