@@ -4,6 +4,7 @@ import dev.adamko.gradle.dev_publish.data.DevPubAttributes
44import dev.adamko.gradle.dev_publish.data.DevPubConfigurationsContainer
55import dev.adamko.gradle.dev_publish.data.PublicationData
66import dev.adamko.gradle.dev_publish.internal.DevPublishInternalApi
7+ import dev.adamko.gradle.dev_publish.internal.DevPublishVersion
78import dev.adamko.gradle.dev_publish.internal.checksums.CreatePublicationChecksum.Companion.createPublicationChecksum
89import dev.adamko.gradle.dev_publish.internal.checksums.LoadPublicationChecksum.Companion.loadPublicationChecksum
910import dev.adamko.gradle.dev_publish.internal.checksums.checksumsToDebugString
@@ -14,6 +15,8 @@ import dev.adamko.gradle.dev_publish.utils.*
1415import javax.inject.Inject
1516import org.gradle.api.Plugin
1617import org.gradle.api.Project
18+ import org.gradle.api.artifacts.Dependency
19+ import org.gradle.api.artifacts.ProjectDependency
1720import org.gradle.api.file.FileCollection
1821import org.gradle.api.file.FileSystemOperations
1922import org.gradle.api.file.ProjectLayout
@@ -53,7 +56,9 @@ constructor(
5356) : Plugin <Project > {
5457
5558 override fun apply (project : Project ) {
56- val devPubExtension = project.extensions.createDevPublishExtension()
59+ val devPublishDependency = createDevPublishDependency(project)
60+
61+ val devPubExtension = project.extensions.createDevPublishExtension(devPublishDependency)
5762
5863 val devPubService = project.gradle.sharedServices.registerDevPubService(project.path)
5964
@@ -85,6 +90,13 @@ constructor(
8590 }
8691 }
8792
93+ configureDevMavenRepoMetadata(
94+ project = project,
95+ devPubExtension = devPubExtension,
96+ devPubTasks = devPubTasks,
97+ devPubConfigurations = devPubConfigurations,
98+ )
99+
88100 configureMavenPublishingPlugin(
89101 project = project,
90102 devPubExtension = devPubExtension,
@@ -103,15 +115,19 @@ constructor(
103115 }
104116 }
105117
106- private fun ExtensionContainer.createDevPublishExtension (): DevPublishPluginExtension {
118+ private fun ExtensionContainer.createDevPublishExtension (
119+ devPublishDependency : Provider <Dependency >,
120+ ): DevPublishPluginExtension {
107121 return create<DevPublishPluginExtension >(DEV_PUB__EXTENSION_NAME ).apply {
108122 devMavenRepo.convention(layout.buildDirectory.dir(DEV_PUB__MAVEN_REPO_DIR ))
109123
110124 val tmpDir = layout.buildDirectory.dir(" tmp/.$DEV_PUB__MAVEN_REPO_DIR /" )
111125
126+ devMavenRepoMetadataDir.convention(tmpDir.map { it.dir(" metadata" ) })
112127 stagingDevMavenRepo.convention(tmpDir.map { it.dir(" staging" ) })
113128 checksumsStore.convention(tmpDir.map { it.dir(" checksum-store" ) })
114129 publicationsStore.convention(tmpDir.map { it.dir(" publications-store" ) })
130+ dependency.convention(devPublishDependency)
115131 }
116132 }
117133
@@ -126,6 +142,32 @@ constructor(
126142 maxParallelUsages.set(1 )
127143 }
128144
145+ /* *
146+ * Expose the generated metadata file, and `dev-publish-utils`, through a consumable
147+ * Configuration, so [DevPublishPluginExtension.dependency] wires up a test source set on its own.
148+ */
149+ private fun configureDevMavenRepoMetadata (
150+ project : Project ,
151+ devPubExtension : DevPublishPluginExtension ,
152+ devPubTasks : DevPublishTasksContainer ,
153+ devPubConfigurations : DevPubConfigurationsContainer ,
154+ ) {
155+ devPubConfigurations.devPublishElements.configure {
156+ outgoing {
157+ artifact(devPubExtension.devMavenRepoMetadataDir) {
158+ builtBy(devPubTasks.generateDevPublishMetadata)
159+ }
160+ // Distinguishes this from `runtimeElements`.
161+ // Without this, the TargetJvmVersion attribute is required, and it's impossible to guess the required value.
162+ capability(
163+ devPublishCapabilityNotation(project.groupProvider, project.name).map { notation ->
164+ " $notation :$DevPublishVersion "
165+ }
166+ )
167+ }
168+ }
169+ }
170+
129171 /* * React to [MavenPublishPlugin], and configure the appropriate DevPublish tasks. */
130172 private fun configureMavenPublishingPlugin (
131173 project : Project ,
@@ -307,13 +349,64 @@ constructor(
307349
308350 const val DEV_PUB__MAVEN_REPO_DIR = " maven-dev"
309351
352+ /* * Name of the consumable Configuration behind [DevPublishPluginExtension.dependency]. */
353+ internal const val DEV_PUB__ELEMENTS_CONFIGURATION = " devPublishElements"
354+
355+ /* * Group of the capability that identifies [DEV_PUB__ELEMENTS_CONFIGURATION]. */
356+ internal const val DEV_PUB__CAPABILITY_GROUP = " dev.adamko.dev-publish"
357+
358+ /* * Coordinates of the helper library that [DevPublishPluginExtension.dependency] brings in. */
359+ internal const val DEV_PUB__UTILS_GROUP = " dev.adamko.gradle"
360+
361+ /* * @see DEV_PUB__UTILS_GROUP */
362+ internal const val DEV_PUB__UTILS_MODULE = " dev-publish-utils"
363+
364+ internal const val DEV_PUB__UTILS_CONFIGURATION = " devPublishUtils"
365+
310366 const val DEV_PUB__PUBLICATION_DEPENDENCIES = " devPublication"
311367 const val DEV_PUB__PUBLICATION_API_DEPENDENCIES = " devPublicationApi"
312368 const val DEV_PUB__PUBLICATION_INCOMING = " devPublicationResolvableElements"
313369 const val DEV_PUB__PUBLICATION_OUTGOING = " devPublicationConsumableElements"
314370
315371 private val logger = Logging .getLogger(DevPublishService ::class .java)
316372
373+ /* *
374+ * The [ProjectDependency] handed to users as [DevPublishPluginExtension.dependency].
375+ *
376+ * Targets [DEV_PUB__ELEMENTS_CONFIGURATION] of this same project, selected by capability
377+ * because `runtimeElements` matches the same attributes and would otherwise be ambiguous.
378+ */
379+ private fun createDevPublishDependency (project : Project ): Provider <Dependency > {
380+ return devPublishCapabilityNotation(group = project.groupProvider, name = project.name).map { capability ->
381+ createProjectDependency(project).apply {
382+ capabilities {
383+ requireCapability(capability)
384+ }
385+ because(" the DevPublish dev Maven repository, and the dev-publish-utils library that reads it" )
386+ }
387+ }
388+ }
389+
390+ /* *
391+ * Coordinates of the capability that distinguishes [DEV_PUB__ELEMENTS_CONFIGURATION] from the
392+ * project's other variants.
393+ *
394+ * Derived from the project's own coordinates, which are already required to be unique.
395+ */
396+ internal fun devPublishCapabilityNotation (group : Provider <String >, name : String ): Provider <String > =
397+ group.map { group ->
398+ buildString {
399+ append(DEV_PUB__CAPABILITY_GROUP )
400+ if (group.isNotEmpty()) {
401+ append(" ." )
402+ append(group)
403+ }
404+ append(" :" )
405+ append(name)
406+ append(" -dev-publish" )
407+ }
408+ }
409+
317410 private fun MavenPublication.getGenerateModuleMetadataTaskName (): String =
318411 " generateMetadataFileFor${name.uppercaseFirstChar()} Publication"
319412 }
0 commit comments