Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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. */
Expand All @@ -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<Dependency> {
return devPublishCapabilityNotation(group = project.groupProvider, name = project.name).map { capability ->
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -32,8 +36,8 @@ class DevPubAttributes(
val DevPublishTypeAttribute: Attribute<String> =
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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,14 +85,29 @@ class DevPubConfigurationsContainer(
}
}

val devPublishElements: NamedDomainObjectProvider<ConsumableConfiguration> =
configurations.consumable(DEV_PUB__ELEMENTS_CONFIGURATION) {
/** Only the library, so compiling against it doesn't trigger dev publishing. */
val devPublishApiElements: NamedDomainObjectProvider<ConsumableConfiguration> =
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<ConsumableConfiguration> =
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DevPublishPluginExtension>().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"
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -77,24 +78,51 @@ 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.
|
|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-runtime
|Artifacts
| - ${Path("build/tmp/.maven-dev/metadata").pathString}
""".trimMargin()
Expand Down
Loading