diff --git a/modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt b/modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt index 39fe450..58d80f5 100644 --- a/modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt +++ b/modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt @@ -183,18 +183,25 @@ constructor( devPubTasks: DevPublishTasksContainer, devPubConfigurations: DevPubConfigurationsContainer, ) { - devPubConfigurations.devPublishElements.configure { + val capability = devPublishCapabilityNotation(project.groupProvider, project.name).map { notation -> + "$notation:$DevPublishVersion" + } + + devPubConfigurations.devPublishApiElements.configure { + outgoing { + // Distinguishes this from `apiElements`. + // Without this, the TargetJvmVersion attribute is required, and it's impossible to guess the required value. + capability(capability) + } + } + + devPubConfigurations.devPublishRuntimeElements.configure { outgoing { artifact(devPubExtension.devMavenRepoMetadataDir) { builtBy(devPubTasks.generateDevPublishMetadata) } // Distinguishes this from `runtimeElements`. - // Without this, the TargetJvmVersion attribute is required, and it's impossible to guess the required value. - capability( - devPublishCapabilityNotation(project.groupProvider, project.name).map { notation -> - "$notation:$DevPublishVersion" - } - ) + capability(capability) } } } @@ -440,13 +447,19 @@ constructor( const val DEV_PUB__MAVEN_REPO_DIR = "maven-dev" - /** Name of the consumable Configuration behind [DevPublishPluginExtension.dependency]. */ - internal const val DEV_PUB__ELEMENTS_CONFIGURATION = "devPublishElements" + /** Name of the consumable Configuration behind [DevPublishPluginExtension.dependency], for compilation. */ + internal const val DEV_PUB__API_ELEMENTS_CONFIGURATION = "devPublishApiElements" + + /** Name of the consumable Configuration behind [DevPublishPluginExtension.dependency], for runtime. */ + internal const val DEV_PUB__RUNTIME_ELEMENTS_CONFIGURATION = "devPublishRuntimeElements" /** Name of the extension Dev Publish adds to [SigningExtension]. */ internal const val SIGNING__EXTERNAL_PUBLISHING_PROPERTY = "publishingOutsideDevRepo" - /** Group of the capability that identifies [DEV_PUB__ELEMENTS_CONFIGURATION]. */ + /** + * Group of the capability that identifies [DEV_PUB__API_ELEMENTS_CONFIGURATION] + * and [DEV_PUB__RUNTIME_ELEMENTS_CONFIGURATION]. + */ internal const val DEV_PUB__CAPABILITY_GROUP = "dev.adamko.dev-publish" /** Coordinates of the helper library that [DevPublishPluginExtension.dependency] brings in. */ @@ -471,8 +484,9 @@ constructor( /** * The [ProjectDependency] handed to users as [DevPublishPluginExtension.dependency]. * - * Targets [DEV_PUB__ELEMENTS_CONFIGURATION] of this same project, selected by capability - * because `runtimeElements` matches the same attributes and would otherwise be ambiguous. + * Targets [DEV_PUB__API_ELEMENTS_CONFIGURATION] or [DEV_PUB__RUNTIME_ELEMENTS_CONFIGURATION] of this + * same project, selected by capability because `apiElements` and `runtimeElements` match the same + * attributes and would otherwise be ambiguous. */ private fun createDevPublishDependency(project: Project): Provider { return devPublishCapabilityNotation(group = project.groupProvider, name = project.name).map { capability -> @@ -486,8 +500,8 @@ constructor( } /** - * Coordinates of the capability that distinguishes [DEV_PUB__ELEMENTS_CONFIGURATION] from the - * project's other variants. + * Coordinates of the capability that distinguishes [DEV_PUB__API_ELEMENTS_CONFIGURATION] and + * [DEV_PUB__RUNTIME_ELEMENTS_CONFIGURATION] from the project's other variants. * * Derived from the project's own coordinates, which are already required to be unique. */ diff --git a/modules/dev-publish-plugin/src/main/kotlin/data/DevPubAttributes.kt b/modules/dev-publish-plugin/src/main/kotlin/data/DevPubAttributes.kt index adb46e7..880267a 100644 --- a/modules/dev-publish-plugin/src/main/kotlin/data/DevPubAttributes.kt +++ b/modules/dev-publish-plugin/src/main/kotlin/data/DevPubAttributes.kt @@ -24,6 +24,10 @@ class DevPubAttributes( /** @see devPublishUsage */ val devPublishCategory: Category = objects.named("dev-publish") + val javaApiUsage: Usage = objects.named(Usage.JAVA_API) + val javaRuntimeUsage: Usage = objects.named(Usage.JAVA_RUNTIME) + val libraryCategory: Category = objects.named(Category.LIBRARY) + val mavenRepositoryType = "maven-repository" val mavenRepositoryChecksumsType = "maven-repository-checksums" @@ -32,8 +36,8 @@ class DevPubAttributes( val DevPublishTypeAttribute: Attribute = Attribute("dev.adamko.gradle.dev_publish.type") /** - * Indicates a [Configuration] contains the generated file recording the dev Maven repository - * location, and the library that reads it. + * Indicates a [Configuration] provides the library that reads the dev Maven repository location, + * and, for runtime, the generated file recording it. */ const val DEV_REPO_METADATA_KEY = "dev-maven-repo-metadata" } diff --git a/modules/dev-publish-plugin/src/main/kotlin/data/DevPubConfigurationsContainer.kt b/modules/dev-publish-plugin/src/main/kotlin/data/DevPubConfigurationsContainer.kt index d290ab5..a3ace1f 100644 --- a/modules/dev-publish-plugin/src/main/kotlin/data/DevPubConfigurationsContainer.kt +++ b/modules/dev-publish-plugin/src/main/kotlin/data/DevPubConfigurationsContainer.kt @@ -2,11 +2,12 @@ package dev.adamko.gradle.dev_publish.data -import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__ELEMENTS_CONFIGURATION +import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__API_ELEMENTS_CONFIGURATION import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__PUBLICATION_API_DEPENDENCIES import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__PUBLICATION_DEPENDENCIES import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__PUBLICATION_INCOMING import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__PUBLICATION_OUTGOING +import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__RUNTIME_ELEMENTS_CONFIGURATION import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__UTILS_CONFIGURATION import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__UTILS_GROUP import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__UTILS_MODULE @@ -84,14 +85,29 @@ class DevPubConfigurationsContainer( } } - val devPublishElements: NamedDomainObjectProvider = - configurations.consumable(DEV_PUB__ELEMENTS_CONFIGURATION) { + /** Only the library, so compiling against it doesn't trigger dev publishing. */ + val devPublishApiElements: NamedDomainObjectProvider = + configurations.consumable(DEV_PUB__API_ELEMENTS_CONFIGURATION) { + description = "The library that reads the dev Maven repository location." + + extendsFrom_(devPublishUtils) + + attributes { + attribute(USAGE_ATTRIBUTE, devPubAttributes.javaApiUsage) + attribute(CATEGORY_ATTRIBUTE, devPubAttributes.libraryCategory) + attribute(DevPublishTypeAttribute, DevPubAttributes.DEV_REPO_METADATA_KEY) + } + } + + val devPublishRuntimeElements: NamedDomainObjectProvider = + configurations.consumable(DEV_PUB__RUNTIME_ELEMENTS_CONFIGURATION) { description = "The dev Maven repository location, and the library that reads it." extendsFrom_(devPublishUtils) attributes { - // An attribute is required to trigger 'variant aware matching'. + attribute(USAGE_ATTRIBUTE, devPubAttributes.javaRuntimeUsage) + attribute(CATEGORY_ATTRIBUTE, devPubAttributes.libraryCategory) attribute(DevPublishTypeAttribute, DevPubAttributes.DEV_REPO_METADATA_KEY) } } diff --git a/modules/dev-publish-plugin/src/test/kotlin/DevPublishDependencyUsageTest.kt b/modules/dev-publish-plugin/src/test/kotlin/DevPublishDependencyUsageTest.kt new file mode 100644 index 0000000..295b94c --- /dev/null +++ b/modules/dev-publish-plugin/src/test/kotlin/DevPublishDependencyUsageTest.kt @@ -0,0 +1,59 @@ +package dev.adamko.gradle.dev_publish + +import dev.adamko.gradle.dev_publish.DevPublishPlugin.Companion.DEV_PUB__UTILS_CONFIGURATION +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.collections.shouldContain +import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder +import io.kotest.matchers.collections.shouldNotContain +import org.gradle.api.plugins.JavaLibraryPlugin +import org.gradle.kotlin.dsl.* +import org.gradle.testfixtures.ProjectBuilder + +/** + * Compiling tests must not trigger dev publishing. + * Only the test runtime needs the dev repo metadata. + */ +class DevPublishDependencyUsageTest : FunSpec({ + + val project = ProjectBuilder.builder() + .withName("foo-project") + .build() + + with(project) { + plugins.apply(JavaLibraryPlugin::class) + plugins.apply(DevPublishPlugin::class) + + // A local file instead of the published module, so resolving needs no repositories. + dependencies.add(DEV_PUB__UTILS_CONFIGURATION, files("dev-publish-utils.jar")) + dependencies.addProvider("testImplementation", extensions.getByType().dependency()) + } + + val metadataDir = project.layout.buildDirectory.dir("tmp/.maven-dev/metadata").get().asFile + val utilsJar = project.file("dev-publish-utils.jar") + + context("test compile classpath") { + val testCompileClasspath = project.configurations.getByName("testCompileClasspath") + + test("expect it contains dev-publish-utils, but not the dev repo metadata") { + testCompileClasspath.files shouldContainExactlyInAnyOrder listOf(utilsJar) + } + + test("expect it does not depend on generating the dev repo metadata") { + val taskNames = testCompileClasspath.buildDependencies.getDependencies(null).map { it.name } + taskNames shouldNotContain "generateDevPublishMetadata" + } + } + + context("test runtime classpath") { + val testRuntimeClasspath = project.configurations.getByName("testRuntimeClasspath") + + test("expect it contains dev-publish-utils and the dev repo metadata") { + testRuntimeClasspath.files shouldContainExactlyInAnyOrder listOf(utilsJar, metadataDir) + } + + test("expect it depends on generating the dev repo metadata") { + val taskNames = testRuntimeClasspath.buildDependencies.getDependencies(null).map { it.name } + taskNames shouldContain "generateDevPublishMetadata" + } + } +}) diff --git a/modules/dev-publish-plugin/src/testIntegration/kotlin/DevPublishDependencyClasspathTest.kt b/modules/dev-publish-plugin/src/testIntegration/kotlin/DevPublishDependencyClasspathTest.kt index 74a5ddb..f9b8523 100644 --- a/modules/dev-publish-plugin/src/testIntegration/kotlin/DevPublishDependencyClasspathTest.kt +++ b/modules/dev-publish-plugin/src/testIntegration/kotlin/DevPublishDependencyClasspathTest.kt @@ -21,7 +21,7 @@ class DevPublishDependencyClasspathTest : FunSpec({ "--configuration", "testCompileClasspath", ).build { output shouldNotContain "project ':lib'" - // the dev repo metadata, and the library that reads it, are still there + // the library that reads the dev repo metadata is still there output shouldContain "dev-publish-utils" } } diff --git a/modules/dev-publish-plugin/src/testIntegration/kotlin/JavaConsumerTest.kt b/modules/dev-publish-plugin/src/testIntegration/kotlin/JavaConsumerTest.kt index 59776cd..fb5d713 100644 --- a/modules/dev-publish-plugin/src/testIntegration/kotlin/JavaConsumerTest.kt +++ b/modules/dev-publish-plugin/src/testIntegration/kotlin/JavaConsumerTest.kt @@ -5,6 +5,7 @@ import dev.adamko.gradle.dev_publish.test_utils.GradleProjectTest.Companion.test import io.kotest.core.spec.style.FunSpec import io.kotest.core.test.TestScope import io.kotest.matchers.shouldBe +import io.kotest.matchers.string.shouldNotContain import kotlin.io.path.Path import kotlin.io.path.pathString import org.gradle.testkit.runner.TaskOutcome.* @@ -77,17 +78,42 @@ class JavaConsumerTest : FunSpec({ } } - test("expect devPublishElements carries a project-specific capability") { + test("expect devPublishApiElements carries a project-specific capability, and no artifacts") { project.runner .withArguments( ":outgoingVariants", - "--variant", "devPublishElements", + "--variant", "devPublishApiElements", "--quiet", ) .build { output.trim().normalizeVersion() shouldBe """ |-------------------------------------------------- - |Variant devPublishElements + |Variant devPublishApiElements + |-------------------------------------------------- + |The library that reads the dev Maven repository location. + | + |Capabilities + | - dev.adamko.dev-publish.foo.project:java-consumer-dev-publish:{version} + |Attributes + | - dev.adamko.gradle.dev_publish.type = dev-maven-repo-metadata + | - org.gradle.category = library + | - org.gradle.usage = java-api + """.trimMargin() + output shouldNotContain "\nArtifacts\n" + } + } + + test("expect devPublishRuntimeElements carries a project-specific capability, and the dev repo metadata") { + project.runner + .withArguments( + ":outgoingVariants", + "--variant", "devPublishRuntimeElements", + "--quiet", + ) + .build { + output.trim().normalizeVersion() shouldBe """ + |-------------------------------------------------- + |Variant devPublishRuntimeElements |-------------------------------------------------- |The dev Maven repository location, and the library that reads it. | @@ -95,6 +121,8 @@ class JavaConsumerTest : FunSpec({ | - dev.adamko.dev-publish.foo.project:java-consumer-dev-publish:{version} |Attributes | - dev.adamko.gradle.dev_publish.type = dev-maven-repo-metadata + | - org.gradle.category = library + | - org.gradle.usage = java-runtime |Artifacts | - ${Path("build/tmp/.maven-dev/metadata").pathString} """.trimMargin()