Does anyone have an example of tweaking the adding...
# gradle
m
Does anyone have an example of tweaking the adding a
kotlinMultiplatform
publication to only include a subset of the target publications? Use case is a Gradle plugin integration tests where I'd like to save publishing all the native targets since I'm only testing JVM + host.
a
there's a few docs that show how to make the publishing tasks conditional using a predicate and
onlyIf {}
https://docs.gradle.org/current/userguide/publishing_customization.html#sec:publishing_maven:conditional_publishing You could skip the tasks if the module name doesn't end in
-jvm
, unless it has no target suffix?
m
Sorry I put this wrong. I meant more to "add" a separate kotlinMultiplatform publication
The existing one should continue working as before for regular kmp publican
And I don't think I can make a task conditional depending what task is requested?
Or maybe I can ? But feels weird
a
it's weird but you can :)
👀 1
hacky example for a different problem: https://github.com/cashapp/redwood/pull/823#issuecomment-1521861045
Copy code
// build.gradle.kts

tasks.matching {
  "CInteropMetadataDependencyTransformationTask" in (it::class.qualifiedName ?: "")
}.configureEach {
  // disable the IDE task when generating documentation
  enabled = gradle.taskGraph.allTasks.none {
    it is org.jetbrains.dokka.gradle.AbstractDokkaTask
  }
}
mind blown 1
m
Wow ok, will try that, thanks!
a
another example
Copy code
tasks.dokkaHtml {
    // Help improve development & integration test speeds, which publish
    // Dokka to MavenLocal but these tests don't require documentation.
    val localPublicationPredicate = provider {
        gradle.taskGraph.allTasks.any { it is PublishToMavenLocal || it is AbstractTestTask }
    }
    onlyIf("running tests or not publishing to MavenLocal") {
        !localPublicationPredicate.get()
    }
    outputDirectory.set(layout.buildDirectory.dir("dokka"))
}
🎯 1
thank you color 1
I will leave it up to the reader to judge whether it's a clever ploy or an ugly hack
😄 1
m
So that works with 2 caveats: • the .module file still reference the skipped publication (watchosArm64 for an example) so it's a bit misleading • but most importantly, the dependencies of
publishWatchosArm64PublicationToPluginTestRepository
are not skipped so I still pay the price of all the
compileWatchosArm64
and
linkWatchosArm64
tasks (edit: well not link because it's a lib and not a binary but still)
a
ahh yes, good points
m
I lied,
publishKotlinMultiplatformPublication
does not depend on
publishWatchosArm64Publication
. I was calling
publishAllPublications
which obviously runs
wachosArm64
and everything 🤦‍♂️. Looks like my problem is as easy as calling each publish task separately and avoiding the ones I don't want. The module file will contain invalid links but I guess that's not too bad
Thanks for the discussion and troubleshooting!
(continuing my journey) part of the reason I want to do this is also to skip dokka configuration because not compatible with CC and using the
taskGraph
isn't really possible because it's not available at configuration time (from what I can see) and execution time is too late because dokka has already been configured
This is how I ended up doing it, not super happy about it though as it uses private APIs through reflection
Copy code
@Suppress("UNCHECKED_CAST")
private val KotlinTarget.kotlinComponents: Set<KotlinTargetComponent>
  get() {
    return this::class.java.getDeclaredMethod("getKotlinComponents").invoke(this) as Set<KotlinTargetComponent>
  }

private fun KotlinTarget.createMavenPublications(publications: PublicationContainer) {
  components
      .map { gradleComponent -> gradleComponent to kotlinComponents.single { it.name == gradleComponent.name } }
      .forEach { (gradleComponent, kotlinComponent) ->
        publications.create("${kotlinComponent.name}Test", MavenPublication::class.java).apply {
          from(gradleComponent)
          (this as MavenPublicationInternal).publishWithOriginalFileName()
          artifactId = kotlinComponent.defaultArtifactId
        }
      }
}