The following worked for me in Groovy
group = [groupId]
version = [version]
task javadoc(type: Javadoc) {
configurations.implementation.canBeResolved(true)
configurations.api.canBeResolved(true)
failOnError false
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
//destinationDir = file("../javadoc/")
classpath += configurations.api
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
archiveClassifier = "sources"
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
artifact(sourcesJar)
// You can then customize attributes of the publication as shown below.
groupId = 'com.github.[name].[repo]'
artifactId = '[submodule name]'
version = '[version]'
pom.withXml {
def dependenciesNode = (asNode().get("dependencies") as groovy.util.NodeList).get(0) as groovy.util.Node
def configurationNames = ["implementation", "api"]
configurationNames.forEach { configurationName ->
configurations[configurationName].allDependencies.forEach {
if (it.group != null && it.version != "unspecified") {
def dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
// dependencyNode.appendNode("scope", configurationName)
}
}
}
}
}
}
}
}
And the following worked for me in KTS
group = [groupId]
version = [version]
val sourcesJar by tasks.registering(Jar::class) {
from(android.sourceSets["main"].java.srcDirs)
archiveClassifier.set("sources")
}
val javadoc by tasks.registering(Javadoc::class) {
configurations.implementation.get().isCanBeResolved = true
configurations.api.get().isCanBeResolved = true
isFailOnError = false
source = android.sourceSets["main"].java.getSourceFiles()
classpath += project.files(android.bootClasspath.joinToString(separator = File.pathSeparator))
classpath += configurations.api
}
// build a jar with javadoc
val javadocJar by tasks.registering(Jar::class) {
dependsOn(javadoc)
archiveClassifier.set("javadoc")
from(javadoc.get().destinationDir)
}
artifacts {
archives(sourcesJar)
archives(javadocJar)
}
afterEvaluate {
publishing {
publications {
register("mavenJava", MavenPublication::class) {
groupId = "com.github.[name].[repo]"
artifactId = "[submodule name]"
version = "[version]"
from(components["release"])
artifact(sourcesJar.get())
pom.withXml {
val dependenciesNode: groovy.util.Node =
(asNode().get("dependencies") as groovy.util.NodeList).get(0) as groovy.util.Node
val configurationNames = arrayOf("implementation", "api")
configurationNames.forEach { configurationName ->
configurations[configurationName].allDependencies.forEach {
if (it.group != null && it.version != "unspecified") {
val dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
// dependencyNode.appendNode("scope", configurationName)
}
}
}
}
}
}
}
}
Beware that even with the withXML
block, the transitive dependencies of api
dependencies don't get added automatically. I had to add them manually.