vanniktech / gradle-maven-publish-plugin

A Gradle plugin that publishes your Android and Kotlin libraries, including sources and javadoc, to Maven Central or any other Nexus instance.
https://vanniktech.github.io/gradle-maven-publish-plugin
Apache License 2.0
1.2k stars 110 forks source link

Add an option to not publish test fixtures #779

Open osipxd opened 1 month ago

osipxd commented 1 month ago

Context

I need to use test fixtures only internally between modules, so I don't want to publish them into Maven Central. I have the following code to exclude test fixtures from publication:

plugins.withId("org.gradle.java-test-fixtures") {
    val component = JavaPluginHelper.getJavaComponent(project) as DefaultJvmSoftwareComponent
    val feature = component.features.getByName(TestFixturesSupport.TEST_FIXTURES_FEATURE_NAME)
    component.withVariantsFromConfiguration(feature.apiElementsConfiguration) { skip() }
    component.withVariantsFromConfiguration(feature.runtimeElementsConfiguration) { skip() }
}

However, gradle-maven-publish-plugin forces publication of testFixturesSourcesElements whether or not other test fixture components are published.

Proposal

I see two options:

  1. Do not configure testFixturesSourcesElements publication if other test fixtures components are skipped from publication
  2. Add option to platform configuration allowing to disable test fixtures publication.
osipxd commented 1 month ago

However, I've found a workaround:

// Exclude test fixtures from publication, as we use it only internally
plugins.withId("org.gradle.java-test-fixtures") {
    val component = components["java"] as AdhocComponentWithVariants
    component.withVariantsFromConfiguration(configurations["testFixturesApiElements"]) { skip() }
    component.withVariantsFromConfiguration(configurations["testFixturesRuntimeElements"]) { skip() }

    // Workaround to not publish test fixtures sources added by com.vanniktech.maven.publish plugin
    // TODO: Remove as soon as https://github.com/vanniktech/gradle-maven-publish-plugin/issues/779 closed
    afterEvaluate {
        component.withVariantsFromConfiguration(configurations["testFixturesSourcesElements"]) { skip() }
    }
}
gabrielittner commented 1 month ago

Having one of the 2 options built into the plugin is reasonable, I'll look into that.

Could you also open a feature request for Gradle directly? This would be benefitial for either option, for option 1 it would mean that we could read whatever config they have to disable the publication and for option 2 it would mean we can use their API internally. It would also remove the need to access internal types like DefaultJvmSoftwareComponent.

osipxd commented 1 month ago

Created the issue for Gradle:

osipxd commented 1 month ago

Oh, it looks like it is actually possible to disable test fixtures publication without internal APIs, so I've updated the workaround to get rid of internal APIs usage.