From c9c0b297957fba437c056a1615a99533a51b41f4 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:27:31 +0100 Subject: [PATCH 1/4] Move GenerateDevPublishMetadataTask helpers into its companion Keeps the helpers next to the task, ready for the repo path helper. --- .../api/dev-publish-plugin.api | 4 + .../tasks/GenerateDevPublishMetadataTask.kt | 121 +++++++++--------- 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/modules/dev-publish-plugin/api/dev-publish-plugin.api b/modules/dev-publish-plugin/api/dev-publish-plugin.api index fc5805f..8d0b325 100644 --- a/modules/dev-publish-plugin/api/dev-publish-plugin.api +++ b/modules/dev-publish-plugin/api/dev-publish-plugin.api @@ -35,11 +35,15 @@ public final class dev/adamko/gradle/dev_publish/tasks/BaseDevPublishTask$Compan } public abstract class dev/adamko/gradle/dev_publish/tasks/GenerateDevPublishMetadataTask : dev/adamko/gradle/dev_publish/tasks/BaseDevPublishTask { + public static final field Companion Ldev/adamko/gradle/dev_publish/tasks/GenerateDevPublishMetadataTask$Companion; public abstract fun getDevMavenRepo ()Lorg/gradle/api/file/DirectoryProperty; public final fun getDevMavenRepoRelativeToMetadataDir ()Lorg/gradle/api/provider/Provider; public abstract fun getOutputDirectory ()Lorg/gradle/api/file/DirectoryProperty; } +public final class dev/adamko/gradle/dev_publish/tasks/GenerateDevPublishMetadataTask$Companion { +} + public abstract class dev/adamko/gradle/dev_publish/tasks/GeneratePublicationDataChecksumTask : dev/adamko/gradle/dev_publish/tasks/BaseDevPublishTask { public abstract fun getOutputDirectory ()Lorg/gradle/api/file/DirectoryProperty; public abstract fun getPublicationData ()Lorg/gradle/api/NamedDomainObjectContainer; diff --git a/modules/dev-publish-plugin/src/main/kotlin/tasks/GenerateDevPublishMetadataTask.kt b/modules/dev-publish-plugin/src/main/kotlin/tasks/GenerateDevPublishMetadataTask.kt index 7a0bce5..30e48bf 100644 --- a/modules/dev-publish-plugin/src/main/kotlin/tasks/GenerateDevPublishMetadataTask.kt +++ b/modules/dev-publish-plugin/src/main/kotlin/tasks/GenerateDevPublishMetadataTask.kt @@ -143,78 +143,83 @@ constructor( .use { digestStream -> state.values .sorted() - .forEach { cacheValue -> digestStream.write("$cacheValue$CHECKSUM_CACHE_VALUE_SEPARATOR") } + .forEach { cacheValue -> + digestStream.write(cacheValue) + digestStream.write(CHECKSUM_CACHE_VALUE_SEPARATOR) + } } return md.digest().toHexString() } -} -/** Separates the normalized path from the cache value, in a state file line. */ -private const val STATE_FILE_PATH_SEPARATOR = '\t' + companion object { -/** Ends each line in the state file. */ -private const val STATE_FILE_LINE_SEPARATOR = '\n' + /** Separates the normalized path from the cache value, in a state file line. */ + private const val STATE_FILE_PATH_SEPARATOR = '\t' -/** Separates cache values in the repo checksum input, so concatenated values can't be ambiguous. */ -private const val CHECKSUM_CACHE_VALUE_SEPARATOR = '\n' + /** Ends each line in the state file. */ + private const val STATE_FILE_LINE_SEPARATOR = '\n' + /** Separates cache values in the repo checksum input, so concatenated values can't be ambiguous. */ + private const val CHECKSUM_CACHE_VALUE_SEPARATOR = "\n" -/** `parentDir:checksum`. The file name is excluded, because SNAPSHOT publishing renames files. */ -private fun computeCacheValue(file: Path, normalizedPath: String): String { - val parentDir = Path(normalizedPath).parent - ?.invariantSeparatorsPathString - ?: "" - return "${parentDir}:${file.checksum()}" -} + /** `parentDir:checksum`. The file name is excluded, because SNAPSHOT publishing renames files. */ + private fun computeCacheValue(file: Path, normalizedPath: String): String { + val parentDir = Path(normalizedPath).parent + ?.invariantSeparatorsPathString + ?: "" + return "${parentDir}:${file.checksum()}" + } -/** Read lines of `normalizedPathcacheValue`. */ -private fun readStateEntries(stateFile: Path): MutableMap { - if (!stateFile.exists()) { - return mutableMapOf() - } - - return stateFile.useLines(UTF_8) { lines -> - lines - .filter { it.isNotBlank() } - .associateTo(mutableMapOf()) { line -> - val parts = line.split(STATE_FILE_PATH_SEPARATOR, limit = 2) - require(parts.size == 2) { "Invalid state file line `$line` in ${stateFile.invariantSeparatorsPathString}" } - parts.run { first() to last() } + /** Read lines of `normalizedPathcacheValue`. */ + private fun readStateEntries(stateFile: Path): MutableMap { + if (!stateFile.exists()) { + return mutableMapOf() } - } -} -/** Write lines of `normalizedPathcacheValue`. */ -private fun writeStateEntries(stateFile: Path, state: Map) { - stateFile.writeText( - state.entries.joinToString("") { (normalizedPath, cacheValue) -> - buildString { - append(normalizedPath) - append(STATE_FILE_PATH_SEPARATOR) - append(cacheValue) - append(STATE_FILE_LINE_SEPARATOR) + return stateFile.useLines(UTF_8) { lines -> + lines + .filter { it.isNotBlank() } + .associateTo(mutableMapOf()) { line -> + val parts = line.split(STATE_FILE_PATH_SEPARATOR, limit = 2) + require(parts.size == 2) { "Invalid state file line `$line` in ${stateFile.invariantSeparatorsPathString}" } + parts.run { first() to last() } + } } - }, - UTF_8, - ) -} - -private fun updateState(state: MutableMap, fileChanges: Iterable) { - val (removalChanges, modificationChanges) = - fileChanges.partition { it.changeType == REMOVED } - - removalChanges.forEach { state.remove(it.normalizedPath) } + } - modificationChanges - .parallelStream() - .map { change -> - val file = change.file.toPath() - val cacheValue = computeCacheValue(file, change.normalizedPath) - change.normalizedPath to cacheValue + /** Write lines of `normalizedPathcacheValue`. */ + private fun writeStateEntries(stateFile: Path, state: Map) { + stateFile.writeText( + state.entries.joinToString("") { (normalizedPath, cacheValue) -> + buildString { + append(normalizedPath) + append(STATE_FILE_PATH_SEPARATOR) + append(cacheValue) + append(STATE_FILE_LINE_SEPARATOR) + } + }, + UTF_8, + ) } - .collect(Collectors.toList()) - .forEach { (normalizedPath, cacheValue) -> - state[normalizedPath] = cacheValue + + private fun updateState(state: MutableMap, fileChanges: Iterable) { + val (removalChanges, modificationChanges) = + fileChanges.partition { it.changeType == REMOVED } + + removalChanges.forEach { state.remove(it.normalizedPath) } + + modificationChanges + .parallelStream() + .map { change -> + val file = change.file.toPath() + val cacheValue = computeCacheValue(file, change.normalizedPath) + change.normalizedPath to cacheValue + } + .collect(Collectors.toList()) + .forEach { (normalizedPath, cacheValue) -> + state[normalizedPath] = cacheValue + } } + } } From 5bb6d653320a974f38895bce3a7165266f2b7d9a Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:29:23 +0100 Subject: [PATCH 2/4] Keep SNAPSHOT dev repos build-cacheable Re-publishing a SNAPSHOT renames every file with a new timestamp, so generateDevPublishMetadata always missed the build cache. Track the files without their paths, and add the repo paths, with timestamps replaced by SNAPSHOT, as a separate input. Key the checksum state by the path relative to the repo, since the tracked files' normalised paths no longer include it. --- .../tasks/GenerateDevPublishMetadataTask.kt | 76 ++++++++-- .../DevMavenRepoTrackedFilesPathsTest.kt | 141 ++++++++++++++++++ .../testIntegration/kotlin/BuildCacheTest.kt | 49 +++++- 3 files changed, 245 insertions(+), 21 deletions(-) create mode 100644 modules/dev-publish-plugin/src/test/kotlin/DevMavenRepoTrackedFilesPathsTest.kt diff --git a/modules/dev-publish-plugin/src/main/kotlin/tasks/GenerateDevPublishMetadataTask.kt b/modules/dev-publish-plugin/src/main/kotlin/tasks/GenerateDevPublishMetadataTask.kt index 30e48bf..e36d536 100644 --- a/modules/dev-publish-plugin/src/main/kotlin/tasks/GenerateDevPublishMetadataTask.kt +++ b/modules/dev-publish-plugin/src/main/kotlin/tasks/GenerateDevPublishMetadataTask.kt @@ -7,6 +7,7 @@ import dev.adamko.gradle.dev_publish.internal.DevPublishMetadata.REPO_CHECKSUM_F import dev.adamko.gradle.dev_publish.internal.DevPublishMetadata.REPO_LOCATION_FILE_NAME import dev.adamko.gradle.dev_publish.internal.checksums.checksum import dev.adamko.gradle.dev_publish.internal.failDevMavenRepoNotRelative +import dev.adamko.gradle.dev_publish.tasks.GenerateDevPublishMetadataTask.Companion.repoPath import dev.adamko.gradle.dev_publish.utils.nullOutputStream import java.nio.file.Path import java.security.DigestOutputStream @@ -22,7 +23,7 @@ import org.gradle.api.problems.Problems import org.gradle.api.provider.Provider import org.gradle.api.provider.SetProperty import org.gradle.api.tasks.* -import org.gradle.api.tasks.PathSensitivity.RELATIVE +import org.gradle.api.tasks.PathSensitivity.NONE import org.gradle.work.ChangeType.REMOVED import org.gradle.work.FileChange import org.gradle.work.Incremental @@ -69,11 +70,25 @@ constructor( */ @get:Incremental @get:InputFiles - @get:PathSensitive(RELATIVE) + @get:PathSensitive(NONE) // file names might include SNAPSHOT timestamps @get:IgnoreEmptyDirectories @DevPublishInternalApi abstract val devMavenRepoTrackedFiles: ConfigurableFileCollection + @get:Input + @get:Optional + internal val devMavenRepoTrackedFilesPaths: Provider> + get() { + return devMavenRepo.zip(devMavenRepoTrackedFiles.elements) { devMavenRepoDir, trackedFiles -> + val devMavenRepo = devMavenRepoDir.asFile.toPath() + trackedFiles + .map { f -> f.asFile.toPath() } + .filter { f -> f.isRegularFile() } + .map { f -> repoPath(devMavenRepo, f) } + .toSortedSet() + } + } + /** * Patterns of files to exclude from the dev Maven repository up-to-date check. * See [org.gradle.api.tasks.util.PatternFilterable] for syntax. @@ -130,10 +145,14 @@ constructor( } stateDir.createDirectories() - // normalizedPath -> cache value + // repoPath -> cache value val state: MutableMap = readStateEntries(stateFile) - updateState(state, inputChanges.getFileChanges(devMavenRepoTrackedFiles)) + updateState( + state = state, + fileChanges = inputChanges.getFileChanges(devMavenRepoTrackedFiles), + devMavenRepo = devMavenRepo.get().asFile.toPath(), + ) writeStateEntries(stateFile, state) @@ -162,16 +181,33 @@ constructor( /** Separates cache values in the repo checksum input, so concatenated values can't be ambiguous. */ private const val CHECKSUM_CACHE_VALUE_SEPARATOR = "\n" + /** The timestamp and build number in a SNAPSHOT file name, e.g. `-20261113.215135-1`. */ + private val SNAPSHOT_TIMESTAMP = Regex("""-\d{8}\.\d{6}-\d+""") + + /** + * Path of [file] relative to [devMavenRepo], with any SNAPSHOT timestamp replaced by `-SNAPSHOT`, + * so re-publishing a SNAPSHOT doesn't change it. + */ + private fun repoPath(devMavenRepo: Path, file: Path): String { + val normalizedFile = + if (file.parent?.name?.endsWith("-SNAPSHOT") == true) { + file.resolveSibling(file.name.replace(SNAPSHOT_TIMESTAMP, "-SNAPSHOT")) + } else { + file + } + return normalizedFile.relativeTo(devMavenRepo).invariantSeparatorsPathString + } + /** `parentDir:checksum`. The file name is excluded, because SNAPSHOT publishing renames files. */ - private fun computeCacheValue(file: Path, normalizedPath: String): String { - val parentDir = Path(normalizedPath).parent + private fun computeCacheValue(file: Path, repoPath: String): String { + val parentDir = Path(repoPath).parent ?.invariantSeparatorsPathString ?: "" return "${parentDir}:${file.checksum()}" } - /** Read lines of `normalizedPathcacheValue`. */ + /** Read lines of `repoPathcacheValue`. */ private fun readStateEntries(stateFile: Path): MutableMap { if (!stateFile.exists()) { return mutableMapOf() @@ -188,12 +224,12 @@ constructor( } } - /** Write lines of `normalizedPathcacheValue`. */ + /** Write lines of `repoPathcacheValue`. */ private fun writeStateEntries(stateFile: Path, state: Map) { stateFile.writeText( - state.entries.joinToString("") { (normalizedPath, cacheValue) -> + state.entries.joinToString("") { (repoPath, cacheValue) -> buildString { - append(normalizedPath) + append(repoPath) append(STATE_FILE_PATH_SEPARATOR) append(cacheValue) append(STATE_FILE_LINE_SEPARATOR) @@ -203,22 +239,30 @@ constructor( ) } - private fun updateState(state: MutableMap, fileChanges: Iterable) { + /** + * Keyed by [repoPath], not [FileChange.getNormalizedPath], + * because [devMavenRepoTrackedFiles] ignores paths, so its normalised paths are empty. + */ + private fun updateState( + state: MutableMap, + fileChanges: Iterable, + devMavenRepo: Path, + ) { val (removalChanges, modificationChanges) = fileChanges.partition { it.changeType == REMOVED } - removalChanges.forEach { state.remove(it.normalizedPath) } + removalChanges.forEach { state.remove(repoPath(devMavenRepo, it.file.toPath())) } modificationChanges .parallelStream() .map { change -> val file = change.file.toPath() - val cacheValue = computeCacheValue(file, change.normalizedPath) - change.normalizedPath to cacheValue + val repoPath = repoPath(devMavenRepo, file) + repoPath to computeCacheValue(file, repoPath) } .collect(Collectors.toList()) - .forEach { (normalizedPath, cacheValue) -> - state[normalizedPath] = cacheValue + .forEach { (repoPath, cacheValue) -> + state[repoPath] = cacheValue } } } diff --git a/modules/dev-publish-plugin/src/test/kotlin/DevMavenRepoTrackedFilesPathsTest.kt b/modules/dev-publish-plugin/src/test/kotlin/DevMavenRepoTrackedFilesPathsTest.kt new file mode 100644 index 0000000..85477e0 --- /dev/null +++ b/modules/dev-publish-plugin/src/test/kotlin/DevMavenRepoTrackedFilesPathsTest.kt @@ -0,0 +1,141 @@ +package dev.adamko.gradle.dev_publish + +import dev.adamko.gradle.dev_publish.tasks.DevPublishTasksContainer.Companion.GENERATE_DEV_PUBLISH_METADATA_TASK_NAME +import dev.adamko.gradle.dev_publish.tasks.GenerateDevPublishMetadataTask +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.nulls.shouldNotBeNull +import io.kotest.matchers.shouldBe +import java.nio.file.Path +import kotlin.io.path.createParentDirectories +import kotlin.io.path.writeText +import org.gradle.api.Project +import org.gradle.api.tasks.TaskContainer +import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.getByType +import org.gradle.kotlin.dsl.named +import org.gradle.testfixtures.ProjectBuilder + +class DevMavenRepoTrackedFilesPathsTest : FunSpec({ + + context("when the dev repo has a release version") { + val project = project() + writeRepoFiles( + project, + "foo/lib/1.0.0/lib-1.0.0-sources.jar", + "foo/lib/1.0.0/lib-1.0.0.jar", + "foo/lib/1.0.0/lib-1.0.0.pom", + ) + + test("expect paths are unchanged") { + project.tasks.generateDevPublishMetadata.shouldHaveTrackedFilesPaths( + "foo/lib/1.0.0/lib-1.0.0-sources.jar", + "foo/lib/1.0.0/lib-1.0.0.jar", + "foo/lib/1.0.0/lib-1.0.0.pom", + ) + } + } + + context("when the dev repo has a SNAPSHOT version") { + val project = project() + writeRepoFiles( + project, + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-20261113.215135-1-javadoc.jar", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-20261113.215135-1-sources.jar", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-20261113.215135-1.jar", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-20261113.215135-1.module", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-20261113.215135-1.pom", + ) + + test("expect timestamps are replaced with SNAPSHOT") { + project.tasks.generateDevPublishMetadata.shouldHaveTrackedFilesPaths( + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-SNAPSHOT-javadoc.jar", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-SNAPSHOT-sources.jar", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-SNAPSHOT.jar", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-SNAPSHOT.module", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-SNAPSHOT.pom", + ) + } + } + + context("when a SNAPSHOT version is re-published with a new timestamp and build number") { + val firstPublish = project() + writeRepoFiles( + firstPublish, + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-20261113.215135-1.pom", + ) + val secondPublish = project() + writeRepoFiles( + secondPublish, + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-20261114.084757-12.pom", + ) + + test("expect the same paths") { + val first = firstPublish.tasks.generateDevPublishMetadata.devMavenRepoTrackedFilesPaths.orNull + val second = secondPublish.tasks.generateDevPublishMetadata.devMavenRepoTrackedFilesPaths.orNull + first shouldBe second + } + } + + context("when a release version file name looks like a SNAPSHOT timestamp") { + val project = project() + writeRepoFiles( + project, + "foo/lib/1.0.0-20261113.215135-1/lib-1.0.0-20261113.215135-1.jar", + ) + + test("expect paths are unchanged") { + project.tasks.generateDevPublishMetadata.shouldHaveTrackedFilesPaths( + "foo/lib/1.0.0-20261113.215135-1/lib-1.0.0-20261113.215135-1.jar", + ) + } + } + + context("when the dev repo has excluded files") { + val project = project() + writeRepoFiles( + project, + "foo/lib/maven-metadata.xml", + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-20261113.215135-1.jar", + ) + + test("expect only tracked files") { + project.tasks.generateDevPublishMetadata.shouldHaveTrackedFilesPaths( + "foo/lib/1.0.0-SNAPSHOT/lib-1.0.0-SNAPSHOT.jar", + ) + } + } +}) { + companion object { + private fun project(): Project { + val project = ProjectBuilder.builder() + .withName("foo-project") + .build() + project.plugins.apply(DevPublishPlugin::class) + return project + } + + private fun Project.devMavenRepo(): Path = + extensions.getByType().devMavenRepo.get().asFile.toPath() + + private fun writeRepoFiles(project: Project, vararg paths: String) { + paths.forEach { path -> + project.devMavenRepo().resolve(path) + .createParentDirectories() + .writeText(path) + } + } + + private fun GenerateDevPublishMetadataTask.shouldHaveTrackedFilesPaths(vararg paths: String) { + devMavenRepoTrackedFilesPathsList.shouldContainExactly(*paths) + } + + private val TaskContainer.generateDevPublishMetadata: GenerateDevPublishMetadataTask + get() = named(GENERATE_DEV_PUBLISH_METADATA_TASK_NAME) + .orNull + .shouldNotBeNull() + + private val GenerateDevPublishMetadataTask.devMavenRepoTrackedFilesPathsList: List? + get() = devMavenRepoTrackedFilesPaths.orNull?.toList() + } +} diff --git a/modules/dev-publish-plugin/src/testIntegration/kotlin/BuildCacheTest.kt b/modules/dev-publish-plugin/src/testIntegration/kotlin/BuildCacheTest.kt index b10647d..e2c4113 100644 --- a/modules/dev-publish-plugin/src/testIntegration/kotlin/BuildCacheTest.kt +++ b/modules/dev-publish-plugin/src/testIntegration/kotlin/BuildCacheTest.kt @@ -6,10 +6,11 @@ import io.kotest.core.spec.style.FunSpec import io.kotest.core.test.TestScope import io.kotest.matchers.paths.shouldBeADirectory import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe import java.nio.file.Path -import kotlin.io.path.fileSize -import kotlin.io.path.isRegularFile -import kotlin.io.path.walk +import kotlin.io.path.* +import kotlin.io.path.PathWalkOption.INCLUDE_DIRECTORIES +import kotlin.time.Duration.Companion.seconds import org.gradle.testkit.runner.TaskOutcome.* class BuildCacheTest : FunSpec({ @@ -81,13 +82,51 @@ class BuildCacheTest : FunSpec({ } } } + + context("when a SNAPSHOT dev repo is rebuilt with the same contents") { + val project = project(version = "0.0.1-SNAPSHOT") + + test("populate generateDevPublishMetadata in build cache") { + project.runner + .withArguments(":generateDevPublishMetadata") + .build { + shouldHaveTaskWithAnyOutcome(":generateDevPublishMetadata", SUCCESS, FROM_CACHE, UP_TO_DATE) + } + } + + test("expect generateDevPublishMetadata is loaded from build cache") { + val firstJarName = snapshotJarName(project) + project.runner + .withArguments( + "clean", + ":generateDevPublishMetadata", + // shift Gradle's clock, so the SNAPSHOT file names get a different timestamp + "-Dorg.gradle.internal.test.clockoffset=${60.seconds.inWholeMilliseconds}", + ) + .build { + // without a new timestamp, this test proves nothing + snapshotJarName(project) shouldNotBe firstJarName + shouldHaveTaskWithOutcome(":generateDevPublishMetadata", FROM_CACHE) + } + } + } }) { companion object { private fun Path.recursiveFileSize(): Long = walk().filter { it.isRegularFile() }.sumOf { it.fileSize() } - private fun TestScope.project(): GradleProjectTest = + private fun snapshotJarName(project: GradleProjectTest): String = + project.projectDir + .resolve("build") + .walk(INCLUDE_DIRECTORIES) + .filter { it.isDirectory() && it.name.endsWith("-SNAPSHOT") } + .flatMap { it.listDirectoryEntries("*.jar") } + .firstOrNull() + ?.name + ?: error("No snapshot jar found") + + private fun TestScope.project(version: String = "0.0.1"): GradleProjectTest = gradleKtsProjectTest( projectName = "build-cache", testProjectPath = testCase.descriptor.slashSeparatedPath(), @@ -101,7 +140,7 @@ class BuildCacheTest : FunSpec({ |} | |group = "foo.project" - |version = "0.0.1" + |version = "$version" | |publishing { | publications { From e56429a8c60b5d8952e13fd53e354447aa0db953 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:39:32 +0100 Subject: [PATCH 3/4] Delete removed publications from the dev repo The publications store was shared as a whole, so a removed publication's files stayed there and were copied into the dev repo. Publish tasks now write to their own directories, and publishAllToDevRepo syncs only the current ones into the store. --- .../api/dev-publish-plugin.api | 3 ++ .../src/main/kotlin/DevPublishPlugin.kt | 15 ++++-- .../main/kotlin/DevPublishPluginExtension.kt | 7 ++- .../kotlin/tasks/DevPublishTasksContainer.kt | 11 ++-- .../kotlin/tasks/PublishAllToDevRepoTask.kt | 54 +++++++++++++++++++ .../kotlin/MultipleGradlePluginsTest.kt | 40 ++++++++++++++ 6 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 modules/dev-publish-plugin/src/main/kotlin/tasks/PublishAllToDevRepoTask.kt diff --git a/modules/dev-publish-plugin/api/dev-publish-plugin.api b/modules/dev-publish-plugin/api/dev-publish-plugin.api index 8d0b325..f6b8450 100644 --- a/modules/dev-publish-plugin/api/dev-publish-plugin.api +++ b/modules/dev-publish-plugin/api/dev-publish-plugin.api @@ -49,6 +49,9 @@ public abstract class dev/adamko/gradle/dev_publish/tasks/GeneratePublicationDat public abstract fun getPublicationData ()Lorg/gradle/api/NamedDomainObjectContainer; } +public abstract class dev/adamko/gradle/dev_publish/tasks/PublishAllToDevRepoTask : dev/adamko/gradle/dev_publish/tasks/BaseDevPublishTask { +} + public abstract class dev/adamko/gradle/dev_publish/tasks/UpdateDevRepoTask : dev/adamko/gradle/dev_publish/tasks/BaseDevPublishTask { public fun from (Lorg/gradle/api/provider/Provider;)V public abstract fun getDevRepo ()Lorg/gradle/api/file/DirectoryProperty; diff --git a/modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt b/modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt index f0207e6..4ab61fb 100644 --- a/modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt +++ b/modules/dev-publish-plugin/src/main/kotlin/DevPublishPlugin.kt @@ -228,7 +228,12 @@ constructor( usesService(devPubService) val stagingDevMavenRepo = devPubExtension.stagingDevMavenRepo - val publicationStore = devPubExtension.publicationsStore.dir(this@configurePublishToMavenRepositoryTask.name) + + val publicationDir = layout.buildDirectory.dir("tmp/.$DEV_PUB__MAVEN_REPO_DIR/publications/$name") + + devPubExtension.publicationDirs.from( + objects.fileCollection().from(publicationDir).builtBy(this) + ) // need to determine the repo lazily because the repo isn't set immediately val repoIsDevPub = providers.provider { repository?.name == DEV_PUB__MAVEN_REPO_NAME }.orElse(false) @@ -237,7 +242,7 @@ constructor( // Gradle already tracks every publishable file as an input, // so declaring an output is enough to make the task up-to-date when the publication is unchanged. outputs - .dir(publicationStore) + .dir(publicationDir) .withPropertyName("devPubPublicationStore") outputs.doNotCacheIf("this task only performs simple file modifications") { _ -> @@ -252,12 +257,12 @@ constructor( } } - doLast_("sync staging repo to publication store") { + doLast_("sync staging repo to publication dir") { if (repoIsDevPub.get()) { - logger.info { ("[$path] Syncing staging-dev-maven-repo to publication store ${publicationStore.get().asFile.invariantSeparatorsPath}") } + logger.info { ("[$path] Syncing staging-dev-maven-repo to publication dir ${publicationDir.get().asFile.invariantSeparatorsPath}") } fs.sync { from(stagingDevMavenRepo) - into(publicationStore) + into(publicationDir) } // clear the staging repo for the next task diff --git a/modules/dev-publish-plugin/src/main/kotlin/DevPublishPluginExtension.kt b/modules/dev-publish-plugin/src/main/kotlin/DevPublishPluginExtension.kt index 737dd6a..415639c 100644 --- a/modules/dev-publish-plugin/src/main/kotlin/DevPublishPluginExtension.kt +++ b/modules/dev-publish-plugin/src/main/kotlin/DevPublishPluginExtension.kt @@ -2,6 +2,7 @@ package dev.adamko.gradle.dev_publish import dev.adamko.gradle.dev_publish.internal.DevPublishInternalApi import org.gradle.api.artifacts.Dependency +import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.DirectoryProperty import org.gradle.api.provider.Property import org.gradle.api.provider.Provider @@ -66,13 +67,17 @@ abstract class DevPublishPluginExtension abstract val stagingDevMavenRepo: DirectoryProperty /** - * Temporary storage of individual publications. + * Temporary storage of this project's current publications, collected by `publishAllToDevRepo`. * * This value is intended for internal use and should not typically be configured in build scripts. */ @DevPublishInternalApi abstract val publicationsStore: DirectoryProperty + /** The repo written by each dev repo publish task. Added to by each task, when it is configured. */ + @DevPublishInternalApi + abstract val publicationDirs: ConfigurableFileCollection + /** * Location of stored * [dev.adamko.gradle.dev_publish.data.PublicationData] diff --git a/modules/dev-publish-plugin/src/main/kotlin/tasks/DevPublishTasksContainer.kt b/modules/dev-publish-plugin/src/main/kotlin/tasks/DevPublishTasksContainer.kt index e63e8e5..ba8613b 100644 --- a/modules/dev-publish-plugin/src/main/kotlin/tasks/DevPublishTasksContainer.kt +++ b/modules/dev-publish-plugin/src/main/kotlin/tasks/DevPublishTasksContainer.kt @@ -19,8 +19,8 @@ class DevPublishTasksContainer( private val problemsReporter: ProblemReporter, ) { - /** Lifecycle task for publishing dev repos to the current subproject's dev repo. */ - val publishAllToDevRepo: TaskProvider = + /** Collects the publications of the current subproject. */ + val publishAllToDevRepo: TaskProvider = tasks.registerPublishAllToDevRepoTask() /** No longer used: Gradle's own up-to-date checks decide when to publish. */ @@ -35,12 +35,13 @@ class DevPublishTasksContainer( val generateDevPublishMetadata: TaskProvider = tasks.registerGenerateDevPublishMetadataTask() - private fun TaskContainer.registerPublishAllToDevRepoTask(): TaskProvider = - register(PUBLISH_ALL_TO_DEV_REPO_TASK_NAME) { + private fun TaskContainer.registerPublishAllToDevRepoTask(): TaskProvider = + register(PUBLISH_ALL_TO_DEV_REPO_TASK_NAME) { description = "Publishes all Maven publications from this project into the dev Maven repository staging area." + "This is an internal task that should not typically be manually referenced or called." - outputs.dir(devPubExtension.stagingDevMavenRepo) + publicationDirs.from(devPubExtension.publicationDirs) + publicationsStore.convention(devPubExtension.publicationsStore) } @Suppress("DEPRECATION") diff --git a/modules/dev-publish-plugin/src/main/kotlin/tasks/PublishAllToDevRepoTask.kt b/modules/dev-publish-plugin/src/main/kotlin/tasks/PublishAllToDevRepoTask.kt new file mode 100644 index 0000000..cccb964 --- /dev/null +++ b/modules/dev-publish-plugin/src/main/kotlin/tasks/PublishAllToDevRepoTask.kt @@ -0,0 +1,54 @@ +package dev.adamko.gradle.dev_publish.tasks + +import dev.adamko.gradle.dev_publish.internal.DevPublishInternalApi +import javax.inject.Inject +import org.gradle.api.file.ConfigurableFileCollection +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.FileSystemOperations +import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity.ABSOLUTE +import org.gradle.api.tasks.TaskAction +import org.gradle.work.DisableCachingByDefault + +/** + * Collects the publications of this project into [publicationsStore]. + * + * Only the current publications are collected, + * so a removed publication's files are deleted from [publicationsStore]. + */ +@DisableCachingByDefault(because = "This task only relocates files") +abstract class PublishAllToDevRepoTask +@Inject +internal constructor( + private val fs: FileSystemOperations, +) : BaseDevPublishTask() { + + /** @see dev.adamko.gradle.dev_publish.DevPublishPluginExtension.publicationsStore */ + @get:OutputDirectory + @DevPublishInternalApi + abstract val publicationsStore: DirectoryProperty + + /** The repo written by each dev repo publish task, in a directory named after the task. */ + @get:InputFiles + // Absolute, because directory names are used in the output. + // Task is not cached, so relocation doesn't matter. + @get:PathSensitive(ABSOLUTE) + @DevPublishInternalApi + abstract val publicationDirs: ConfigurableFileCollection + + @TaskAction + @DevPublishInternalApi + protected fun collectPublications() { + fs.sync { + publicationDirs.forEach { dir -> + from(dir) { + into(dir.name) + } + } + into(publicationsStore) + includeEmptyDirs = false + } + } +} diff --git a/modules/dev-publish-plugin/src/testIntegration/kotlin/MultipleGradlePluginsTest.kt b/modules/dev-publish-plugin/src/testIntegration/kotlin/MultipleGradlePluginsTest.kt index 4a1b167..ace57d6 100644 --- a/modules/dev-publish-plugin/src/testIntegration/kotlin/MultipleGradlePluginsTest.kt +++ b/modules/dev-publish-plugin/src/testIntegration/kotlin/MultipleGradlePluginsTest.kt @@ -4,7 +4,10 @@ import dev.adamko.gradle.dev_publish.test_utils.* import io.kotest.core.spec.style.FunSpec import io.kotest.core.test.TestScope import io.kotest.matchers.nulls.shouldNotBeNull +import io.kotest.matchers.paths.shouldExist +import io.kotest.matchers.paths.shouldNotExist import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe import io.kotest.matchers.string.shouldContain import java.nio.file.Path import org.gradle.testkit.runner.TaskOutcome.* @@ -127,6 +130,43 @@ class MultipleGradlePluginsTest : FunSpec({ } } } + + context("when a plugin is removed") { + val project = project() + val mavenDevDir = project.projectDir.resolve("build/maven-dev") + + test("expect the plugin is published first") { + project.runner + .withArguments("updateDevRepo") + .forwardOutput() + .build { + mavenDevDir.resolve("plugin-gamma").shouldExist() + } + } + + test("expect the removed plugin is deleted from the dev repo") { + val buildGradleKtsWithGamma = project.buildGradleKts + project.buildGradleKts = buildGradleKtsWithGamma.replace( + """ + | plugins.register("gamma") { + | id = "plugin-gamma" + | displayName = "PluginGamma" + | implementationClass = "PluginGamma" + | } + |""".trimMargin(), + "", + ) + project.buildGradleKts shouldNotBe buildGradleKtsWithGamma + + project.runner + .withArguments("updateDevRepo") + .forwardOutput() + .build { + mavenDevDir.resolve("plugin-alpha").shouldExist() + mavenDevDir.resolve("plugin-gamma").shouldNotExist() + } + } + } }) private fun Path.shouldBeMavenDevRepoWithPlugins( From d37ce5f2a8218d55bb8d9be8bda6b5cf727c4164 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:27:00 +0100 Subject: [PATCH 4/4] Stop reporting tested-projects test results in GHA Only match the modules' own test results instead. action-junit-report makes a separate report from each line of report_paths, so the `!**/build/project-tests/**` line never excluded anything. --- .github/workflows/run_gradle_task.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/run_gradle_task.yml b/.github/workflows/run_gradle_task.yml index 409f4b1..6b4e0e8 100644 --- a/.github/workflows/run_gradle_task.yml +++ b/.github/workflows/run_gradle_task.yml @@ -109,7 +109,5 @@ jobs: uses: mikepenz/action-junit-report@v6 if: always() with: - report_paths: | - **/build/test-results/**/TEST-*.xml - !**/build/project-tests/** + report_paths: modules/*/build/test-results/**/TEST-*.xml require_tests: false