is there a way to apply a gradle plugin to an indi...
# multiplatform
r
is there a way to apply a gradle plugin to an individual target?
🚫 1
👌 1
a
Gradle Plugins can be applied to any Java type, so you could create a plugin that will configure a Kotlin Multiplatform target. But that’s probably not what you’re asking… what do you want to achieve?
r
What im trying to do use entire specific gradle plugins for a target like https://github.com/PaperMC/paperweight as i have multiple targets built for different versions and platforms and each require a different setup for paperweight and some other plugins
a
to keep things separated I’d probably split the project up into separate Gradle subprojects rather than trying to manage variants via Kotlin targets
•
:my-project-core
- the core project, with all the variants, and Paperweight specific code if you like, but no Paperweight plugin •
:my-project-papermc-variant-a
- some variant of the core - apply the Paperweight plugin in here •
:my-project-papermc-variant-b
etc to help out with that you could create a buildSrc convention plugin, and in that just register all the Kotlin and Paperweight targets and their links - nothing complicated.
Copy code
//$projectRoot/buildSrc/src/main/kotlin/kotlin-multiplatform-paperweight.gradle.kts

plugins { 
   kotlin("multiplatform") // version must be set by a dependency in $projectRoot/buildSrc/build.gradle.kts
}

kotlin {
  // set up targets...
  jvm()

  // create source sets for the 'core' and 'variants' each variant
  sourceSets {
    val commonMain by getting {}

    val paperweightMain by creating { dependsOn(commonMain) }
    val paperweightVariantA by creating { dependsOn(paperweight) }
    val paperweightVariantB by creating { dependsOn(paperweight) }
    val paperweightVariantC by creating { dependsOn(paperweight) }
  }
}
Then you could apply
kotlin-multiplatform-paperweight
to each subproject, and Gradle would generate the Kotlin DSL accessors, so you could easily add dependencies on each project.
Copy code
// $projectRoot/my-project-papermc-variant-a/build.gradle.kts

plugins {
  `kotlin-multiplatform-paperweight`
}

// apply any variant-specific Paperweight Gradle config
// ...

kotlin {
  // can set up extra targets if necessary, but jvm() is provided by the convention

  sourceSets {
    paperweightVariantA { // can use the Kotlin DSL generated accessor
      dependencies {
        implementation(projects(":my-project-core"))
      }
    }
  }
}
actually maybe you’d want to define new Kotlin targets in the buildSrc convention plugin. Then the project dependencies would work out better. But that’s not something I’ve played around with…
r
that sounds like it could work, thanks