https://kotlinlang.org logo
Title
r

Reece H. Dunn

05/03/2023, 7:30 AM
How do you support multiple published JVM artifacts (one per JVM target)? -- This is so I can deploy e.g. JVM 11 and 17 artifacts for use in different IntelliJ releases. In my build.gradle.kts script for project A, I can set the artifactId of the MavenPublication so it gets deployed as
project-a-jvm11
, etc. This is similar to naming the native target based on the KonanTarget name. In my build.gradle.kts script for project B, I'm importing project A as
implementation("group-id:project-a:version")
. The build here is failing to locate the
project-a-jvm11
artifact, as it is looking for
project-a-jvm
. -- This is because it does not know about my custom naming scheme, but also does not know how to resolve the dependency based on the JVM target.
a

Adam S

05/03/2023, 7:37 AM
the multiplatform suffixes, like
-jvm
, are determined automatically using the name of the target. Updating the publication artifact name manually will probably disrupt that, and not in the way you want. If you wanted to change the name of the suffix then you can do that by renaming the target in the
build.gradle.kts
// build.gradle.kts

plugins {
  kotlin("multiplatform")
}

kotlin {
  jvm("jvm11") // custom target name of 'jvm11'
}
That will produce
group-id:project-a-jvm11:version
EDIT actually I’m not sure if it will… maybe the suffix is based on the platform, which will always be
-jvm
? 🤔 Try taking a look at this answer for an overview of how the suffixes work: https://stackoverflow.com/questions/73914158/how-does-gradle-choose-actually-libraries-ending-with-jvm/73919346#73919346
r

Reece H. Dunn

05/03/2023, 7:42 AM
Thanks.
a

Adam S

05/03/2023, 7:47 AM
what you could try some Gradle cleverness. You can add custom attributes to Kotlin targets https://kotlinlang.org/docs/multiplatform-set-up-targets.html#distinguish-several-targets-for-one-platform So if you used Gradle’s built-in
org.gradle.jvm.version
JVM attribute https://docs.gradle.org/current/userguide/variant_attributes.html#sub:jvm_default_attributes, that might be something useful…
kotlin {
  jvm("jvm11") {
        attributes.attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 11)
    }
}
but as I’m explaining it, I can see how complicated and confusing it is, and it’s probably unnecessary…
r

Reece H. Dunn

05/03/2023, 7:50 AM
That makes sense. I'll take a look.
Adding that attribute works. Thanks.
a

Adam S

05/03/2023, 8:09 AM
good to hear, although I am very surprised it was so simple! Normally I expect a big struggle when I try being clever with Gradle
r

Reece H. Dunn

05/03/2023, 8:11 AM
It's the way that the resolution works for multiple native artifacts. -- Kotlin adds the configured KonanTarget as an attribute, so it knows which native build to use.