According to documentation publishing to artifacto...
# gradle
p
According to documentation publishing to artifactory of a multiplatform project should be done by:
Copy code
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId = 'com.example.android'
                artifactId = 'test'
                version = '0.0.1'
            }
            debug(MavenPublication) {
                from components.debug
                groupId = 'com.example.android'
                artifactId = 'test-debug'
                version = '0.0.1'
            }
        }
    }
}
But this doesn’t compile for my project. release(MavenPublication) is not found. Tried this way as well, but then I get error on aar-file:
Copy code
create<MavenPublication>("maven") {
                groupId = "com.example.android"
                artifactId = "test"
                version = "0.1.1"
                artifact("$buildDir/outputs/aar/lib-debug.aar")
            }
v
afterEvaluate
is almost always a bad idea, but does just symptom treatment for the price of adding ordering problems, timing problems, and race conditions. I have no idea about multiplatform, so don't know whether it uses
afterEvaluate
itself which might cause a need to follow suit with the bad practice. But besides that, the upper syntax is Groovy DSL, the lower is Kotlin DSL. So if you put the upper in a Kotlin DSL build script, it will of course fail, yes.
You want
create<MavenPublication>("release") {
and
create<MavenPublication>("debug") {
.
And better do not use
artifact(...)
, that is bad practice too. Especially when there are components that you can publish, so even in the Kotlin DSL version publish the components, not hard-coded artifacts that then will not be built, or will be stale, and just be unreliable that way.
p
Could be afterEvaluate I’ve tried with and without it. But I only have one gradle task: publishToMavenLocal, how would I then enforce only publish debug for instance? instead of artifact(… ) what should I use? from components.debug (release) does not work for the second scenario.
v
components["debug"]
as documented in the Gradle docs ;-)
how would I then enforce only publish debug for instance
publishDebugPublicationToMavenLocal
publishToMavenLocal
is just a convenience lifecycle task to publish all publications to maven local
And if you want to publish to maven local for another Gradle build to consume it, better not do it and better avoid maven local wherever possible as it makes builds slower and is broken by design. Instead better use a composite build
p
I don’t find the documents very clear on how to publish.
Copy code
create<MavenPublication>("debug") {
            groupId = "groupid"
            artifactId = "artifactid"
            version = "0.1-SNAPSHOT"
            from(components["debug"])
        }
gives me:
SoftwareComponentInternal with name 'debug' not found.
So the component specification seems missing or wrong.
OK, thanks, what actually worked is: _create_<MavenPublication>(“DebugAar”) { groupId = “com.somegroup” artifactId = “library” version = “0.1-SNAPSHOT” afterEvaluate { artifact(tasks.getByName(“bundleDebugAar”)) } }
v
Don't use
artifact
I guess the component is indeed badly added by the plugin in
afterEvaluate
so you might indeed need to use it too
But please still publish the component, or does it also not work in
afterEvaluate
?
p
The problem is this line:
from(_components_["DebugAar"])
v
Why
DebugAar
? I thought
debug
At least that is what you had in your original post
p
Components doesn’t work for me at all and “release/debug” did not work. “Release/DebugAar” seems to work.
Copy code
create<MavenPublication>("ReleaseAar") {
    groupId = "com.company"
    artifactId = "library"
    version = project.version.toString()
    val artifactFile = "$buildDir/outputs/aar/${project.name}-release.aar"
    artifact(artifactFile)
}
And a println on components gives me:
Copy code
components.forEach { c ->
                println("Components: ${c.name}")
            }
Components: kotlin
v
Then you should probably open a docu bug issue. You said some documentation told you those components are there.
And again, with the setup as you just showed it, it might seem to work as you said, but it at best is flaky.
p
I wanna understand why it’s flaky? Documentation regarding this has been hard to find and google results provides like lots of different versions. I’ve read the gradle maven publish documentation but it hasn’t helped very much.
v
For example because you just configure a hard-coded path to publish without any dependency. If you try to publish from a clean worktree, it will fail with not finding the file. If you try to publish from a dirty worktree, it could well be you are publishing a stale file from earlier runs, not a current one. ... Hard-coding paths is practically always the wrong thing, except at the start of the task chain.
Usually it is better to wire some task to a component and publish that component. Or if you really want to publish an artifact without proper metadata, at least wire it properly. https://docs.gradle.org/current/userguide/publishing_customization.html#sec:publishing_custom_artifacts_to_maven should have all the details I think
p
Ok, thanks for detailed answer. It makes sense.
👌 1
140 Views