title: Publish Kotlin MPP metadata with Gradle Kotlin DSL
body: <p>I have created a Kotlin MPP to share Json utilities between JVM and JS. All the code lies in the common source set and I have configured the necessary targets with their respective dependencies. Without further configuration I'm able to use the utilities from both JVM and JS but not from the common source set of another MPP, which has to do with the way Gradle handles metadata.</p>
  <p>I already found the solution (taken from <a href="https://medium.com/xorum-io/crafting-and-publishing-kotlin-multiplatform-library-to-bintray-cbc00a4f770" rel="nofollow noreferrer">https://medium.com/xorum-io/crafting-and-publishing-kotlin-multiplatform-library-to-bintray-cbc00a4f770</a>)</p>
  <pre><code>afterEvaluate {
      project.publishing.publications.all {
          groupId = group
          if (it.name.contains('metadata')) {
              artifactId = &quot;$libraryName&quot;
          } else {
              artifactId = &quot;$libraryName-$name&quot;
          }
      }
  }
  </code></pre>
  <p>and I also got it to work with the Gradle Kotlin DSL:</p>
  <pre><code>afterEvaluate {
      publishing.publications.all {
          this as MavenPublication
          artifactId = project.name + &quot;-$name&quot;.takeUnless { &quot;metadata&quot; in name }.orEmpty()
      }
  }
  </code></pre>
  <p>However, this doesn't feel quite right yet.</p>
  <ol>
  <li><p>There is no such code snippet in the official documentation.</p>
  </li>
  <li><p>The documentation advertises that a single dependency from the common source set should suffice to automatically resolve target specific dependencies: <a href="https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#metadata-publishing" rel="nofollow noreferrer">https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#metadata-publishing</a>. I had to add the dependency for each target, respectively, for it to work.</p>
  </li>
  <li><p><code>this as MavenPublication</code> is necessary because <code>Publication</code> has no field <code>artifactId</code>.</p>
  </li>
  <li><p>I use <code>project.name</code> instead of <code>libraryName</code>.</p>
  </li>
  </ol>
  <p>Is this even remotely the right way to do things or am I missing some other option which would make the whole process trivial?</p>
  <p>Right now I'm using Kotlin <code>1.3.72</code> and Gradle <code>5.2.1</code> with <code>enableFeaturePreview(&quot;GRADLE_METADATA&quot;)</code> in <code>settings.gradle.kts</code>. I also tried it with Gradle <code>6.5.1</code> (latest) but it behaves exactly the same.</p>
  <p>For now I'm glad that it's working at all but I suspect there is a cleaner way to do this. I'd really appreciate if someone with a bit more Gradle expertise could clear things up for me or point me into the right direction.</p>
  <p>Edit:</p>
  <p><code>gradle.build.kts</code> for completeness. Although there isn't much going on here.</p>
  <pre><code>group = &quot;org.example&quot;
  version = &quot;1.0-SNAPSHOT&quot;
  
  plugins {
      kotlin(&quot;multiplatform&quot;) version &quot;1.3.72&quot;
      `maven-publish`
  }
  
  repositories {
      mavenCentral()
  }
  
  kotlin {
      jvm()
  
      sourceSets {
          val commonMain by getting {
              dependencies {
                  implementation(kotlin(&quot;stdlib-common&quot;))
              }
          }
  
          val jvmMain by getting {
              dependencies {
                  implementation(kotlin(&quot;stdlib&quot;))
              }
          }
      }
  }
  </code></pre>