Hugh Bollen
Hugh Bollen

Reputation: 83

How to set artifactId when publishing Kotlin Multiplatform Project to mavenLocal?

Background: I have a kotlin multiplatform project to publish to mavenLocal for use in another project. I'd like to be able to set the artifactId but this seems to be set elsewhere. After the publishToMavenLocal command executes and the project has been published locally the artifactId seems to be prefixed by a string from settings.gradle.kts specifically, include(":kotlin-xtype")

Example Output: Publishing Output

How can I set the artifactId outside of the settings.gradle? I've tried adding artifactId = "xyz" in the publishing block of build.gradle but this only changed the names of the metadata and multiplatform artifacts and didn't change the jvm or js artifacts.

Here is some of my build.gradle.kts:

plugins{
    `maven-publish`
    kotlin("multiplatform") version("1.4.20") //Version.kotlin)
    application
}

application {
    group = "nz.salect.xtypes"

    version = "0.0.2"
    mainClassName = "io.ktor.server.netty.EngineMain"
}
val javadocJar by tasks.creating(Jar::class) {
    from(tasks.getByName("javadoc"))
    archiveClassifier.set("javadoc")
}
//// The root publication also needs a sources JAR as it does not have one by default
val sourcesJar by tasks.creating(Jar::class) {
    archiveClassifier.value("sources")
}
publishing {
    publications.withType<MavenPublication>().all {
        artifact(javadocJar)
        artifactId = "xyz"
    }
}

publishing.publications.withType<MavenPublication>().getByName("kotlinMultiplatform").artifact(sourcesJar)

Upvotes: 6

Views: 2135

Answers (2)

goncalossilva
goncalossilva

Reputation: 1860

You can check for the publication name, and act accordingly.

Here's an example in Kotlin, supposing you have the id you want in the artifactId variable:

publishing {
  publications.withType<MavenPublication> {
    if (name == "kotlinMultiplatform") {
      setArtifactId(artifactId)
    } else {
      setArtifactId("$artifactId-$name")
    }
  }
  // ...
}

Upvotes: 6

Nicodemus Ojwee
Nicodemus Ojwee

Reputation: 822

Add the following to your publications block to generate the missing artifacts.

artifact(sourcesJar)

Upvotes: 0

Related Questions