I am having problems when trying to use maven-publish plugin from AS.
I tried that example with a project and it works without problem. But as soon as I move to kotlin dsl, I have this issue:
SoftwareComponentInternal with name 'release' not found.
This is my first time dealing with kotlin dsl. First, I do not know if you can have both, kotlin dsl and groovy, but I tried that the first time by just adding kotlin dsl to the root and the app:build.gradle. I have this error so I decided to migrate also the library to kotlin dsl: mylib:build.gradle. I ended having this code:
plugins {
id(BuildPlugins.androidLibrary)
id(BuildPlugins.kotlinAndroid)
id(BuildPlugins.kotlinAndroidExtensions)
id(BuildPlugins.mavenPublish)
}
afterEvaluate {
publishing {
publications {
// Creates a Maven publication called "release".
create<MavenPublication>("release") {
// Applies the component for the release build variant.
from(components["release"])
// You can then customize attributes of the publication as shown below.
groupId = "com.mylib"
artifactId = "alpha"
version = "0.1"
}
}
}
}
Any idea about this and how to solve it?
So far I could come with this solution when using kotlin dsl:
Keep in mind that in this case, the solution posted is in groovy. But you can do it in kotlin dsl without problem.
Why is this in groovy? Well, the problem I had started when I included kotlin dsl in the app module, even though I had groovy gradle files in the submodules (which I want to publish as libraries too).
In this example, I am publishing a debug and a release version.
afterEvaluate {
publishing {
publications {
def groupIdPublication = 'com.mypackage'
def artifactIdPublication = "util"
// Creates a Maven publication called "release".
release(MavenPublication) {
// Applies the component for the release build variant.
//from components.release - this is not working
// You can then customize attributes of the publication as shown below.
groupId = groupIdPublication
artifactId = artifactIdPublication
version = '0.1'
artifact("$buildDir/outputs/aar/${project.name}-release.aar") // this is the solution I came up with
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
applyDependenciesToPOM(dependenciesNode, configurations.api.allDependencies)
applyDependenciesToPOM(dependenciesNode, configurations.implementation.allDependencies)
}
}
// Creates a Maven publication called “debug”.
debug(MavenPublication) {
// Applies the component for the debug build variant.
//from components.debug - this is not working
groupId = groupIdPublication
artifactId = artifactIdPublication
version = 'debug-0.1'
artifact("$buildDir/outputs/aar/${project.name}-debug.aar") // this is the solution I came up with
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
applyDependenciesToPOM(dependenciesNode, configurations.api.allDependencies)
applyDependenciesToPOM(dependenciesNode, configurations.implementation.allDependencies)
}
}
}
}
}
static def applyDependenciesToPOM(Object dependenciesNode, DependencySet allDependencies) {
allDependencies.each {
if (it.group != null && (it.name != null && !it.name.equals("unspecified")) &&
(it.version != null && !it.version.equals("unspecified"))) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}