From 1bf7413e77d916897eb69b2dec471b682cd021fb Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Fri, 9 Oct 2026 19:29:41 +0200 Subject: [PATCH 1/6] Add combined Central release workflow --- README.md | 4 + build.gradle | 7 + combined-releases.md | 75 ++++++ .../plugin/release/BundleAllTask.groovy | 34 +++ .../gradle/plugin/release/BundleTask.groovy | 9 +- .../plugin/release/BundleValidator.groovy | 5 + .../plugin/release/CombinedBundle.groovy | 66 +++++ .../plugin/release/CombinedDeployment.groovy | 52 ++++ .../plugin/release/PrepareMavenTask.groovy | 62 +++++ .../release/PublicationDescriptor.groovy | 24 ++ .../plugin/release/PublicationStatus.groovy | 65 +++++ .../plugin/release/ReleaseAllPlanTask.groovy | 101 ++++++++ .../release/ReleaseAllSupportPlugin.groovy | 17 ++ .../plugin/release/ReleaseAllTask.groovy | 53 ++++ .../plugin/release/ReleaseChecksums.groovy | 17 ++ .../plugin/release/ReleaseCoordinate.groovy | 29 +++ .../plugin/release/ReleaseManifest.groovy | 44 ++++ .../plugin/release/ReleasePlanner.groovy | 73 ++++++ .../gradle/plugin/release/ReleasePom.groovy | 53 ++++ .../release/ReleaseSourceGuardTask.groovy | 25 ++ .../release/SigningPreflightTask.groovy | 29 +++ .../plugin/release/CombinedReleaseTest.groovy | 238 ++++++++++++++++++ .../plugin/release/CombinedTasksTest.groovy | 114 +++++++++ 23 files changed, 1188 insertions(+), 8 deletions(-) create mode 100644 combined-releases.md create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/BundleAllTask.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/PrepareMavenTask.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/PublicationDescriptor.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/PublicationStatus.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllSupportPlugin.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseChecksums.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseCoordinate.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseSourceGuardTask.groovy create mode 100644 src/main/groovy/se/alipsa/gradle/plugin/release/SigningPreflightTask.groovy create mode 100644 src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy create mode 100644 src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy diff --git a/README.md b/README.md index 6a7b0b2..2bebc50 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,10 @@ nexusReleasePlugin { An explicit `githubRepo` always takes priority over auto-detection. +## Combined releases + +The opt-in `se.alipsa.nexus-release-all` support plugin adds manifest planning, guarded preparation, combined bundling and one-upload deployment recovery. See [the task contract and workflow](combined-releases.md). Existing single-module releases remain available. + ## Building the Plugin ```bash diff --git a/build.gradle b/build.gradle index 20a61a9..b80c324 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,13 @@ gradlePlugin { website.set("https://github.com/Alipsa/nexus-release-plugin") vcsUrl.set("https://github.com/Alipsa/nexus-release-plugin") plugins { + combinedRelease { + id = 'se.alipsa.nexus-release-all' + implementationClass = 'se.alipsa.gradle.plugin.release.ReleaseAllSupportPlugin' + displayName = 'Combined Central Release Support' + description = 'Plans, bundles and submits multiple publications in one Central deployment' + tags.set(['publish', 'release', 'central']) + } simplePlugin { id = 'se.alipsa.nexus-release-plugin' implementationClass = 'se.alipsa.gradle.plugin.release.NexusReleasePlugin' diff --git a/combined-releases.md b/combined-releases.md new file mode 100644 index 0000000..5a7553d --- /dev/null +++ b/combined-releases.md @@ -0,0 +1,75 @@ +# Combined release support + +The opt-in `se.alipsa.nexus-release-all` support plugin registers root tasks without +registering single-publication tasks or changing `se.alipsa.nexus-release-plugin`. +The existing `bundle` and `release` tasks remain available. + +Matrix provides the complete orchestration, prerequisites and command examples in +[its combined-release guide](https://github.com/Alipsa/matrix/blob/main/docs/releaseAll.md). +During development it resolves this checkout through a Gradle composite build; publishing +this plugin is a separate operation. + +## Task contract + +All file locations and credentials are configured explicitly through typed task properties. +The source repository must be clean and committed. Generated files belong in ignored outputs. + +| Task | Inputs and behavior | +| --- | --- | +| `releaseAllPlan` | `sourceDirectory`, `configuredRevision`, `candidatesJson`, generated `candidatePoms`, static `bomPom`/`allPom`, `repositoryUrl`, `portalUrl`, `userName`, `password`, `portalFixture`; writes `manifestFile` and optional `reportFile`. Never invokes Maven or resolves Maven candidates. | +| `releaseAllSigningCheck` | `manifestFile`, `configuredSigningJson` mapping project paths to signatory availability, `strict`; reports unavailable Gradle signing or fails when strict. No signing operation. | +| `releaseAllGuard` | `sourceDirectory`, `manifestFile`, `versionsJson`; checks revision, clean tree, source hashes and effective publication versions before staging tasks run. | +| `releaseAllPrepareMaven` | `sourceDirectory`, `manifestFile`, `stagingDirectory`, separate `localRepository`, `mavenWorkDirectory`, `verifierFile`, `settingsHelperFile`, optional `userSettingsFile` (default `~/.m2/settings.xml`); runs the caller's verifier and Maven `install -Prelease`, never deploy. The settings helper takes source user-settings, staged repository, output-settings and repository ID; it must preserve signing settings without logging secrets. | +| `bundleAll` | `sourceDirectory`, `manifestFile`, `stagingDirectory`; writes `bundleFile` and `receiptFile`. Rechecks source, dependency POMs, metadata, artifact policy, signatures and staged checksums. Generates MD5/SHA-1 for artifacts/signatures. | +| `releaseAll` | `sourceDirectory`, `manifestFile`, `bundleFile`, `receiptFile`, `recordFile`, `portalFixture`, repository/Portal URLs and credentials; `resumeId` defaults to the caller-supplied empty string for a new upload, or a saved deployment ID for status-only recovery. Rechecks availability and uploads once. | + +The Matrix build configures these properties in `gradle/release-all.gradle`. For example, +a caller configures the bundle task after registering its own staging/preparation workflow: + +```groovy +plugins { id 'se.alipsa.nexus-release-all' } + +bundleAll { + sourceDirectory = layout.projectDirectory + manifestFile = layout.buildDirectory.file('releaseAll/manifest.json') + stagingDirectory = layout.buildDirectory.dir('releaseAll/staging') + bundleFile = layout.buildDirectory.file('releaseAll/bundle.zip') + receiptFile = layout.buildDirectory.file('releaseAll/bundle-receipt.json') +} +``` + +Run discovery in a separate invocation before configuring staging dependencies from the +saved manifest. Run source guards before staging, the caller's scoped verification before +preparation, then `bundleAll`, then `releaseAll`. Do not upload if any preceding phase fails. +The combined tasks support strict configuration-cache reuse; legacy verification tasks can +run with `--no-configuration-cache` independently. + +## Manifest and publication policy + +Publication descriptors preserve exact configured coordinates, expected filenames, source +project/publication or Maven POM, dependency origins, and published dependency/management +expectations. Include production compile and runtime project edges even if omitted by POMs. +Only exact non-SNAPSHOT versions whose dependencies are released or in the final bundle are +selected. Exclusions cascade; a selected module's mismatched BOM pin blocks the new BOM. +An unselected module can retain an already-released BOM pin with a warning. + +Normal JAR components require main, sources and Javadoc JARs plus POM. POM components require +only a POM. An explicitly configured `sourcesAndJavadocRequired: false` policy preserves an +aggregate JAR's existing main-JAR/POM publication set. All expected files need nonempty +signatures. No Gradle metadata, repository bookkeeping or unrelated components are copied. +The ZIP size limit defaults to 1,000,000,000 bytes. + +Publication checks use exact repository POMs and authenticated Portal +`GET /publisher/published?namespace=groupId&name=artifactId&version=version` under the configured +Portal API base. Errors never mean absence. Confirmed, sanitized namespace/absence fixtures +are required for production absence-based discovery and new uploads; mock fixtures are only +used in tests. Availability checks retry transient failures at most three times by default. + +## Interrupted deployments + +The record is written as `SUBMITTING` before HTTP and stores the ID immediately on success. +Never automatically retry an unknown upload outcome. Resume only the saved ID, without +source checks, builds or signing. Polling defaults to 60 attempts at 10-second intervals; +Central propagation remains separate from Portal publication. A failed record reports all +component coordinates. Archive an `AVAILABLE` record before a new deployment; manually +reconcile every unresolved record rather than deleting it to retry. diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/BundleAllTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/BundleAllTask.groovy new file mode 100644 index 0000000..6245f8f --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/BundleAllTask.groovy @@ -0,0 +1,34 @@ +package se.alipsa.gradle.plugin.release + +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.tasks.* +import org.gradle.work.DisableCachingByDefault + +/** Configuration-cache compatible aggregation without implicit build or upload dependencies. */ +@DisableCachingByDefault(because = 'Release requires source guards') +abstract class BundleAllTask extends DefaultTask { + @InputFile @PathSensitive(PathSensitivity.NONE) + abstract RegularFileProperty getManifestFile() + @InputDirectory @PathSensitive(PathSensitivity.RELATIVE) + abstract DirectoryProperty getStagingDirectory() + @Internal + abstract DirectoryProperty getSourceDirectory() + @OutputFile + abstract RegularFileProperty getBundleFile() + + @OutputFile + abstract RegularFileProperty getReceiptFile() + + BundleAllTask() { outputs.upToDateWhen { false } } + + /** Validates the source and all manifest artifacts and creates the combined ZIP. */ + @TaskAction + void bundle() { + Map manifest = ReleaseManifest.read(manifestFile.get().asFile) + ReleaseManifest.guard(sourceDirectory.get().asFile, manifest) + CombinedBundle.create(stagingDirectory.get().asFile, bundleFile.get().asFile, manifest.selected as List) + ReleaseManifest.write(receiptFile.get().asFile, [bundleHash: ReleaseChecksums.checksum(bundleFile.get().asFile, 'SHA-256'), manifestHash: ReleaseChecksums.checksum(manifestFile.get().asFile, 'SHA-256')]) + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/BundleTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/BundleTask.groovy index 1c135d3..9df4d7c 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/BundleTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/BundleTask.groovy @@ -16,8 +16,6 @@ import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction import org.gradle.work.DisableCachingByDefault -import java.security.DigestOutputStream -import java.security.MessageDigest import java.nio.charset.StandardCharsets import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream @@ -170,12 +168,7 @@ abstract class BundleTask extends DefaultTask { } private static String checksumFor(File file, String algo) { - def digest = MessageDigest.getInstance(algo) - file.withInputStream { is -> - new DigestOutputStream(OutputStream.nullOutputStream(), digest) - .withCloseable { dos -> is.transferTo(dos) } - } - digest.digest().encodeHex().toString() + ReleaseChecksums.checksum(file, algo) } private static String checksumExtension(String algo) { diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/BundleValidator.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/BundleValidator.groovy index 8dcac49..2af62f5 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/BundleValidator.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/BundleValidator.groovy @@ -89,6 +89,11 @@ class BundleValidator { } String pomText = zip.getInputStream(pomEntry).getText('UTF-8') + validatePomMetadata(pomText, expectedPomPath, errors) + } + + /** Checks Central metadata for both single-publication and combined archives. */ + static void validatePomMetadata(String pomText, String expectedPomPath, List errors) { GPathResult pom try { pom = new XmlSlurper(false, false).parseText(pomText) diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy new file mode 100644 index 0000000..57fbbe1 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy @@ -0,0 +1,66 @@ +package se.alipsa.gradle.plugin.release + +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** Builds one validated allow-listed Maven-layout archive. */ +class CombinedBundle { + static final long MAX_BYTES = 1_000_000_000L + + /** Validates all components before writing any ZIP entry. */ + static void create(File staging, File target, List publications, long limit = MAX_BYTES) { + if (publications.empty) throw new IllegalArgumentException('Cannot bundle an empty selection') + Map entries = [:] + publications.each { Map publication -> + def coordinate = new ReleaseCoordinate(publication.groupId, publication.artifactId, publication.version) + coordinate.validate() + String prefix = "${coordinate.artifactId}-${coordinate.version}" + List required = ["${prefix}.pom".toString()] + if (publication.packaging != 'pom') { + required.add("${prefix}.jar".toString()) + if (publication.sourcesAndJavadocRequired != false) required.addAll(["${prefix}-sources.jar".toString(), "${prefix}-javadoc.jar".toString()]) + } + if (!(publication.files as Set).containsAll(required)) throw new IllegalArgumentException('Manifest omits required publication artifacts') + publication.files.each { String name -> + if (!(name ==~ /[A-Za-z0-9_.+-]+/) || !name.startsWith(prefix) || !(name.endsWith('.jar') || name.endsWith('.pom'))) { + throw new IllegalArgumentException('Unsafe or unsupported artifact filename') + } + String path = "${coordinate.path()}/${name}" + [path, path + '.asc'].each { String entry -> + File source = new File(staging, entry) + if (!source.isFile() || source.length() == 0 || java.nio.file.Files.isSymbolicLink(source.toPath()) || !source.canonicalPath.startsWith(staging.canonicalPath + File.separator)) { + throw new IllegalArgumentException("Missing or unsafe release artifact: ${entry}") + } + if (entries.put(entry, source) != null) throw new IllegalArgumentException("Duplicate release path: ${entry}") + ['MD5', 'SHA-1'].each { String algorithm -> + File checksum = new File(source.path + '.' + algorithm.toLowerCase().replace('-', '')) + String hash = ReleaseChecksums.checksum(source, algorithm) + if (checksum.exists() && checksum.text.trim() != hash) throw new IllegalArgumentException("Incorrect checksum for ${entry}") + } + } + } + File pomFile = new File(staging, "${coordinate.path()}/${prefix}.pom") + ReleasePom.compare(pomFile, publication) + List metadataErrors = [] + BundleValidator.validatePomMetadata(pomFile.getText('UTF-8'), coordinate.key(), metadataErrors) + if (!metadataErrors.empty) throw new IllegalArgumentException(metadataErrors.join('; ')) + } + target.parentFile.mkdirs() + File temporary = new File(target.path + '.tmp') + try { + temporary.withOutputStream { output -> + new ZipOutputStream(output).withCloseable { zip -> + entries.sort().each { String path, File source -> + zip.putNextEntry(new ZipEntry(path)); source.withInputStream { it.transferTo(zip) }; zip.closeEntry() + ['MD5', 'SHA-1'].each { String algorithm -> + zip.putNextEntry(new ZipEntry(path + '.' + algorithm.toLowerCase().replace('-', ''))) + zip.write(ReleaseChecksums.checksum(source, algorithm).getBytes('UTF-8')); zip.closeEntry() + } + } + } + } + if (temporary.length() > limit) throw new IllegalArgumentException('ZIP exceeds bundle limit') + java.nio.file.Files.move(temporary.toPath(), target.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) + } finally { temporary.delete() } + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy new file mode 100644 index 0000000..85e9bd7 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy @@ -0,0 +1,52 @@ +package se.alipsa.gradle.plugin.release + +/** One-attempt upload journaling and status-only recovery. */ +class CombinedDeployment { + /** Uploads once, persisting intent before HTTP and ID immediately after it. */ + static Map submit(File record, File bundle, Map manifest, PublicationStatus availability, CentralPortalClient client) { + if (record.exists()) throw new IllegalStateException('Deployment record exists; resume or reconcile it before a new upload') + manifest.selected.each { Map component -> + if (availability.published(new ReleaseCoordinate(component.groupId, component.artifactId, component.version), true)) { + throw new IllegalStateException("Component was published since planning: ${component.artifactId}") + } + } + Map state = [state: 'SUBMITTING', bundleHash: ReleaseChecksums.checksum(bundle, 'SHA-256'), + manifest: manifest, created: java.time.Instant.now().toString()] + // Reserve the active journal atomically: two local invocations must not both submit. + record.parentFile.mkdirs() + try { + java.nio.file.Files.writeString(record.toPath(), groovy.json.JsonOutput.prettyPrint(groovy.json.JsonOutput.toJson(state)), java.nio.file.StandardOpenOption.CREATE_NEW) + } catch (java.nio.file.FileAlreadyExistsException ignored) { + throw new IllegalStateException('Deployment record was created concurrently; resume or reconcile it before a new upload') + } + // No retry: even an exception may mean the server accepted the upload. + String id = client.upload(bundle) + if (!id || !(id ==~ /[A-Za-z0-9-]+/)) throw new IllegalStateException('Upload outcome unknown; reconcile SUBMITTING record in Portal') + state.deploymentId = id + state.state = 'UPLOADED' + ReleaseManifest.write(record, state) + state + } + + /** Polls recorded ID without source guards, rebuilding, or another submission. */ + static void resume(File record, String id, CentralPortalClient client, Closure available, int attempts = 60, long interval = 10000) { + Map state = ReleaseManifest.read(record) + if (!id || state.deploymentId != id) throw new IllegalStateException('Deployment ID does not match the saved record') + for (int attempt = 0; attempt < attempts; attempt++) { + Map status = client.getDeploymentStatus(id) + state.state = status.deploymentState + ReleaseManifest.write(record, state) + if (state.state == 'FAILED') { + String coordinates = state.manifest.selected.collect { "${it.groupId}:${it.artifactId}:${it.version}" }.join(', ') + throw new IllegalStateException("Deployment ${id} failed for ${coordinates}; inspect Portal for component validation errors") + } + if (state.state == 'PUBLISHED' && available(state.manifest as Map)) { + state.state = 'AVAILABLE' + ReleaseManifest.write(record, state) + return + } + if (attempt + 1 < attempts) Thread.sleep(interval) + } + throw new IllegalStateException("Deployment ${id} is pending publication/propagation; resume without uploading") + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/PrepareMavenTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/PrepareMavenTask.groovy new file mode 100644 index 0000000..1a2948f --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/PrepareMavenTask.groovy @@ -0,0 +1,62 @@ +package se.alipsa.gradle.plugin.release + +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.tasks.* +import org.gradle.work.DisableCachingByDefault + +/** Builds Maven publications locally, preserving user signing settings and isolating downloads. */ +@DisableCachingByDefault(because = 'Signs local release artifacts') +abstract class PrepareMavenTask extends DefaultTask { + @Internal abstract DirectoryProperty getSourceDirectory() + @InputFile @PathSensitive(PathSensitivity.NONE) abstract RegularFileProperty getManifestFile() + @Internal abstract DirectoryProperty getStagingDirectory() + @Internal abstract DirectoryProperty getLocalRepository() + @Internal abstract DirectoryProperty getMavenWorkDirectory() + @InputFile @PathSensitive(PathSensitivity.RELATIVE) abstract RegularFileProperty getVerifierFile() + @InputFile @PathSensitive(PathSensitivity.RELATIVE) abstract RegularFileProperty getSettingsHelperFile() + @Internal abstract RegularFileProperty getUserSettingsFile() + + /** Runs only install, never deploy; validates flattened dependencies before staging. */ + @TaskAction + void prepare() { + File root = sourceDirectory.get().asFile + Map manifest = ReleaseManifest.read(manifestFile.get().asFile) + ReleaseManifest.guard(root, manifest) + File stage = stagingDirectory.get().asFile + File cache = localRepository.get().asFile + cache.parentFile.mkdirs() + File settingsFile = java.nio.file.Files.createTempFile(cache.parentFile.toPath(), 'signing-settings-', '.xml', + java.nio.file.attribute.PosixFilePermissions.asFileAttribute(java.nio.file.attribute.PosixFilePermissions.fromString('rw-------'))).toFile() + File original = userSettingsFile.present ? userSettingsFile.get().asFile : new File(System.getProperty('user.home'), '.m2/settings.xml') + try { + PrepareMavenTask.run(root, ['groovy', settingsHelperFile.get().asFile.path, original.path, stage.path, settingsFile.path, 'matrix-release-staging'], logger) + boolean mavenSelected = manifest.selected.any { !it.projectPath } + if (!mavenSelected) return + // Run existing verifier against staged artifacts, without publishing or cleaning staging. + PrepareMavenTask.run(root, ['bash', verifierFile.get().asFile.path, '--manifest', manifestFile.get().asFile.path, '--staging', stage.path, '--local', cache.path], logger) + manifest.selected.findAll { !it.projectPath }.sort { it.role == 'bom' ? 0 : 1 }.each { Map component -> + PrepareMavenTask.run(mavenWorkDirectory.get().asFile, ['mvn', '-s', settingsFile.path, "-Dmaven.repo.local=${cache}", '-Prelease', '-f', new File(root, component.pomFile).path, 'install'], logger) + def coordinate = new ReleaseCoordinate(component.groupId, component.artifactId, component.version) + File source = new File(cache, coordinate.path()) + File destination = new File(stage, coordinate.path()) + destination.mkdirs() + component.files.each { String name -> + [name, name + '.asc'].each { String fileName -> + File artifact = new File(source, fileName) + if (!artifact.isFile()) throw new IllegalStateException("Maven release output missing: ${fileName}") + java.nio.file.Files.copy(artifact.toPath(), new File(destination, fileName).toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) + } + } + ReleasePom.compare(new File(destination, "${component.artifactId}-${component.version}.pom"), component) + } + } finally { settingsFile.delete() } + } + + private static void run(File directory, List command, org.gradle.api.logging.Logger taskLogger) { + Process process = new ProcessBuilder(command.collect { it.toString() }).directory(directory).redirectErrorStream(true).start() + process.inputStream.withReader('UTF-8') { reader -> reader.eachLine { line -> taskLogger.lifecycle(line) } } + if (process.waitFor() != 0) throw new IllegalStateException('Maven preparation/API verification failed; no upload attempted. If a Maven mirror intercepts the staging file remote, exclude matrix-release-staging from mirrorOf.') + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationDescriptor.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationDescriptor.groovy new file mode 100644 index 0000000..517eec9 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationDescriptor.groovy @@ -0,0 +1,24 @@ +package se.alipsa.gradle.plugin.release + +/** Serializable manifest publication and its eligibility edges. */ +class PublicationDescriptor implements Serializable { + String projectPath + String publication + String pomFile + String packaging = 'jar' + String role = 'module' + boolean sourcesAndJavadocRequired = true + ReleaseCoordinate coordinate + List> dependencies = [] + List> publishedDependencies = [] + List> publishedManagement = [] + List files = [] + List reasons = [] + + /** Serializes this publication without credentials. */ + Map toMap() { + [projectPath: projectPath, publication: publication, pomFile: pomFile, packaging: packaging, role: role, sourcesAndJavadocRequired: sourcesAndJavadocRequired, + groupId: coordinate.groupId, artifactId: coordinate.artifactId, version: coordinate.version, + dependencies: dependencies, publishedDependencies: publishedDependencies, publishedManagement: publishedManagement, files: files, reasons: reasons] + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationStatus.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationStatus.groovy new file mode 100644 index 0000000..3b71d25 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationStatus.groovy @@ -0,0 +1,65 @@ +package se.alipsa.gradle.plugin.release + +import groovy.json.JsonSlurper +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.time.Duration + +/** Fail-closed exact-version availability checks, with bounded transient retries. */ +class PublicationStatus { + String repositoryUrl = 'https://repo.maven.apache.org/maven2' + String portalUrl = CentralPortalClient.CENTRAL_PORTAL_URL + String userName + String password + int attempts = 3 + boolean requireFixture = false + File fixtureFile + HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(15)).build() + + /** Returns true only for a successful repository response or explicit Portal publication. */ + boolean published(ReleaseCoordinate coordinate, boolean checkBoth = false) { + coordinate.validate() + int status = request("${repositoryUrl}/${coordinate.path()}/${coordinate.artifactId}-${coordinate.version}.pom", false).statusCode() + if (status != 200 && status != 404) throw new IOException("Repository availability returned HTTP ${status} for ${coordinate.key()}") + if (status == 200 && !checkBoth) return true + boolean portal = portalPublished(coordinate) + status == 200 || portal + } + + /** Implements checkComponentPublished; errors, including 403, never mean absence. */ + boolean portalPublished(ReleaseCoordinate coordinate) { + if (requireFixture) { + if (!fixtureFile?.isFile()) throw new IOException('Capture confirmed Portal fixtures before absence-based discovery/upload') + Map fixture = ReleaseManifest.read(fixtureFile) + if (fixture.namespacePolicy != 'groupId' || fixture.published?.body?.published != true || fixture.absent?.body?.published != false) { + throw new IOException('Portal namespace/absence fixtures are not confirmed') + } + } + if (!userName || !password) throw new IOException('Portal checks require sonatypeUsername and sonatypePassword') + String query = [namespace: coordinate.groupId, name: coordinate.artifactId, version: coordinate.version] + .collect { String key, String value -> "${key}=${URLEncoder.encode(value, 'UTF-8')}" }.join('&') + def response = request("${portalUrl}/publisher/published?${query}", true) + if (response.statusCode() != 200) throw new IOException("Portal published-status returned HTTP ${response.statusCode()} for ${coordinate.key()}") + def body + try { body = new JsonSlurper().parseText(response.body()) } + catch (Exception ignored) { throw new IOException('Portal published-status returned invalid JSON') } + if (!(body instanceof Map) || !(body.published instanceof Boolean)) throw new IOException('Portal published-status requires a boolean published field') + body.published + } + + private HttpResponse request(String url, boolean authenticated) { + for (int attempt = 1; attempt <= attempts; attempt++) { + def builder = HttpRequest.newBuilder(URI.create(url)).timeout(Duration.ofSeconds(30)).GET() + if (authenticated) builder.header('Authorization', 'Bearer ' + Base64.encoder.encodeToString("${userName}:${password}".getBytes('UTF-8'))) + try { + def response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString()) + if (!(response.statusCode() in [429, 500, 502, 503, 504]) || attempt == attempts) return response + } catch (IOException ignored) { + if (attempt == attempts) throw new IOException('Availability request failed after bounded retries') + } + Thread.sleep(250L * attempt) + } + throw new IOException('Availability request failed') + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy new file mode 100644 index 0000000..9949de6 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy @@ -0,0 +1,101 @@ +package se.alipsa.gradle.plugin.release + +import groovy.json.JsonSlurper +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.* +import org.gradle.work.DisableCachingByDefault + +/** Plans configured publications without compiling or resolving Maven candidates. */ +@DisableCachingByDefault(because = 'Queries current publication availability') +abstract class ReleaseAllPlanTask extends DefaultTask { + @Input abstract Property getCandidatesJson() + @Input abstract Property getConfiguredRevision() + @Internal abstract DirectoryProperty getSourceDirectory() + @InputFile @PathSensitive(PathSensitivity.RELATIVE) abstract RegularFileProperty getBomPom() + @InputFile @PathSensitive(PathSensitivity.RELATIVE) abstract RegularFileProperty getAllPom() + @Input abstract Property getRepositoryUrl() + @Input abstract Property getPortalUrl() + @Internal abstract Property getUserName() + @Internal abstract Property getPassword() + @Internal abstract RegularFileProperty getPortalFixture() + @InputFiles @PathSensitive(PathSensitivity.RELATIVE) abstract org.gradle.api.file.ConfigurableFileCollection getCandidatePoms() + @OutputFile abstract RegularFileProperty getManifestFile() + @OutputFile @Optional abstract RegularFileProperty getReportFile() + + ReleaseAllPlanTask() { outputs.upToDateWhen { false } } + + /** Performs fail-closed selection and persists all exclusions and source guards. */ + @TaskAction + void plan() { + File root = sourceDirectory.get().asFile + if (ReleaseManifest.git(root, 'rev-parse', 'HEAD') != configuredRevision.get()) throw new IllegalStateException('HEAD changed since publication configuration; restart planning') + if (ReleaseManifest.git(root, 'status', '--porcelain')) throw new IllegalStateException('Release planning requires a clean Git working tree') + List candidates = [] + new JsonSlurper().parseText(candidatesJson.get()).each { Map registered -> + Map parsed = ReleasePom.read(new File(root, registered.pomFile)) + def publication = new PublicationDescriptor(projectPath: registered.projectPath, publication: registered.publication, + pomFile: registered.pomFile, coordinate: new ReleaseCoordinate(registered.groupId, registered.artifactId, registered.version), + dependencies: registered.dependencies + (parsed.dependencies + parsed.management).findAll { it.version }, + publishedDependencies: parsed.dependencies, publishedManagement: parsed.management, files: registered.files) + candidates.add(publication) + } + Map bom = ReleasePom.read(bomPom.get().asFile) + Map managed = bom.management.collectEntries { ["${it.groupId}:${it.artifactId}".toString(), it.version] } + Map all = ReleasePom.read(allPom.get().asFile, bom.properties, managed) + [bom, all].each { Map parsed -> + String prefix = "${parsed.artifactId}-${parsed.version}" + File inputPom = parsed.is(bom) ? bomPom.get().asFile : allPom.get().asFile + if (!inputPom.canonicalPath.startsWith(root.canonicalPath + File.separator)) throw new IllegalArgumentException('Maven candidate POM must be inside the source repository') + String pomPath = root.toPath().toAbsolutePath().normalize().relativize(inputPom.toPath().toAbsolutePath().normalize()).toString() + def publication = new PublicationDescriptor(pomFile: pomPath, + coordinate: new ReleaseCoordinate(parsed.groupId, parsed.artifactId, parsed.version), packaging: parsed.packaging, + role: parsed.is(bom) ? 'bom' : 'aggregate', sourcesAndJavadocRequired: false, + dependencies: (parsed.dependencies + parsed.management).findAll { it.version }, + publishedDependencies: parsed.dependencies.findAll { it.scope != 'test' }, publishedManagement: parsed.is(bom) ? parsed.management : [], files: ["${prefix}.pom".toString()]) + if (parsed.packaging == 'jar') publication.files.add("${prefix}.jar".toString()) + candidates.add(publication) + } + def status = new PublicationStatus(repositoryUrl: repositoryUrl.get(), portalUrl: portalUrl.get(), userName: userName.getOrElse(''), password: password.getOrElse(''), requireFixture: true, fixtureFile: portalFixture.get().asFile) + List warnings = [] + List selected = ReleasePlanner.select(candidates, { ReleaseCoordinate c -> status.published(c) }, warnings) + Set scope = selected.findAll { it.projectPath }.collect { it.projectPath } as Set + boolean changed = true + while (changed) { + int previous = scope.size() + candidates.findAll { it.projectPath }.each { PublicationDescriptor p -> + if (p.dependencies.any { Map dep -> candidates.any { it.projectPath in scope && it.coordinate.groupId == dep.groupId && it.coordinate.artifactId == dep.artifactId } }) scope.add(p.projectPath) + } + changed = scope.size() != previous + } + Map hashes = [:] + ReleaseManifest.git(root, 'ls-files').readLines().each { String path -> + File input = new File(root, path) + if (input.isFile()) hashes[path] = ReleaseChecksums.checksum(input, 'SHA-256') + } + Map manifest = [schemaVersion: 1, revision: configuredRevision.get(), hashes: hashes, + versions: candidates.findAll { it.projectPath }.collectEntries { [it.projectPath, it.coordinate.version] }, + selected: selected.collect { it.toMap() }, excluded: candidates.findAll { !it.reasons.empty }.collect { it.toMap() }, + verificationScope: scope.sort(), warnings: warnings, created: java.time.Instant.now().toString()] + ReleaseManifest.guard(root, manifest) + ReleaseManifest.write(manifestFile.get().asFile, manifest) + List report = ["Release plan at ${manifest.revision}"] + (manifest.selected + manifest.excluded).each { Map component -> + report.add("${component.reasons.empty ? 'SELECT' : 'SKIP'} ${component.groupId}:${component.artifactId}:${component.version}".toString()) + component.reasons.each { report.add(" ${it}".toString()) } + component.dependencies.each { Map edge -> + report.add(" depends ${edge.groupId}:${edge.artifactId}:${edge.version} (${edge.origin})".toString()) + } + } + warnings.each { report.add("WARNING ${it}".toString()) } + report.add("Verification scope: ${scope.sort().join(', ')}".toString()) + String readable = report.join('\n') + '\n' + if (reportFile.present) { + reportFile.get().asFile.parentFile.mkdirs() + reportFile.get().asFile.setText(readable, 'UTF-8') + } + logger.lifecycle(readable) + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllSupportPlugin.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllSupportPlugin.groovy new file mode 100644 index 0000000..b900449 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllSupportPlugin.groovy @@ -0,0 +1,17 @@ +package se.alipsa.gradle.plugin.release + +import org.gradle.api.Plugin +import org.gradle.api.Project + +/** Exposes combined-release support independently of per-publication task registration. */ +class ReleaseAllSupportPlugin implements Plugin { + /** Combined workflows configure typed tasks without creating single-publication tasks. */ + void apply(Project project) { + project.tasks.register('releaseAllSigningCheck', SigningPreflightTask) + project.tasks.register('releaseAllGuard', ReleaseSourceGuardTask) + project.tasks.register('bundleAll', BundleAllTask) + project.tasks.register('releaseAll', ReleaseAllTask) + project.tasks.register('releaseAllPlan', ReleaseAllPlanTask) + project.tasks.register('releaseAllPrepareMaven', PrepareMavenTask) + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy new file mode 100644 index 0000000..412bf12 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy @@ -0,0 +1,53 @@ +package se.alipsa.gradle.plugin.release + +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.* +import org.gradle.work.DisableCachingByDefault + +/** Uploads a prepared combined bundle or resumes a persisted deployment. */ +@DisableCachingByDefault(because = 'External deployment state') +abstract class ReleaseAllTask extends DefaultTask { + @Internal abstract DirectoryProperty getSourceDirectory() + @Internal abstract RegularFileProperty getManifestFile() + @Internal abstract RegularFileProperty getBundleFile() + @Internal abstract RegularFileProperty getRecordFile() + @Internal abstract RegularFileProperty getReceiptFile() + @Internal abstract RegularFileProperty getPortalFixture() + @Internal abstract Property getUserName() + @Internal abstract Property getPassword() + @Input abstract Property getPortalUrl() + @Input abstract Property getRepositoryUrl() + @Input abstract Property getResumeId() + + /** Checks source and availability before submitting, or resumes without building. */ + @TaskAction + void release() { + def client = new CentralPortalClient(logger, portalUrl.get(), userName.getOrElse(''), password.getOrElse('')) + String id = resumeId.getOrElse('') + File record = recordFile.get().asFile + if (!id) { + Map manifest = ReleaseManifest.read(manifestFile.get().asFile) + ReleaseManifest.guard(sourceDirectory.get().asFile, manifest) + Map receipt = ReleaseManifest.read(receiptFile.get().asFile) + if (receipt.bundleHash != ReleaseChecksums.checksum(bundleFile.get().asFile, 'SHA-256') || receipt.manifestHash != ReleaseChecksums.checksum(manifestFile.get().asFile, 'SHA-256')) { + throw new IllegalStateException('Bundle or manifest changed since validation; rebuild the bundle') + } + def status = new PublicationStatus(repositoryUrl: repositoryUrl.get(), portalUrl: portalUrl.get(), userName: userName.getOrElse(''), password: password.getOrElse(''), requireFixture: true, fixtureFile: portalFixture.get().asFile) + id = CombinedDeployment.submit(record, bundleFile.get().asFile, manifest, status, client).deploymentId + } + CombinedDeployment.resume(record, id, client, { Map manifest -> + manifest.selected.every { Map p -> + def coordinate = new ReleaseCoordinate(p.groupId, p.artifactId, p.version) + p.files.findAll { it.endsWith('.pom') || it == "${p.artifactId}-${p.version}.jar" }.every { String name -> + def connection = URI.create("${repositoryUrl.get()}/${coordinate.path()}/${name}").toURL().openConnection() + connection.connectTimeout = 15000; connection.readTimeout = 15000 + try { connection.responseCode == 200 } finally { connection.disconnect() } + } + } + }) + logger.lifecycle("Deployment ${id} is published and available") + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseChecksums.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseChecksums.groovy new file mode 100644 index 0000000..b1e5728 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseChecksums.groovy @@ -0,0 +1,17 @@ +package se.alipsa.gradle.plugin.release + +import java.security.MessageDigest + +/** Shared streaming checksums for single and combined release archives. */ +class ReleaseChecksums { + /** Computes a Maven checksum without loading the artifact into memory. */ + static String checksum(File file, String algorithm) { + MessageDigest digest = MessageDigest.getInstance(algorithm) + file.withInputStream { input -> + byte[] buffer = new byte[65536] + int count + while ((count = input.read(buffer)) != -1) digest.update(buffer, 0, count) + } + digest.digest().encodeHex().toString() + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseCoordinate.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseCoordinate.groovy new file mode 100644 index 0000000..485198f --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseCoordinate.groovy @@ -0,0 +1,29 @@ +package se.alipsa.gradle.plugin.release + +import groovy.transform.Canonical + +/** An exact publication coordinate; never substitutes a previously released version. */ +@Canonical +class ReleaseCoordinate implements Serializable { + String groupId + String artifactId + String version + + /** Returns the Maven repository directory for this version. */ + String path() { "${groupId.replace('.', '/')}/${artifactId}/${version}" } + + /** Returns an unambiguous coordinate key. */ + String key() { "${groupId}:${artifactId}:${version}" } + + /** Rejects unresolved properties, ranges, and unsafe archive paths. */ + void validate() { + if (version?.endsWith('+') || version in ['latest.release', 'latest.integration']) { + throw new IllegalArgumentException('Dynamic versions cannot be released') + } + [groupId, artifactId, version].each { String value -> + if (!value || !(value ==~ /[A-Za-z0-9_][A-Za-z0-9_.+-]*/) || value.contains('..')) { + throw new IllegalArgumentException('Release coordinates must contain exact, safe versions and names') + } + } + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy new file mode 100644 index 0000000..7d14819 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy @@ -0,0 +1,44 @@ +package se.alipsa.gradle.plugin.release + +import groovy.json.JsonOutput +import groovy.json.JsonSlurper + +/** Source guards and atomic, credential-free release records. */ +class ReleaseManifest { + /** Runs git without shell interpretation. */ + static String git(File root, String... arguments) { + Process process = new ProcessBuilder(['git', '-C', root.absolutePath] + arguments.toList()).redirectErrorStream(true).start() + String output = process.inputStream.getText('UTF-8').trim() + if (process.waitFor() != 0) throw new IllegalStateException('Git source validation failed') + output + } + + /** Checks revision, cleanliness, input hashes and effective publication versions. */ + static void guard(File root, Map manifest, Map versions = [:]) { + (manifest.selected ?: []).each { Map component -> + new ReleaseCoordinate(component.groupId, component.artifactId, component.version).validate() + if (component.version.endsWith('-SNAPSHOT')) throw new IllegalStateException('Manifest contains a SNAPSHOT publication') + } + if (git(root, 'rev-parse', 'HEAD') != manifest.revision || git(root, 'status', '--porcelain')) { + throw new IllegalStateException('Stale manifest: HEAD changed or working tree is dirty') + } + manifest.hashes.each { String path, String hash -> + File source = new File(root, path) + if (!source.isFile() || ReleaseChecksums.checksum(source, 'SHA-256') != hash) throw new IllegalStateException("Stale manifest input: ${path}") + } + versions.each { String path, String version -> + if (manifest.versions[path] != version) throw new IllegalStateException("Stale publication version: ${path}") + } + } + + /** Reads the persisted JSON manifest. */ + static Map read(File file) { new JsonSlurper().parse(file) as Map } + + /** Atomically replaces a JSON file in its output directory. */ + static void write(File file, Map value) { + file.parentFile.mkdirs() + File temporary = new File(file.path + '.tmp') + temporary.setText(JsonOutput.prettyPrint(JsonOutput.toJson(value)), 'UTF-8') + java.nio.file.Files.move(temporary.toPath(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy new file mode 100644 index 0000000..7c1b395 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy @@ -0,0 +1,73 @@ +package se.alipsa.gradle.plugin.release + +/** Fixed-point eligibility over published and compile-time dependencies. */ +class ReleasePlanner { + /** Selects exact unreleased coordinates, retaining direct and cascading exclusions. */ + static List select(List candidates, Closure released, + List warnings = []) { + Map availability = [:] + Closure lookup = { ReleaseCoordinate c -> + availability.computeIfAbsent(c.key()) { released(c) } + } + candidates.each { PublicationDescriptor p -> + p.coordinate.validate() + if (p.coordinate.version.endsWith('-SNAPSHOT')) p.reasons.add('SNAPSHOT version') + else if (lookup(p.coordinate)) p.reasons.add('Already released') + } + boolean changed = true + while (changed) { + changed = false + def selected = candidates.findAll { it.reasons.empty } + Set keys = selected.collect { it.coordinate.key() } as Set + selected.each { PublicationDescriptor p -> + p.dependencies.each { Map edge -> + def dependency = new ReleaseCoordinate(edge.groupId, edge.artifactId, edge.version) + dependency.validate() + String reason + if (dependency.version.endsWith('-SNAPSHOT')) reason = 'SNAPSHOT dependency' + else if (!keys.contains(dependency.key()) && !lookup(dependency)) reason = 'Unavailable dependency' + if (reason) { + p.reasons.add("${reason}: ${dependency.key()} (${edge.origin})") + changed = true + } + + } + } + } + // First stabilize dependency exclusions. A module excluded in this phase must not + // cause a false BOM mismatch against an otherwise valid released pin. + def selectedBeforeDrift = candidates.findAll { it.reasons.empty } + selectedBeforeDrift.findAll { it.role == 'bom' }.each { PublicationDescriptor bom -> + bom.dependencies.each { Map edge -> + def module = selectedBeforeDrift.find { it.coordinate.groupId == edge.groupId && it.coordinate.artifactId == edge.artifactId } + if (module && module.coordinate.version != edge.version) { + bom.reasons.add("Selected module/BOM pin mismatch: ${edge.artifactId}:${edge.version} versus ${module.coordinate.version}") + } + } + } + // Cascade any BOM/all exclusion without reviving a previously blocked component. + changed = true + while (changed) { + changed = false + Set keys = candidates.findAll { it.reasons.empty }.collect { it.coordinate.key() } as Set + candidates.findAll { it.reasons.empty }.each { PublicationDescriptor p -> + p.dependencies.each { Map edge -> + def dependency = new ReleaseCoordinate(edge.groupId, edge.artifactId, edge.version) + if (!keys.contains(dependency.key()) && !lookup(dependency)) { + p.reasons.add("Unavailable dependency after BOM exclusion: ${dependency.key()} (${edge.origin})") + changed = true + } + } + } + } + candidates.findAll { it.role == 'bom' }.each { PublicationDescriptor bom -> + bom.dependencies.each { Map edge -> + def module = candidates.find { it.coordinate.artifactId == edge.artifactId && it.coordinate.groupId == edge.groupId } + if (module && module.coordinate.version != edge.version && !module.reasons.empty) { + warnings.add("BOM pin ${edge.artifactId}:${edge.version} differs from unselected module ${module.coordinate.version}") + } + } + } + candidates.findAll { it.reasons.empty } + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy new file mode 100644 index 0000000..cf73520 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy @@ -0,0 +1,53 @@ +package se.alipsa.gradle.plugin.release + +import groovy.xml.XmlSlurper + +/** Static POM inspection: does not resolve artifacts or run Maven. */ +class ReleasePom { + /** Reads exact dependencies with inherited BOM properties and management. */ + static Map read(File file, Map inherited = [:], Map managed = [:]) { + def pom = new XmlSlurper().parse(file) + Map properties = new LinkedHashMap<>(inherited) + pom.properties.children().each { properties[it.name().toString()] = it.text() } + properties['project.version'] = pom.version.text() + properties['project.groupId'] = pom.groupId.text() + Closure resolve = { String value -> + String result = value + 20.times { + result = result.replaceAll(/\$\{([^}]+)\}/) { full, key -> properties[key] ?: full } + } + if (result.contains('${')) throw new IllegalArgumentException("Unresolved POM expression in ${file.name}: ${value}") + result + } + Closure>> deps = { nodes, String origin -> + nodes.collect { node -> + String groupId = resolve(node.groupId.text()) + String artifactId = resolve(node.artifactId.text()) + String version = resolve(node.version.text() ?: managed["${groupId}:${artifactId}"] ?: '') + if (version || groupId.startsWith('se.alipsa.matrix')) new ReleaseCoordinate(groupId, artifactId, version).validate() + [groupId: groupId, artifactId: artifactId, version: version, + scope: resolve(node.scope.text() ?: 'compile'), type: resolve(node.type.text() ?: 'jar'), + classifier: resolve(node.classifier.text()), origin: origin] + } + } + [groupId: resolve(pom.groupId.text()), artifactId: resolve(pom.artifactId.text()), + version: resolve(pom.version.text()), packaging: resolve(pom.packaging.text() ?: 'jar'), properties: properties, + dependencies: deps(pom.dependencies.dependency, 'pom.dependencies'), + management: deps(pom.dependencyManagement.dependencies.dependency, 'pom.dependencyManagement')] + } + + /** Ensures flattened publication matches the expected dependency set. */ + static void compare(File pom, Map expected) { + Map actual = read(pom) + ['groupId', 'artifactId', 'version', 'packaging'].each { String key -> + if (actual[key] != expected[key]) throw new IllegalArgumentException("Published POM ${key} differs from manifest") + } + def signature = { Map edge -> ['groupId', 'artifactId', 'version', 'scope', 'type', 'classifier'].collect { edge[it] ?: '' }.join(':') } + if (actual.dependencies.collect(signature).sort() != expected.publishedDependencies.collect(signature).sort()) { + throw new IllegalArgumentException('Published dependencies differ from manifest') + } + if (expected.containsKey('publishedManagement') && actual.management.collect(signature).sort() != expected.publishedManagement.collect(signature).sort()) { + throw new IllegalArgumentException('Published dependency management differs from manifest') + } + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseSourceGuardTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseSourceGuardTask.groovy new file mode 100644 index 0000000..5d90481 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseSourceGuardTask.groovy @@ -0,0 +1,25 @@ +package se.alipsa.gradle.plugin.release + +import groovy.json.JsonSlurper +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.* +import org.gradle.work.DisableCachingByDefault + +/** Rechecks source in the execution phase, including when the configuration cache is reused. */ +@DisableCachingByDefault(because = 'Git state must be checked every invocation') +abstract class ReleaseSourceGuardTask extends DefaultTask { + @InputFile @PathSensitive(PathSensitivity.NONE) abstract RegularFileProperty getManifestFile() + @Internal abstract DirectoryProperty getSourceDirectory() + @Input abstract Property getVersionsJson() + + ReleaseSourceGuardTask() { outputs.upToDateWhen { false } } + + /** Rejects stale manifests before any preparation task can run. */ + @TaskAction + void guard() { + ReleaseManifest.guard(sourceDirectory.get().asFile, ReleaseManifest.read(manifestFile.get().asFile), new JsonSlurper().parseText(versionsJson.get()) as Map) + } +} diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/SigningPreflightTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/SigningPreflightTask.groovy new file mode 100644 index 0000000..8b0a666 --- /dev/null +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/SigningPreflightTask.groovy @@ -0,0 +1,29 @@ +package se.alipsa.gradle.plugin.release + +import groovy.json.JsonSlurper +import org.gradle.api.DefaultTask +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.* +import org.gradle.work.DisableCachingByDefault + +/** Checks configured signing before long verification builds, without signing anything. */ +@DisableCachingByDefault(because = 'Preflight reports current signing configuration') +abstract class SigningPreflightTask extends DefaultTask { + @Input abstract Property getConfiguredSigningJson() + @InputFile @PathSensitive(PathSensitivity.NONE) abstract RegularFileProperty getManifestFile() + @Input abstract Property getStrict() + + /** Reports missing setup in dry-run; blocks preparation without signing configuration. */ + @TaskAction + void checkSigning() { + Map configured = new JsonSlurper().parseText(configuredSigningJson.get()) as Map + Map manifest = ReleaseManifest.read(manifestFile.get().asFile) + List missing = manifest.selected.findAll { it.projectPath && configured[it.projectPath] != true }.collect { it.projectPath } + if (!missing.empty) { + String message = 'Signing is not configured for: ' + missing.join(', ') + '. Configure the existing Gradle signing properties before preparation.' + if (strict.get()) throw new IllegalStateException(message) + logger.warn(message) + } + } +} diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy new file mode 100644 index 0000000..2fd44fb --- /dev/null +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -0,0 +1,238 @@ +package se.alipsa.gradle.plugin.release + +import com.sun.net.httpserver.HttpServer +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.util.zip.ZipFile +import static org.junit.jupiter.api.Assertions.* + +class CombinedReleaseTest { + @TempDir File directory + + @Test void compileOnlySnapshotAndCascadingExclusions() { + def core = candidate('core', '3.10.0-SNAPSHOT') + def stats = candidate('stats', '2.5.4') + stats.dependencies = [[groupId: 'g', artifactId: 'core', version: core.coordinate.version, origin: 'compileOnly']] + def plot = candidate('plot', '1.0') + plot.dependencies = [[groupId: 'g', artifactId: 'stats', version: '2.5.4', origin: 'compileClasspath']] + assertTrue(ReleasePlanner.select([core, stats, plot], { false }).empty) + assertTrue(stats.reasons.first().contains('compileOnly')) + assertTrue(plot.reasons.first().contains('Unavailable')) + } + + @Test void releasedOldBomPinDoesNotBlockUnselectedSnapshot() { + def arff = candidate('matrix-arff', '0.3.1-SNAPSHOT') + def bom = candidate('matrix-bom', '2.7.0') + bom.dependencies = [[groupId: 'g', artifactId: 'matrix-arff', version: '0.3.0', origin: 'management']] + List warnings = [] + assertEquals([bom], ReleasePlanner.select([arff, bom], { it.version == '0.3.0' }, warnings)) + assertEquals(1, warnings.size()) + } + + @Test void bomDriftIsEvaluatedAfterDependencyCascades() { + def arff = candidate('matrix-arff', '0.3.2') + arff.dependencies = [[groupId: 'g', artifactId: 'core', version: '1-SNAPSHOT', origin: 'compileOnly']] + def bom = candidate('matrix-bom', '2.7.0') + bom.dependencies = [[groupId: 'g', artifactId: 'matrix-arff', version: '0.3.0', origin: 'management']] + assertEquals([bom], ReleasePlanner.select([bom, arff], { it.version == '0.3.0' })) + } + + @Test void selectedModulePinMismatchBlocksBom() { + def arff = candidate('matrix-arff', '0.3.2') + def bom = candidate('matrix-bom', '2.7.0') + bom.dependencies = [[groupId: 'g', artifactId: 'matrix-arff', version: '0.3.0', origin: 'management']] + assertEquals([arff], ReleasePlanner.select([arff, bom], { it.version == '0.3.0' })) + } + + @Test void portalOverridesRepository404AndErrorsFailClosed() { + HttpServer server = HttpServer.create(new InetSocketAddress('127.0.0.1', 0), 0) + int responseStatus = 200 + String body = '{"published":true}' + List queries = [] + server.createContext('/') { exchange -> + boolean portal = exchange.requestURI.path.contains('/publisher/published') + if (portal) { + queries.add(exchange.requestURI.rawQuery) + assertTrue(exchange.requestHeaders.getFirst('Authorization').startsWith('Bearer ')) + } + byte[] data = (portal ? body : '').getBytes('UTF-8') + exchange.sendResponseHeaders(portal ? responseStatus : 404, data.length) + exchange.responseBody.withCloseable { it.write(data) } + } + server.start() + try { + def status = new PublicationStatus(repositoryUrl: "http://127.0.0.1:${server.address.port}/repo", portalUrl: "http://127.0.0.1:${server.address.port}/api/v1", userName: 'u', password: 'p', attempts: 2) + def coordinate = new ReleaseCoordinate('g', 'a', '1.0+test') + assertTrue(status.published(coordinate)) + assertTrue(queries.last().contains('version=1.0%2Btest')) + body = '{"published":false}' + assertFalse(status.published(coordinate)) + body = '{"published":"false"}' + assertThrows(IOException) { status.published(coordinate) } + [400, 401, 403, 500].each { code -> + responseStatus = code + assertThrows(IOException) { status.published(coordinate) } + } + } finally { server.stop(0) } + } + + @Test void pomAndJarPoliciesAllowlistAndChecksums() { + def pom = publication('matrix-bom', 'pom', ['matrix-bom-1.pom']) + def all = publication('matrix-all', 'jar', ['matrix-all-1.pom', 'matrix-all-1.jar']) + [pom, all].each { p -> + p.files.each { name -> + def file = new File(directory, "g/${p.artifactId}/1/${name}") + file.parentFile.mkdirs() + file.text = name.endsWith('.pom') ? pomText(p.artifactId, p.packaging) : 'jar fixture' + new File(file.path + '.asc').text = 'signature fixture' + } + } + new File(directory, 'download.pom').text = 'unrelated' + File zip = new File(directory, 'bundle.zip') + CombinedBundle.create(directory, zip, [pom, all]) + new ZipFile(zip).withCloseable { archive -> + assertEquals(18, archive.size()) + assertNull(archive.getEntry('download.pom')) + assertEquals(ReleaseChecksums.checksum(new File(directory, 'g/matrix-all/1/matrix-all-1.jar'), 'SHA-1'), archive.getInputStream(archive.getEntry('g/matrix-all/1/matrix-all-1.jar.sha1')).text) + } + assertThrows(IllegalArgumentException) { CombinedBundle.create(directory, zip, [pom, pom]) } + assertThrows(IllegalArgumentException) { CombinedBundle.create(directory, zip, [pom, all], 1) } + new File(directory, 'g/matrix-all/1/matrix-all-1.jar.asc').delete() + assertThrows(IllegalArgumentException) { CombinedBundle.create(directory, zip, [all]) } + } + + @Test void staticMavenPropertiesAndUnresolvedExpressions() { + File pom = new File(directory, 'pom.xml') + pom.text = pomText('a', 'pom').replace('', 'gb${bVersion}') + assertThrows(IllegalArgumentException) { ReleasePom.read(pom) } + assertEquals('2', ReleasePom.read(pom, [bVersion: '2']).dependencies.first().version) + } + + @Test void sourceGuardsRejectRevisionDirtyTreeAndChangedInput() { + File root = new File(directory, 'repo'); root.mkdirs() + new File(root, 'input.txt').text = 'original' + git(root, 'init'); git(root, 'add', '.') + git(root, '-c', 'user.name=Test', '-c', 'user.email=test@example.org', 'commit', '-m', 'Fixture') + Map manifest = [revision: ReleaseManifest.git(root, 'rev-parse', 'HEAD'), hashes: ['input.txt': ReleaseChecksums.checksum(new File(root, 'input.txt'), 'SHA-256')], versions: [':a': '1']] + ReleaseManifest.guard(root, manifest, [':a': '1']) + assertThrows(IllegalStateException) { ReleaseManifest.guard(root, manifest, [':a': '2']) } + new File(root, 'input.txt').text = 'changed' + assertThrows(IllegalStateException) { ReleaseManifest.guard(root, manifest) } + git(root, 'add', '.'); git(root, '-c', 'user.name=Test', '-c', 'user.email=test@example.org', 'commit', '-m', 'Changed') + assertThrows(IllegalStateException) { ReleaseManifest.guard(root, manifest) } + } + + @Test void uploadIsOnceAndUnknownOutcomeCannotBeRetried() { + File bundle = new File(directory, 'bundle.zip'); bundle.text = 'bundle' + File record = new File(directory, 'deployment.json') + Map manifest = [selected: [[groupId: 'g', artifactId: 'a', version: '1']]] + def availability = new PublicationStatus() { + @Override boolean published(ReleaseCoordinate coordinate, boolean checkBoth) { false } + } + int uploads = 0 + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { uploads++; throw new IOException('Disconnected after server accepted upload') } + } + assertThrows(IOException) { CombinedDeployment.submit(record, bundle, manifest, availability, client) } + assertEquals('SUBMITTING', ReleaseManifest.read(record).state) + assertThrows(IllegalStateException) { CombinedDeployment.submit(record, bundle, manifest, availability, client) } + assertEquals(1, uploads) + } + + @Test void resumePollsAndPropagationTimeoutDoesNotUpload() { + File record = new File(directory, 'deployment.json') + ReleaseManifest.write(record, [deploymentId: 'id', state: 'UPLOADED', manifest: [selected: []]]) + int polls = 0 + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override Map getDeploymentStatus(String id) { polls++; [deploymentState: 'PUBLISHED'] } + @Override String upload(File file) { throw new AssertionError('Resume must never upload') } + } + assertThrows(IllegalStateException) { CombinedDeployment.resume(record, 'id', client, { false }, 2, 0) } + assertEquals(2, polls) + CombinedDeployment.resume(record, 'id', client, { true }, 1, 0) + assertEquals('AVAILABLE', ReleaseManifest.read(record).state) + } + + @Test void multiComponentUploadAndLatePublicationCheck() { + File bundle = new File(directory, 'bundle.zip'); bundle.text = 'combined' + File record = new File(directory, 'deployment.json') + Map manifest = [selected: ['a', 'b'].collect { [groupId: 'g', artifactId: it, version: '1'] }] + List checked = [] + boolean latePublication = true + def status = new PublicationStatus() { + @Override boolean published(ReleaseCoordinate coordinate, boolean both) { + assertTrue(both) + checked.add(coordinate.key()) + latePublication && coordinate.artifactId == 'b' + } + } + int uploads = 0 + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { uploads++; 'deployment-id' } + } + assertThrows(IllegalStateException) { CombinedDeployment.submit(record, bundle, manifest, status, client) } + assertEquals(0, uploads) + assertFalse(record.exists()) + latePublication = false + Map result = CombinedDeployment.submit(record, bundle, manifest, status, client) + assertEquals(1, uploads) + assertEquals('deployment-id', result.deploymentId) + assertEquals(['g:a:1', 'g:b:1', 'g:a:1', 'g:b:1'], checked) + assertThrows(IllegalStateException) { CombinedDeployment.submit(record, bundle, manifest, status, client) } + assertEquals(1, uploads) + } + + @Test void failedDeploymentReportsEveryCoordinate() { + File record = new File(directory, 'deployment.json') + ReleaseManifest.write(record, [deploymentId: 'id', manifest: [selected: [[groupId: 'g', artifactId: 'a', version: '1']]]]) + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override Map getDeploymentStatus(String id) { [deploymentState: 'FAILED'] } + } + def failure = assertThrows(IllegalStateException) { CombinedDeployment.resume(record, 'id', client, { true }, 1, 0) } + assertTrue(failure.message.contains('g:a:1')) + assertEquals('FAILED', ReleaseManifest.read(record).state) + } + + @Test void concurrentLocalSubmissionsReserveOneJournal() { + File record = new File(directory, 'deployment.json') + File bundle = new File(directory, 'bundle.zip'); bundle.text = 'bundle' + Map manifest = [selected: [[groupId: 'g', artifactId: 'a', version: '1']]] + def barrier = new java.util.concurrent.CyclicBarrier(2) + def uploads = new java.util.concurrent.atomic.AtomicInteger() + def availability = new PublicationStatus() { + @Override boolean published(ReleaseCoordinate coordinate, boolean both) { + barrier.await(5, java.util.concurrent.TimeUnit.SECONDS) + false + } + } + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { uploads.incrementAndGet(); 'saved-id' } + } + def executor = java.util.concurrent.Executors.newFixedThreadPool(2) + try { + def calls = (1..2).collect { + executor.submit({ -> + try { CombinedDeployment.submit(record, bundle, manifest, availability, client); true } + catch (IllegalStateException ignored) { false } + } as java.util.concurrent.Callable) + } + assertEquals(1, calls.count { it.get(10, java.util.concurrent.TimeUnit.SECONDS) }) + assertEquals(1, uploads.get()) + assertEquals('saved-id', ReleaseManifest.read(record).deploymentId) + } finally { executor.shutdownNow() } + } + + private static void git(File root, String... args) { + ReleaseManifest.git(root, args) + } + + private static PublicationDescriptor candidate(String artifact, String version) { + new PublicationDescriptor(coordinate: new ReleaseCoordinate('g', artifact, version), role: artifact == 'matrix-bom' ? 'bom' : 'module') + } + private static Map publication(String artifact, String packaging, List files) { + [groupId: 'g', artifactId: artifact, version: '1', packaging: packaging, sourcesAndJavadocRequired: artifact != 'matrix-all', files: files, publishedDependencies: []] + } + private static String pomText(String artifact, String packaging) { + """g${artifact}1${packaging}AAhttps://example.orgMIThttps://example.orgAhttps://example.orgscm:git:example""" + } +} diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy new file mode 100644 index 0000000..e68083c --- /dev/null +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy @@ -0,0 +1,114 @@ +package se.alipsa.gradle.plugin.release + +import com.sun.net.httpserver.HttpServer +import org.gradle.testkit.runner.GradleRunner +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import static org.junit.jupiter.api.Assertions.* + +class CombinedTasksTest { + @TempDir File root + + @Test void planningAndBundlingReuseStrictConfigurationCache() { + HttpServer server = HttpServer.create(new InetSocketAddress('127.0.0.1', 0), 0) + server.createContext('/') { exchange -> + boolean portal = exchange.requestURI.path.contains('/publisher/published') + byte[] body = (portal ? '{"published":false}' : '').getBytes('UTF-8') + exchange.sendResponseHeaders(portal ? 200 : 404, body.length) + exchange.responseBody.withCloseable { it.write(body) } + } + server.start() + try { + String base = "http://127.0.0.1:${server.address.port}" + new File(root, 'settings.gradle').text = "rootProject.name='combined-fixture'" + new File(root, '.gitignore').text = 'build/\n.gradle/\n' + new File(root, 'fixture.json').text = '{"namespacePolicy":"groupId","published":{"body":{"published":true}},"absent":{"body":{"published":false}}}' + new File(root, 'verify.sh').text = '#!/usr/bin/env bash\nexit 0\n' + new File(root, 'settings-helper.groovy').text = '// fixture settings helper' + File bin = new File(root, 'bin'); bin.mkdirs() + File groovy = new File(bin, 'groovy') + groovy.text = '#!/usr/bin/env bash\nexit 0\n'; groovy.setExecutable(true) + File maven = new File(bin, 'mvn') + maven.text = '#!/usr/bin/env bash\nprintf "%s\\n" "$*" >> build/maven-commands.txt\n'; maven.setExecutable(true) + ['matrix-bom', 'matrix-all'].each { name -> + String packaging = name == 'matrix-bom' ? 'pom' : 'jar' + String xml = """g${name}1${packaging}TestTesthttps://example.orgMIThttps://example.orgTesthttps://example.orgscm:git:example""" + new File(root, name + '.xml').text = xml + File staged = new File(root, "build/staging/g/${name}/1"); staged.mkdirs() + new File(staged, "${name}-1.pom").text = xml + new File(staged, "${name}-1.pom.asc").text = 'signature' + if (packaging == 'jar') { + new File(staged, "${name}-1.jar").text = 'jar' + new File(staged, "${name}-1.jar.asc").text = 'signature' + } + } + new File(root, 'build.gradle').text = """ +plugins { id 'se.alipsa.nexus-release-all' } +releaseAllPlan { + sourceDirectory = layout.projectDirectory + configuredRevision = providers.exec { commandLine 'git', 'rev-parse', 'HEAD' }.standardOutput.asText.get().trim() + candidatesJson = '[]' + bomPom = layout.projectDirectory.file('matrix-bom.xml') + allPom = layout.projectDirectory.file('matrix-all.xml') + portalFixture = layout.projectDirectory.file('fixture.json') + repositoryUrl = '${base}/repo' + portalUrl = '${base}/api/v1' + userName = 'u'; password = 'p' + manifestFile = layout.buildDirectory.file('manifest.json') +} +bundleAll { + sourceDirectory = layout.projectDirectory + manifestFile = layout.buildDirectory.file('manifest.json') + stagingDirectory = layout.buildDirectory.dir('staging') + bundleFile = layout.buildDirectory.file('bundle.zip') + receiptFile = layout.buildDirectory.file('receipt.json') +} +releaseAllGuard { + sourceDirectory = layout.projectDirectory + manifestFile = layout.buildDirectory.file('manifest.json') + versionsJson = '{}' +} +releaseAllPrepareMaven { + sourceDirectory = layout.projectDirectory + manifestFile = layout.buildDirectory.file('manifest.json') + stagingDirectory = layout.buildDirectory.dir('staging') + localRepository = layout.buildDirectory.dir('maven-local') + mavenWorkDirectory = layout.projectDirectory + verifierFile = layout.projectDirectory.file('verify.sh') + settingsHelperFile = layout.projectDirectory.file('settings-helper.groovy') +} +""" + ReleaseManifest.git(root, 'init'); ReleaseManifest.git(root, 'add', '.') + ReleaseManifest.git(root, '-c', 'user.name=Test', '-c', 'user.email=test@example.org', 'commit', '-m', 'Fixture') + run('releaseAllPlan') + assertTrue(run('releaseAllPlan').output.contains('Reusing configuration cache')) + assertEquals(2, ReleaseManifest.read(new File(root, 'build/manifest.json')).selected.size()) + assertEquals(['matrix-bom.xml', 'matrix-all.xml'], ReleaseManifest.read(new File(root, 'build/manifest.json')).selected*.pomFile) + run('bundleAll') + assertTrue(run('bundleAll').output.contains('Reusing configuration cache')) + assertTrue(new File(root, 'build/bundle.zip').isFile()) + run('releaseAllGuard') + assertTrue(run('releaseAllGuard').output.contains('Reusing configuration cache')) + new File(root, 'build/staging').eachFileRecurse { File artifact -> + if (artifact.isFile()) { + File installed = new File(root, 'build/maven-local/' + new File(root, 'build/staging').toPath().relativize(artifact.toPath()).toString()) + installed.parentFile.mkdirs(); installed.bytes = artifact.bytes + } + } + run('releaseAllPrepareMaven') + assertTrue(run('releaseAllPrepareMaven').output.contains('Reusing configuration cache')) + assertEquals(4, new File(root, 'build/maven-commands.txt').readLines().size()) + assertTrue(new File(root, 'build/maven-commands.txt').readLines().every { it.contains('-Prelease') && it.endsWith(' install') && !it.contains('deploy') }) + new File(root, 'matrix-bom.xml').append('changed') + def result = runner('bundleAll').buildAndFail() + assertTrue(result.output.contains('Stale manifest')) + } finally { server.stop(0) } + } + + private def run(String task) { runner(task).build() } + private GradleRunner runner(String task) { + GradleRunner.create().withProjectDir(root).withPluginClasspath() + .withEnvironment(new HashMap(System.getenv()) + [PATH: new File(root, 'bin').path + File.pathSeparator + System.getenv('PATH')]) + .withArguments(task, '--configuration-cache', '--configuration-cache-problems=fail', '--stacktrace') + } +} From b51fff82250ddff9ad7dacb53ad485fc62f8175f Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Fri, 9 Oct 2026 19:33:20 +0200 Subject: [PATCH 2/6] Exclude unpublished Maven test dependencies from eligibility --- .../alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy | 2 +- .../se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy index 9949de6..4be5264 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy @@ -53,7 +53,7 @@ abstract class ReleaseAllPlanTask extends DefaultTask { def publication = new PublicationDescriptor(pomFile: pomPath, coordinate: new ReleaseCoordinate(parsed.groupId, parsed.artifactId, parsed.version), packaging: parsed.packaging, role: parsed.is(bom) ? 'bom' : 'aggregate', sourcesAndJavadocRequired: false, - dependencies: (parsed.dependencies + parsed.management).findAll { it.version }, + dependencies: (parsed.dependencies.findAll { it.scope != 'test' } + parsed.management).findAll { it.version }, publishedDependencies: parsed.dependencies.findAll { it.scope != 'test' }, publishedManagement: parsed.is(bom) ? parsed.management : [], files: ["${prefix}.pom".toString()]) if (parsed.packaging == 'jar') publication.files.add("${prefix}.jar".toString()) candidates.add(publication) diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy index e68083c..78f9e3a 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy @@ -33,9 +33,11 @@ class CombinedTasksTest { ['matrix-bom', 'matrix-all'].each { name -> String packaging = name == 'matrix-bom' ? 'pom' : 'jar' String xml = """g${name}1${packaging}TestTesthttps://example.orgMIThttps://example.orgTesthttps://example.orgscm:git:example""" + String testOnly = name == 'matrix-all' ? 'gtest-only1-SNAPSHOTtest' : '' + xml = xml.replace('', testOnly + '') new File(root, name + '.xml').text = xml File staged = new File(root, "build/staging/g/${name}/1"); staged.mkdirs() - new File(staged, "${name}-1.pom").text = xml + new File(staged, "${name}-1.pom").text = testOnly ? xml.replace(testOnly, '') : xml new File(staged, "${name}-1.pom.asc").text = 'signature' if (packaging == 'jar') { new File(staged, "${name}-1.jar").text = 'jar' @@ -84,6 +86,7 @@ releaseAllPrepareMaven { assertTrue(run('releaseAllPlan').output.contains('Reusing configuration cache')) assertEquals(2, ReleaseManifest.read(new File(root, 'build/manifest.json')).selected.size()) assertEquals(['matrix-bom.xml', 'matrix-all.xml'], ReleaseManifest.read(new File(root, 'build/manifest.json')).selected*.pomFile) + assertFalse(ReleaseManifest.read(new File(root, 'build/manifest.json')).selected.find { it.artifactId == 'matrix-all' }.dependencies.any { it.scope == 'test' }) run('bundleAll') assertTrue(run('bundleAll').output.contains('Reusing configuration cache')) assertTrue(new File(root, 'build/bundle.zip').isFile()) From 93f113907e1b0471f8c4baf3a0d487c5449130ae Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Fri, 9 Oct 2026 23:24:01 +0200 Subject: [PATCH 3/6] Fix combined release planning, recovery and validation --- combined-releases.md | 22 +++- .../plugin/release/CentralPortalClient.groovy | 8 +- .../plugin/release/CombinedBundle.groovy | 19 +++- .../plugin/release/CombinedDeployment.groovy | 52 ++++++--- .../plugin/release/PrepareMavenTask.groovy | 17 ++- .../plugin/release/PublicationStatus.groovy | 4 +- .../plugin/release/ReleaseAllPlanTask.groovy | 26 ++++- .../release/ReleaseAllSupportPlugin.groovy | 16 ++- .../plugin/release/ReleaseAllTask.groovy | 1 + .../plugin/release/ReleaseManifest.groovy | 16 ++- .../plugin/release/ReleasePlanner.groovy | 7 +- .../gradle/plugin/release/ReleasePom.groovy | 1 - .../plugin/release/CombinedReleaseTest.groovy | 101 +++++++++++++++++- .../plugin/release/CombinedTasksTest.groovy | 49 ++++++++- 14 files changed, 292 insertions(+), 47 deletions(-) diff --git a/combined-releases.md b/combined-releases.md index 5a7553d..f574638 100644 --- a/combined-releases.md +++ b/combined-releases.md @@ -18,10 +18,10 @@ The source repository must be clean and committed. Generated files belong in ign | --- | --- | | `releaseAllPlan` | `sourceDirectory`, `configuredRevision`, `candidatesJson`, generated `candidatePoms`, static `bomPom`/`allPom`, `repositoryUrl`, `portalUrl`, `userName`, `password`, `portalFixture`; writes `manifestFile` and optional `reportFile`. Never invokes Maven or resolves Maven candidates. | | `releaseAllSigningCheck` | `manifestFile`, `configuredSigningJson` mapping project paths to signatory availability, `strict`; reports unavailable Gradle signing or fails when strict. No signing operation. | -| `releaseAllGuard` | `sourceDirectory`, `manifestFile`, `versionsJson`; checks revision, clean tree, source hashes and effective publication versions before staging tasks run. | -| `releaseAllPrepareMaven` | `sourceDirectory`, `manifestFile`, `stagingDirectory`, separate `localRepository`, `mavenWorkDirectory`, `verifierFile`, `settingsHelperFile`, optional `userSettingsFile` (default `~/.m2/settings.xml`); runs the caller's verifier and Maven `install -Prelease`, never deploy. The settings helper takes source user-settings, staged repository, output-settings and repository ID; it must preserve signing settings without logging secrets. | -| `bundleAll` | `sourceDirectory`, `manifestFile`, `stagingDirectory`; writes `bundleFile` and `receiptFile`. Rechecks source, dependency POMs, metadata, artifact policy, signatures and staged checksums. Generates MD5/SHA-1 for artifacts/signatures. | -| `releaseAll` | `sourceDirectory`, `manifestFile`, `bundleFile`, `receiptFile`, `recordFile`, `portalFixture`, repository/Portal URLs and credentials; `resumeId` defaults to the caller-supplied empty string for a new upload, or a saved deployment ID for status-only recovery. Rechecks availability and uploads once. | +| `releaseAllGuard` | `sourceDirectory`, `manifestFile`, `versionsJson`; checks revision, clean tree, publication-POM hashes and Git index flags and effective publication versions before staging tasks run. | +| `releaseAllPrepareMaven` | `sourceDirectory`, `manifestFile`, `stagingDirectory`, separate `localRepository`, `mavenWorkDirectory`, `verifierFile`, `settingsHelperFile`, `stagingRepositoryId` (default `release-staging`), optional `userSettingsFile` (default `~/.m2/settings.xml`); runs the caller's verifier and Maven `install -Prelease`, never deploy. The settings helper takes source user-settings, staged repository, output-settings and repository ID; it must preserve signing settings without logging secrets. | +| `bundleAll` | `sourceDirectory`, `manifestFile`, `stagingDirectory`; writes `bundleFile` and `receiptFile`. Rechecks source, dependency POMs, metadata, artifact policy, nonempty signature files and staged checksums (signature cryptography is not verified). Generates MD5/SHA-1 for artifacts/signatures. | +| `releaseAll` | `sourceDirectory`, `manifestFile`, `bundleFile`, `receiptFile`, `recordFile`, `portalFixture`, repository/Portal URLs and credentials; `resumeId` defaults to an empty string for a new upload, or a saved deployment ID for status-only recovery. Rechecks availability and uploads once. | The Matrix build configures these properties in `gradle/release-all.gradle`. For example, a caller configures the bundle task after registering its own staging/preparation workflow: @@ -49,6 +49,12 @@ run with `--no-configuration-cache` independently. Publication descriptors preserve exact configured coordinates, expected filenames, source project/publication or Maven POM, dependency origins, and published dependency/management expectations. Include production compile and runtime project edges even if omitted by POMs. +Eligibility edges include candidate group/artifact pairs and groups covered by the optional +`releaseAllPlan.releaseNamespaces` list (exact namespaces and their subgroups). Third-party +dependencies remain in POM expectations but are not availability probes; test/system edges +are also excluded from eligibility. Candidate JSON requires a nonempty `files` list; omitted +`dependencies` defaults to an empty list. +Invalid release dependency versions exclude the dependent candidate rather than aborting planning. Only exact non-SNAPSHOT versions whose dependencies are released or in the final bundle are selected. Exclusions cascade; a selected module's mismatched BOM pin blocks the new BOM. An unselected module can retain an already-released BOM pin with a warning. @@ -57,6 +63,10 @@ Normal JAR components require main, sources and Javadoc JARs plus POM. POM compo only a POM. An explicitly configured `sourcesAndJavadocRequired: false` policy preserves an aggregate JAR's existing main-JAR/POM publication set. All expected files need nonempty signatures. No Gradle metadata, repository bookkeeping or unrelated components are copied. +Source guards hash publication POMs and require a clean, unchanged Git revision with no +assume-unchanged or skip-worktree index flags. Maven preparation requires a POSIX filesystem +and bash, groovy and mvn; Windows users must use WSL. Repository and Portal URLs default to +Maven Central and the Central Portal API. The ZIP size limit defaults to 1,000,000,000 bytes. Publication checks use exact repository POMs and authenticated Portal @@ -68,8 +78,12 @@ used in tests. Availability checks retry transient failures at most three times ## Interrupted deployments The record is written as `SUBMITTING` before HTTP and stores the ID immediately on success. +Definite HTTP 401/403/422 rejections are archived beside the active record as +`*.rejected-.json`, allowing a corrected new invocation. Other upload exceptions persist +`UPLOAD_ERROR` with diagnostics; invalid ID responses retain the raw response in `SUBMITTING`. Never automatically retry an unknown upload outcome. Resume only the saved ID, without source checks, builds or signing. Polling defaults to 60 attempts at 10-second intervals; +Transient I/O errors are recorded and polling continues within that bound. Availability probes use HEAD. Central propagation remains separate from Portal publication. A failed record reports all component coordinates. Archive an `AVAILABLE` record before a new deployment; manually reconcile every unresolved record rather than deleting it to retry. diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CentralPortalClient.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CentralPortalClient.groovy index 5cd7806..8b29786 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CentralPortalClient.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CentralPortalClient.groovy @@ -97,14 +97,14 @@ class CentralPortalClient extends WsClient { String body = e.body?.trim() switch (e.statusCode) { case 401: - return new GradleException("Authentication failed - check sonatypeUsername/sonatypePassword") + return new GradleException("Authentication failed - check sonatypeUsername/sonatypePassword", e) case 403: - return new GradleException("Forbidden - is the namespace verified in your Central Portal account?") + return new GradleException("Forbidden - is the namespace verified in your Central Portal account?", e) case 422: - return new GradleException("Rejected by Central Portal - see body for details: ${body}") + return new GradleException("Rejected by Central Portal - see body for details: ${body}", e) default: if (e.statusCode >= 500) { - return new GradleException("Central Portal returned ${e.statusCode} - try again later") + return new GradleException("Central Portal returned ${e.statusCode} - try again later", e) } return new GradleException("Upload failed with HTTP ${e.statusCode}: ${body}", e) } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy index 57fbbe1..ae259bb 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy @@ -11,6 +11,7 @@ class CombinedBundle { static void create(File staging, File target, List publications, long limit = MAX_BYTES) { if (publications.empty) throw new IllegalArgumentException('Cannot bundle an empty selection') Map entries = [:] + Map> checksums = [:] publications.each { Map publication -> def coordinate = new ReleaseCoordinate(publication.groupId, publication.artifactId, publication.version) coordinate.validate() @@ -32,9 +33,11 @@ class CombinedBundle { throw new IllegalArgumentException("Missing or unsafe release artifact: ${entry}") } if (entries.put(entry, source) != null) throw new IllegalArgumentException("Duplicate release path: ${entry}") + checksums[entry] = [:] ['MD5', 'SHA-1'].each { String algorithm -> File checksum = new File(source.path + '.' + algorithm.toLowerCase().replace('-', '')) String hash = ReleaseChecksums.checksum(source, algorithm) + checksums[entry][algorithm] = hash if (checksum.exists() && checksum.text.trim() != hash) throw new IllegalArgumentException("Incorrect checksum for ${entry}") } } @@ -49,12 +52,24 @@ class CombinedBundle { File temporary = new File(target.path + '.tmp') try { temporary.withOutputStream { output -> - new ZipOutputStream(output).withCloseable { zip -> + OutputStream bounded = new FilterOutputStream(output) { + long bytesWritten = 0 + @Override void write(int value) { + if (++bytesWritten > limit) throw new IllegalArgumentException('ZIP exceeds bundle limit') + output.write(value) + } + @Override void write(byte[] bytes, int offset, int length) { + bytesWritten += length + if (bytesWritten > limit) throw new IllegalArgumentException('ZIP exceeds bundle limit') + output.write(bytes, offset, length) + } + } + new ZipOutputStream(bounded).withCloseable { zip -> entries.sort().each { String path, File source -> zip.putNextEntry(new ZipEntry(path)); source.withInputStream { it.transferTo(zip) }; zip.closeEntry() ['MD5', 'SHA-1'].each { String algorithm -> zip.putNextEntry(new ZipEntry(path + '.' + algorithm.toLowerCase().replace('-', ''))) - zip.write(ReleaseChecksums.checksum(source, algorithm).getBytes('UTF-8')); zip.closeEntry() + zip.write(checksums[path][algorithm].getBytes('UTF-8')); zip.closeEntry() } } } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy index 85e9bd7..abb8928 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy @@ -20,8 +20,31 @@ class CombinedDeployment { throw new IllegalStateException('Deployment record was created concurrently; resume or reconcile it before a new upload') } // No retry: even an exception may mean the server accepted the upload. - String id = client.upload(bundle) - if (!id || !(id ==~ /[A-Za-z0-9-]+/)) throw new IllegalStateException('Upload outcome unknown; reconcile SUBMITTING record in Portal') + String id + try { id = client.upload(bundle) } + catch (Exception failure) { + Throwable cause = failure + while (cause.cause && !(cause instanceof WsException)) cause = cause.cause + boolean rejected = cause instanceof WsException && cause.statusCode in [401, 403, 422] + state.state = rejected ? 'UPLOAD_REJECTED' : 'UPLOAD_ERROR' + state.error = failure.message + if (cause instanceof WsException) { + state.httpStatus = cause.statusCode + state.rawResponse = cause.body + } + ReleaseManifest.write(record, state) + if (rejected) { + // Retain the rejected attempt, freeing the active journal only after a definite rejection. + java.nio.file.Files.move(record.toPath(), new File(record.path + '.rejected-' + UUID.randomUUID() + '.json').toPath()) + } + throw failure + } + if (!id || !(id ==~ /[A-Za-z0-9-]+/)) { + state.rawResponse = id + state.error = 'Upload response did not contain a valid deployment ID' + ReleaseManifest.write(record, state) + throw new IllegalStateException('Upload outcome unknown; reconcile SUBMITTING record in Portal') + } state.deploymentId = id state.state = 'UPLOADED' ReleaseManifest.write(record, state) @@ -33,17 +56,22 @@ class CombinedDeployment { Map state = ReleaseManifest.read(record) if (!id || state.deploymentId != id) throw new IllegalStateException('Deployment ID does not match the saved record') for (int attempt = 0; attempt < attempts; attempt++) { - Map status = client.getDeploymentStatus(id) - state.state = status.deploymentState - ReleaseManifest.write(record, state) - if (state.state == 'FAILED') { - String coordinates = state.manifest.selected.collect { "${it.groupId}:${it.artifactId}:${it.version}" }.join(', ') - throw new IllegalStateException("Deployment ${id} failed for ${coordinates}; inspect Portal for component validation errors") - } - if (state.state == 'PUBLISHED' && available(state.manifest as Map)) { - state.state = 'AVAILABLE' + try { + Map status = client.getDeploymentStatus(id) + state.state = status.deploymentState + ReleaseManifest.write(record, state) + if (state.state == 'FAILED') { + String coordinates = state.manifest.selected.collect { "${it.groupId}:${it.artifactId}:${it.version}" }.join(', ') + throw new IllegalStateException("Deployment ${id} failed for ${coordinates}; inspect Portal for component validation errors") + } + if (state.state == 'PUBLISHED' && available(state.manifest as Map)) { + state.state = 'AVAILABLE' + ReleaseManifest.write(record, state) + return + } + } catch (IOException failure) { + state.lastPollingError = failure.message ReleaseManifest.write(record, state) - return } if (attempt + 1 < attempts) Thread.sleep(interval) } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/PrepareMavenTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/PrepareMavenTask.groovy index 1a2948f..30a8030 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/PrepareMavenTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/PrepareMavenTask.groovy @@ -9,6 +9,7 @@ import org.gradle.work.DisableCachingByDefault /** Builds Maven publications locally, preserving user signing settings and isolating downloads. */ @DisableCachingByDefault(because = 'Signs local release artifacts') abstract class PrepareMavenTask extends DefaultTask { + @Input abstract org.gradle.api.provider.Property getStagingRepositoryId() @Internal abstract DirectoryProperty getSourceDirectory() @InputFile @PathSensitive(PathSensitivity.NONE) abstract RegularFileProperty getManifestFile() @Internal abstract DirectoryProperty getStagingDirectory() @@ -21,7 +22,13 @@ abstract class PrepareMavenTask extends DefaultTask { /** Runs only install, never deploy; validates flattened dependencies before staging. */ @TaskAction void prepare() { + if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows')) { + throw new IllegalStateException('Maven preparation requires a POSIX environment with bash, groovy and mvn; use WSL on Windows') + } File root = sourceDirectory.get().asFile + if (!java.nio.file.Files.getFileStore(root.toPath()).supportsFileAttributeView('posix')) { + throw new IllegalStateException('Maven preparation requires a POSIX filesystem for private signing settings') + } Map manifest = ReleaseManifest.read(manifestFile.get().asFile) ReleaseManifest.guard(root, manifest) File stage = stagingDirectory.get().asFile @@ -31,13 +38,13 @@ abstract class PrepareMavenTask extends DefaultTask { java.nio.file.attribute.PosixFilePermissions.asFileAttribute(java.nio.file.attribute.PosixFilePermissions.fromString('rw-------'))).toFile() File original = userSettingsFile.present ? userSettingsFile.get().asFile : new File(System.getProperty('user.home'), '.m2/settings.xml') try { - PrepareMavenTask.run(root, ['groovy', settingsHelperFile.get().asFile.path, original.path, stage.path, settingsFile.path, 'matrix-release-staging'], logger) + run(root, ['groovy', settingsHelperFile.get().asFile.path, original.path, stage.path, settingsFile.path, stagingRepositoryId.get()], logger) boolean mavenSelected = manifest.selected.any { !it.projectPath } if (!mavenSelected) return // Run existing verifier against staged artifacts, without publishing or cleaning staging. - PrepareMavenTask.run(root, ['bash', verifierFile.get().asFile.path, '--manifest', manifestFile.get().asFile.path, '--staging', stage.path, '--local', cache.path], logger) + run(root, ['bash', verifierFile.get().asFile.path, '--manifest', manifestFile.get().asFile.path, '--staging', stage.path, '--local', cache.path], logger) manifest.selected.findAll { !it.projectPath }.sort { it.role == 'bom' ? 0 : 1 }.each { Map component -> - PrepareMavenTask.run(mavenWorkDirectory.get().asFile, ['mvn', '-s', settingsFile.path, "-Dmaven.repo.local=${cache}", '-Prelease', '-f', new File(root, component.pomFile).path, 'install'], logger) + run(mavenWorkDirectory.get().asFile, ['mvn', '-s', settingsFile.path, "-Dmaven.repo.local=${cache}", '-Prelease', '-f', new File(root, component.pomFile).path, 'install'], logger) def coordinate = new ReleaseCoordinate(component.groupId, component.artifactId, component.version) File source = new File(cache, coordinate.path()) File destination = new File(stage, coordinate.path()) @@ -54,9 +61,9 @@ abstract class PrepareMavenTask extends DefaultTask { } finally { settingsFile.delete() } } - private static void run(File directory, List command, org.gradle.api.logging.Logger taskLogger) { + protected void run(File directory, List command, org.gradle.api.logging.Logger taskLogger) { Process process = new ProcessBuilder(command.collect { it.toString() }).directory(directory).redirectErrorStream(true).start() process.inputStream.withReader('UTF-8') { reader -> reader.eachLine { line -> taskLogger.lifecycle(line) } } - if (process.waitFor() != 0) throw new IllegalStateException('Maven preparation/API verification failed; no upload attempted. If a Maven mirror intercepts the staging file remote, exclude matrix-release-staging from mirrorOf.') + if (process.waitFor() != 0) throw new IllegalStateException("Maven preparation/API verification failed; no upload attempted. If a Maven mirror intercepts the staging file remote, exclude ${stagingRepositoryId.get()} from mirrorOf.") } } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationStatus.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationStatus.groovy index 3b71d25..29d746a 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationStatus.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/PublicationStatus.groovy @@ -55,8 +55,8 @@ class PublicationStatus { try { def response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString()) if (!(response.statusCode() in [429, 500, 502, 503, 504]) || attempt == attempts) return response - } catch (IOException ignored) { - if (attempt == attempts) throw new IOException('Availability request failed after bounded retries') + } catch (IOException failure) { + if (attempt == attempts) throw new IOException('Availability request failed after bounded retries', failure) } Thread.sleep(250L * attempt) } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy index 4be5264..4e661a7 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy @@ -13,6 +13,7 @@ import org.gradle.work.DisableCachingByDefault abstract class ReleaseAllPlanTask extends DefaultTask { @Input abstract Property getCandidatesJson() @Input abstract Property getConfiguredRevision() + @Input abstract org.gradle.api.provider.ListProperty getReleaseNamespaces() @Internal abstract DirectoryProperty getSourceDirectory() @InputFile @PathSensitive(PathSensitivity.RELATIVE) abstract RegularFileProperty getBomPom() @InputFile @PathSensitive(PathSensitivity.RELATIVE) abstract RegularFileProperty getAllPom() @@ -35,11 +36,16 @@ abstract class ReleaseAllPlanTask extends DefaultTask { if (ReleaseManifest.git(root, 'status', '--porcelain')) throw new IllegalStateException('Release planning requires a clean Git working tree') List candidates = [] new JsonSlurper().parseText(candidatesJson.get()).each { Map registered -> + if (!(registered.files instanceof List) || registered.files.empty) { + throw new IllegalArgumentException("Candidate ${registered.projectPath ?: registered.artifactId} must declare a nonempty files list") + } Map parsed = ReleasePom.read(new File(root, registered.pomFile)) def publication = new PublicationDescriptor(projectPath: registered.projectPath, publication: registered.publication, pomFile: registered.pomFile, coordinate: new ReleaseCoordinate(registered.groupId, registered.artifactId, registered.version), - dependencies: registered.dependencies + (parsed.dependencies + parsed.management).findAll { it.version }, - publishedDependencies: parsed.dependencies, publishedManagement: parsed.management, files: registered.files) + packaging: registered.packaging ?: parsed.packaging, + sourcesAndJavadocRequired: registered.sourcesAndJavadocRequired != false, + dependencies: (registered.dependencies ?: []) + (parsed.dependencies + parsed.management), + publishedDependencies: parsed.dependencies, publishedManagement: parsed.management, files: registered.files ?: []) candidates.add(publication) } Map bom = ReleasePom.read(bomPom.get().asFile) @@ -53,11 +59,19 @@ abstract class ReleaseAllPlanTask extends DefaultTask { def publication = new PublicationDescriptor(pomFile: pomPath, coordinate: new ReleaseCoordinate(parsed.groupId, parsed.artifactId, parsed.version), packaging: parsed.packaging, role: parsed.is(bom) ? 'bom' : 'aggregate', sourcesAndJavadocRequired: false, - dependencies: (parsed.dependencies.findAll { it.scope != 'test' } + parsed.management).findAll { it.version }, + dependencies: (parsed.dependencies.findAll { it.scope != 'test' } + parsed.management), publishedDependencies: parsed.dependencies.findAll { it.scope != 'test' }, publishedManagement: parsed.is(bom) ? parsed.management : [], files: ["${prefix}.pom".toString()]) if (parsed.packaging == 'jar') publication.files.add("${prefix}.jar".toString()) candidates.add(publication) } + Set candidateNames = candidates.collect { "${it.coordinate.groupId}:${it.coordinate.artifactId}".toString() } as Set + List namespaces = releaseNamespaces.get() + candidates.each { PublicationDescriptor publication -> + publication.dependencies = publication.dependencies.findAll { Map edge -> + !(edge.scope in ['test', 'system']) && (candidateNames.contains("${edge.groupId}:${edge.artifactId}".toString()) || + namespaces.any { edge.groupId == it || edge.groupId?.startsWith(it + '.') }) + } + } def status = new PublicationStatus(repositoryUrl: repositoryUrl.get(), portalUrl: portalUrl.get(), userName: userName.getOrElse(''), password: password.getOrElse(''), requireFixture: true, fixtureFile: portalFixture.get().asFile) List warnings = [] List selected = ReleasePlanner.select(candidates, { ReleaseCoordinate c -> status.published(c) }, warnings) @@ -71,9 +85,11 @@ abstract class ReleaseAllPlanTask extends DefaultTask { changed = scope.size() != previous } Map hashes = [:] - ReleaseManifest.git(root, 'ls-files').readLines().each { String path -> + ReleaseManifest.checkTrackedFlags(root) + candidates.collect { it.pomFile }.unique().each { String path -> File input = new File(root, path) - if (input.isFile()) hashes[path] = ReleaseChecksums.checksum(input, 'SHA-256') + if (!input.isFile()) throw new IllegalStateException("Missing publication input: ${path}") + hashes[path] = ReleaseChecksums.checksum(input, 'SHA-256') } Map manifest = [schemaVersion: 1, revision: configuredRevision.get(), hashes: hashes, versions: candidates.findAll { it.projectPath }.collectEntries { [it.projectPath, it.coordinate.version] }, diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllSupportPlugin.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllSupportPlugin.groovy index b900449..d085bd6 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllSupportPlugin.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllSupportPlugin.groovy @@ -10,8 +10,18 @@ class ReleaseAllSupportPlugin implements Plugin { project.tasks.register('releaseAllSigningCheck', SigningPreflightTask) project.tasks.register('releaseAllGuard', ReleaseSourceGuardTask) project.tasks.register('bundleAll', BundleAllTask) - project.tasks.register('releaseAll', ReleaseAllTask) - project.tasks.register('releaseAllPlan', ReleaseAllPlanTask) - project.tasks.register('releaseAllPrepareMaven', PrepareMavenTask) + project.tasks.register('releaseAll', ReleaseAllTask) { + resumeId.convention('') + repositoryUrl.convention('https://repo.maven.apache.org/maven2') + portalUrl.convention(CentralPortalClient.CENTRAL_PORTAL_URL) + } + project.tasks.register('releaseAllPlan', ReleaseAllPlanTask) { + releaseNamespaces.convention([]) + repositoryUrl.convention('https://repo.maven.apache.org/maven2') + portalUrl.convention(CentralPortalClient.CENTRAL_PORTAL_URL) + } + project.tasks.register('releaseAllPrepareMaven', PrepareMavenTask) { + stagingRepositoryId.convention('release-staging') + } } } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy index 412bf12..33881de 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllTask.groovy @@ -43,6 +43,7 @@ abstract class ReleaseAllTask extends DefaultTask { def coordinate = new ReleaseCoordinate(p.groupId, p.artifactId, p.version) p.files.findAll { it.endsWith('.pom') || it == "${p.artifactId}-${p.version}.jar" }.every { String name -> def connection = URI.create("${repositoryUrl.get()}/${coordinate.path()}/${name}").toURL().openConnection() + connection.requestMethod = 'HEAD' connection.connectTimeout = 15000; connection.readTimeout = 15000 try { connection.responseCode == 200 } finally { connection.disconnect() } } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy index 7d14819..26d47b6 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy @@ -6,13 +6,24 @@ import groovy.json.JsonSlurper /** Source guards and atomic, credential-free release records. */ class ReleaseManifest { /** Runs git without shell interpretation. */ - static String git(File root, String... arguments) { + static String git(File root, String... arguments) { gitOutput(root, arguments).trim() } + + static String gitOutput(File root, String... arguments) { Process process = new ProcessBuilder(['git', '-C', root.absolutePath] + arguments.toList()).redirectErrorStream(true).start() - String output = process.inputStream.getText('UTF-8').trim() + String output = process.inputStream.getText('UTF-8') if (process.waitFor() != 0) throw new IllegalStateException('Git source validation failed') output } + /** Rejects index flags that can hide working-tree changes, preserving arbitrary filenames. */ + static void checkTrackedFlags(File root) { + gitOutput(root, 'ls-files', '-v', '-z').split('\u0000').each { String entry -> + if (entry && (Character.isLowerCase(entry.charAt(0)) || entry.charAt(0) == ('S' as char))) { + throw new IllegalStateException("Source guard requires clearing assume-unchanged/skip-worktree: ${entry.substring(2)}") + } + } + } + /** Checks revision, cleanliness, input hashes and effective publication versions. */ static void guard(File root, Map manifest, Map versions = [:]) { (manifest.selected ?: []).each { Map component -> @@ -22,6 +33,7 @@ class ReleaseManifest { if (git(root, 'rev-parse', 'HEAD') != manifest.revision || git(root, 'status', '--porcelain')) { throw new IllegalStateException('Stale manifest: HEAD changed or working tree is dirty') } + checkTrackedFlags(root) manifest.hashes.each { String path, String hash -> File source = new File(root, path) if (!source.isFile() || ReleaseChecksums.checksum(source, 'SHA-256') != hash) throw new IllegalStateException("Stale manifest input: ${path}") diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy index 7c1b395..b256a9a 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy @@ -22,7 +22,12 @@ class ReleasePlanner { selected.each { PublicationDescriptor p -> p.dependencies.each { Map edge -> def dependency = new ReleaseCoordinate(edge.groupId, edge.artifactId, edge.version) - dependency.validate() + try { dependency.validate() } + catch (IllegalArgumentException invalid) { + p.reasons.add("Invalid release dependency: ${dependency.key()} (${edge.origin}): ${invalid.message}") + changed = true + return + } String reason if (dependency.version.endsWith('-SNAPSHOT')) reason = 'SNAPSHOT dependency' else if (!keys.contains(dependency.key()) && !lookup(dependency)) reason = 'Unavailable dependency' diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy index cf73520..3397744 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy @@ -24,7 +24,6 @@ class ReleasePom { String groupId = resolve(node.groupId.text()) String artifactId = resolve(node.artifactId.text()) String version = resolve(node.version.text() ?: managed["${groupId}:${artifactId}"] ?: '') - if (version || groupId.startsWith('se.alipsa.matrix')) new ReleaseCoordinate(groupId, artifactId, version).validate() [groupId: groupId, artifactId: artifactId, version: version, scope: resolve(node.scope.text() ?: 'compile'), type: resolve(node.type.text() ?: 'jar'), classifier: resolve(node.classifier.text()), origin: origin] diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy index 2fd44fb..cd8fba2 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -134,7 +134,8 @@ class CombinedReleaseTest { @Override String upload(File file) { uploads++; throw new IOException('Disconnected after server accepted upload') } } assertThrows(IOException) { CombinedDeployment.submit(record, bundle, manifest, availability, client) } - assertEquals('SUBMITTING', ReleaseManifest.read(record).state) + assertEquals('UPLOAD_ERROR', ReleaseManifest.read(record).state) + assertTrue(ReleaseManifest.read(record).error.contains('Disconnected')) assertThrows(IllegalStateException) { CombinedDeployment.submit(record, bundle, manifest, availability, client) } assertEquals(1, uploads) } @@ -222,6 +223,104 @@ class CombinedReleaseTest { } finally { executor.shutdownNow() } } + @Test void rangesExcludeOnlyReleaseDependents() { + def bad = candidate('bad', '1') + bad.dependencies = [[groupId: 'g', artifactId: 'core', version: '[1,2)', origin: 'pom.dependencies']] + def good = candidate('good', '1') + assertEquals([good], ReleasePlanner.select([bad, good], { false })) + assertTrue(bad.reasons.first().contains('Invalid release dependency')) + File pom = new File(directory, 'range.pom') + pom.text = pomText('good', 'jar').replace('', 'third.partya[1,2)') + assertEquals('[1,2)', ReleasePom.read(pom).dependencies.first().version) + } + + @Test void nonAsciiIndexFlagsCannotHideChanges() { + File root = new File(directory, 'unicode'); root.mkdirs() + new File(root, 'å.txt').text = 'original' + git(root, 'init'); git(root, 'add', '.') + git(root, '-c', 'user.name=Test', '-c', 'user.email=test@example.org', 'commit', '-m', 'Fixture') + assertTrue(ReleaseManifest.gitOutput(root, 'ls-files', '-z').contains('å.txt')) + Map manifest = [revision: ReleaseManifest.git(root, 'rev-parse', 'HEAD'), hashes: [:]] + ReleaseManifest.guard(root, manifest) + ['assume-unchanged', 'skip-worktree'].each { flag -> + git(root, 'update-index', '--' + flag, 'å.txt') + def failure = assertThrows(IllegalStateException) { ReleaseManifest.guard(root, manifest) } + assertTrue(failure.message.contains('å.txt')) + git(root, 'update-index', '--no-' + flag, 'å.txt') + } + } + + @Test void definiteUploadRejectionsAreArchivedAndPermitCorrectedSubmission() { + File bundle = new File(directory, 'bundle.zip'); bundle.text = 'bundle' + Map manifest = [selected: []] + def availability = new PublicationStatus() + [401, 403, 422].each { int code -> + File record = new File(directory, "deployment-${code}.json") + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { throw new org.gradle.api.GradleException('rejected', new WsException(code, 'details')) } + } + assertThrows(org.gradle.api.GradleException) { CombinedDeployment.submit(record, bundle, manifest, availability, client) } + assertFalse(record.exists()) + File archived = directory.listFiles().find { it.name.startsWith(record.name + '.rejected-') } + assertEquals('UPLOAD_REJECTED', ReleaseManifest.read(archived).state) + assertEquals('details', ReleaseManifest.read(archived).rawResponse) + def corrected = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { 'corrected-id' } + } + assertEquals('corrected-id', CombinedDeployment.submit(record, bundle, manifest, availability, corrected).deploymentId) + } + } + + @Test void invalidUploadIdRetainsResponseAndBlocksResubmission() { + File bundle = new File(directory, 'bundle.zip'); bundle.text = 'bundle' + File record = new File(directory, 'deployment.json') + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override String upload(File file) { 'unexpected response' } + } + assertThrows(IllegalStateException) { CombinedDeployment.submit(record, bundle, [selected: []], new PublicationStatus(), client) } + assertEquals('unexpected response', ReleaseManifest.read(record).rawResponse) + assertThrows(IllegalStateException) { CombinedDeployment.submit(record, bundle, [selected: []], new PublicationStatus(), client) } + } + + @Test void pollingSurvivesStatusAndPropagationIoErrors() { + File record = new File(directory, 'deployment.json') + ReleaseManifest.write(record, [deploymentId: 'id', state: 'UPLOADED', manifest: [selected: []]]) + int polls = 0 + int probes = 0 + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override Map getDeploymentStatus(String id) { + if (++polls == 1) throw new WsException(503, 'temporary') + [deploymentState: 'PUBLISHED'] + } + } + CombinedDeployment.resume(record, 'id', client, { + if (++probes == 1) throw new IOException('temporary propagation error') + true + }, 3, 0) + assertEquals(3, polls) + assertEquals('AVAILABLE', ReleaseManifest.read(record).state) + } + + @Test void availabilityRetriesPreserveIoCause() { + def socket = new ServerSocket(0) + int port = socket.localPort + socket.close() + def status = new PublicationStatus(repositoryUrl: "http://127.0.0.1:${port}", attempts: 1) + def failure = assertThrows(IOException) { status.published(new ReleaseCoordinate('g', 'a', '1')) } + assertTrue(failure.cause instanceof IOException) + } + + @Test void combinedTaskDefaultsArePresent() { + def project = org.gradle.testfixtures.ProjectBuilder.builder().withProjectDir(directory).build() + project.pluginManager.apply(ReleaseAllSupportPlugin) + def release = project.tasks.getByName('releaseAll') + assertEquals('', release.resumeId.get()) + assertEquals('https://repo.maven.apache.org/maven2', release.repositoryUrl.get()) + assertEquals(CentralPortalClient.CENTRAL_PORTAL_URL, release.portalUrl.get()) + assertEquals([], project.tasks.getByName('releaseAllPlan').releaseNamespaces.get()) + assertEquals('release-staging', project.tasks.getByName('releaseAllPrepareMaven').stagingRepositoryId.get()) + } + private static void git(File root, String... args) { ReleaseManifest.git(root, args) } diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy index 78f9e3a..d765d0f 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy @@ -30,10 +30,11 @@ class CombinedTasksTest { groovy.text = '#!/usr/bin/env bash\nexit 0\n'; groovy.setExecutable(true) File maven = new File(bin, 'mvn') maven.text = '#!/usr/bin/env bash\nprintf "%s\\n" "$*" >> build/maven-commands.txt\n'; maven.setExecutable(true) - ['matrix-bom', 'matrix-all'].each { name -> - String packaging = name == 'matrix-bom' ? 'pom' : 'jar' + ['matrix-bom', 'matrix-all', 'platform', 'slim'].each { name -> + String packaging = name in ['matrix-bom', 'platform'] ? 'pom' : 'jar' String xml = """g${name}1${packaging}TestTesthttps://example.orgMIThttps://example.orgTesthttps://example.orgscm:git:example""" String testOnly = name == 'matrix-all' ? 'gtest-only1-SNAPSHOTtest' : '' + if (name == 'slim') xml = xml.replace('', 'externalprivate-library[1,2)') xml = xml.replace('', testOnly + '') new File(root, name + '.xml').text = xml File staged = new File(root, "build/staging/g/${name}/1"); staged.mkdirs() @@ -49,7 +50,7 @@ plugins { id 'se.alipsa.nexus-release-all' } releaseAllPlan { sourceDirectory = layout.projectDirectory configuredRevision = providers.exec { commandLine 'git', 'rev-parse', 'HEAD' }.standardOutput.asText.get().trim() - candidatesJson = '[]' + candidatesJson = '[{"projectPath":":platform","publication":"maven","pomFile":"platform.xml","groupId":"g","artifactId":"platform","version":"1","files":["platform-1.pom"]},{"projectPath":":slim","publication":"maven","pomFile":"slim.xml","groupId":"g","artifactId":"slim","version":"1","sourcesAndJavadocRequired":false,"files":["slim-1.pom","slim-1.jar"]}]' bomPom = layout.projectDirectory.file('matrix-bom.xml') allPom = layout.projectDirectory.file('matrix-all.xml') portalFixture = layout.projectDirectory.file('fixture.json') @@ -83,9 +84,13 @@ releaseAllPrepareMaven { ReleaseManifest.git(root, 'init'); ReleaseManifest.git(root, 'add', '.') ReleaseManifest.git(root, '-c', 'user.name=Test', '-c', 'user.email=test@example.org', 'commit', '-m', 'Fixture') run('releaseAllPlan') + Map planned = ReleaseManifest.read(new File(root, 'build/manifest.json')) + assertEquals('pom', planned.selected.find { it.artifactId == 'platform' }.packaging) + assertFalse(planned.selected.find { it.artifactId == 'slim' }.sourcesAndJavadocRequired) + assertEquals(4, planned.hashes.size()) assertTrue(run('releaseAllPlan').output.contains('Reusing configuration cache')) - assertEquals(2, ReleaseManifest.read(new File(root, 'build/manifest.json')).selected.size()) - assertEquals(['matrix-bom.xml', 'matrix-all.xml'], ReleaseManifest.read(new File(root, 'build/manifest.json')).selected*.pomFile) + assertEquals(4, ReleaseManifest.read(new File(root, 'build/manifest.json')).selected.size()) + assertEquals(['platform.xml', 'slim.xml', 'matrix-bom.xml', 'matrix-all.xml'], ReleaseManifest.read(new File(root, 'build/manifest.json')).selected*.pomFile) assertFalse(ReleaseManifest.read(new File(root, 'build/manifest.json')).selected.find { it.artifactId == 'matrix-all' }.dependencies.any { it.scope == 'test' }) run('bundleAll') assertTrue(run('bundleAll').output.contains('Reusing configuration cache')) @@ -108,6 +113,40 @@ releaseAllPrepareMaven { } finally { server.stop(0) } } + @Test void resumeUsesHeadForRepositoryArtifacts() { + List methods = Collections.synchronizedList([]) + HttpServer server = HttpServer.create(new InetSocketAddress('127.0.0.1', 0), 0) + server.createContext('/') { exchange -> + if (exchange.requestURI.path.contains('/publisher/status')) { + byte[] body = '{"deploymentState":"PUBLISHED"}'.getBytes('UTF-8') + exchange.sendResponseHeaders(200, body.length) + exchange.responseBody.withCloseable { it.write(body) } + } else { + methods.add(exchange.requestMethod) + exchange.sendResponseHeaders(200, -1) + exchange.close() + } + } + server.start() + try { + String base = "http://127.0.0.1:${server.address.port}" + new File(root, 'settings.gradle').text = "rootProject.name='resume-fixture'" + ReleaseManifest.write(new File(root, 'record.json'), [deploymentId: 'saved-id', state: 'UPLOADED', manifest: [selected: [[groupId: 'g', artifactId: 'a', version: '1', files: ['a-1.pom', 'a-1.jar']]]]]) + new File(root, 'build.gradle').text = """ +plugins { id 'se.alipsa.nexus-release-all' } +releaseAll { + recordFile = layout.projectDirectory.file('record.json') + resumeId = 'saved-id' + repositoryUrl = '${base}/repo' + portalUrl = '${base}/api/v1' +} +""" + run('releaseAll') + assertEquals(['HEAD', 'HEAD'], methods) + assertEquals('AVAILABLE', ReleaseManifest.read(new File(root, 'record.json')).state) + } finally { server.stop(0) } + } + private def run(String task) { runner(task).build() } private GradleRunner runner(String task) { GradleRunner.create().withProjectDir(root).withPluginClasspath() From 2ef5b2801f421bfea1217e7152cbb47b1e5a7eaa Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Sat, 10 Oct 2026 11:24:24 +0200 Subject: [PATCH 4/6] Harden combined release journaling, uploads and POM validation --- .../plugin/release/CentralPortalClient.groovy | 2 +- .../plugin/release/CombinedBundle.groovy | 6 +- .../plugin/release/CombinedDeployment.groovy | 4 + .../plugin/release/ReleaseAllPlanTask.groovy | 3 + .../plugin/release/ReleaseManifest.groovy | 18 +++-- .../gradle/plugin/release/ReleasePom.groovy | 17 +++- .../release/SigningPreflightTask.groovy | 2 + .../gradle/plugin/release/WsClient.groovy | 21 +++-- .../release/CentralUploadApiTest.groovy | 1 + .../plugin/release/CombinedReleaseTest.groovy | 78 +++++++++++++++++++ 10 files changed, 129 insertions(+), 23 deletions(-) diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CentralPortalClient.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CentralPortalClient.groovy index 8b29786..025ad37 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CentralPortalClient.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CentralPortalClient.groovy @@ -84,7 +84,7 @@ class CentralPortalClient extends WsClient { body = new JsonSlurper().parseText(result.get(BODY) as String) as Map } catch (Exception e) { String responseBody = result.get(BODY) as String - throw new GradleException("Failed to parse deployment status response for deployment ID ${deploymentId}: ${e.message}. Body: ${responseBody}") + throw new IOException("Failed to parse deployment status response for deployment ID ${deploymentId}: ${e.message}. Body: ${responseBody}", e) } return body as Map } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy index ae259bb..25c3040 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedBundle.groovy @@ -38,7 +38,7 @@ class CombinedBundle { File checksum = new File(source.path + '.' + algorithm.toLowerCase().replace('-', '')) String hash = ReleaseChecksums.checksum(source, algorithm) checksums[entry][algorithm] = hash - if (checksum.exists() && checksum.text.trim() != hash) throw new IllegalArgumentException("Incorrect checksum for ${entry}") + if (checksum.exists() && checksum.text.trim().split(/\s+/)[0].toLowerCase(Locale.ROOT) != hash) throw new IllegalArgumentException("Incorrect checksum for ${entry}") } } } @@ -49,7 +49,7 @@ class CombinedBundle { if (!metadataErrors.empty) throw new IllegalArgumentException(metadataErrors.join('; ')) } target.parentFile.mkdirs() - File temporary = new File(target.path + '.tmp') + File temporary = java.nio.file.Files.createTempFile(target.absoluteFile.parentFile.toPath(), target.name + '.', '.tmp').toFile() try { temporary.withOutputStream { output -> OutputStream bounded = new FilterOutputStream(output) { @@ -75,7 +75,7 @@ class CombinedBundle { } } if (temporary.length() > limit) throw new IllegalArgumentException('ZIP exceeds bundle limit') - java.nio.file.Files.move(temporary.toPath(), target.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) + java.nio.file.Files.move(temporary.toPath(), target.toPath(), java.nio.file.StandardCopyOption.ATOMIC_MOVE, java.nio.file.StandardCopyOption.REPLACE_EXISTING) } finally { temporary.delete() } } } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy index abb8928..15ec5c9 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/CombinedDeployment.groovy @@ -58,6 +58,9 @@ class CombinedDeployment { for (int attempt = 0; attempt < attempts; attempt++) { try { Map status = client.getDeploymentStatus(id) + if (!(status?.deploymentState in ['PENDING', 'VALIDATING', 'VALIDATED', 'PUBLISHING', 'PUBLISHED', 'FAILED'])) { + throw new IOException('Invalid deployment status response: missing or unknown deploymentState') + } state.state = status.deploymentState ReleaseManifest.write(record, state) if (state.state == 'FAILED') { @@ -70,6 +73,7 @@ class CombinedDeployment { return } } catch (IOException failure) { + if (failure instanceof WsException && failure.statusCode in [401, 403]) throw failure state.lastPollingError = failure.message ReleaseManifest.write(record, state) } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy index 4e661a7..d0ab720 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseAllPlanTask.groovy @@ -48,6 +48,9 @@ abstract class ReleaseAllPlanTask extends DefaultTask { publishedDependencies: parsed.dependencies, publishedManagement: parsed.management, files: registered.files ?: []) candidates.add(publication) } + candidates.groupBy { it.projectPath }.each { String path, List publications -> + if (path && publications.size() > 1) throw new IllegalArgumentException("Multiple publications for project ${path} are not supported by the version guard") + } Map bom = ReleasePom.read(bomPom.get().asFile) Map managed = bom.management.collectEntries { ["${it.groupId}:${it.artifactId}".toString(), it.version] } Map all = ReleasePom.read(allPom.get().asFile, bom.properties, managed) diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy index 26d47b6..1f03e97 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleaseManifest.groovy @@ -9,10 +9,12 @@ class ReleaseManifest { static String git(File root, String... arguments) { gitOutput(root, arguments).trim() } static String gitOutput(File root, String... arguments) { - Process process = new ProcessBuilder(['git', '-C', root.absolutePath] + arguments.toList()).redirectErrorStream(true).start() - String output = process.inputStream.getText('UTF-8') - if (process.waitFor() != 0) throw new IllegalStateException('Git source validation failed') - output + Process process = new ProcessBuilder(['git', '-C', root.absolutePath] + arguments.toList()).start() + StringBuffer output = new StringBuffer() + StringBuffer errors = new StringBuffer() + process.waitForProcessOutput(output, errors) + if (process.exitValue() != 0) throw new IllegalStateException('Git source validation failed: ' + errors.toString().trim()) + output.toString() } /** Rejects index flags that can hide working-tree changes, preserving arbitrary filenames. */ @@ -49,8 +51,10 @@ class ReleaseManifest { /** Atomically replaces a JSON file in its output directory. */ static void write(File file, Map value) { file.parentFile.mkdirs() - File temporary = new File(file.path + '.tmp') - temporary.setText(JsonOutput.prettyPrint(JsonOutput.toJson(value)), 'UTF-8') - java.nio.file.Files.move(temporary.toPath(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING) + File temporary = java.nio.file.Files.createTempFile(file.absoluteFile.parentFile.toPath(), file.name + '.', '.tmp').toFile() + try { + temporary.setText(JsonOutput.prettyPrint(JsonOutput.toJson(value)), 'UTF-8') + java.nio.file.Files.move(temporary.toPath(), file.toPath(), java.nio.file.StandardCopyOption.ATOMIC_MOVE, java.nio.file.StandardCopyOption.REPLACE_EXISTING) + } finally { temporary.delete() } } } diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy index 3397744..2efa32c 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePom.groovy @@ -7,6 +7,7 @@ class ReleasePom { /** Reads exact dependencies with inherited BOM properties and management. */ static Map read(File file, Map inherited = [:], Map managed = [:]) { def pom = new XmlSlurper().parse(file) + if (pom.parent.size()) throw new IllegalArgumentException("Parent POM inheritance is not supported for ${file.name}; provide a self-contained POM") Map properties = new LinkedHashMap<>(inherited) pom.properties.children().each { properties[it.name().toString()] = it.text() } properties['project.version'] = pom.version.text() @@ -42,7 +43,21 @@ class ReleasePom { if (actual[key] != expected[key]) throw new IllegalArgumentException("Published POM ${key} differs from manifest") } def signature = { Map edge -> ['groupId', 'artifactId', 'version', 'scope', 'type', 'classifier'].collect { edge[it] ?: '' }.join(':') } - if (actual.dependencies.collect(signature).sort() != expected.publishedDependencies.collect(signature).sort()) { + List unmatched = new ArrayList<>(actual.dependencies) + boolean dependenciesMatch = expected.publishedDependencies.every { Map edge -> + int index = unmatched.findIndexOf { Map published -> + if (edge.version) return signature(published) == signature(edge) + if (signature(published + [version: '']) != signature(edge)) return false + try { + new ReleaseCoordinate(published.groupId, published.artifactId, published.version).validate() + } catch (IllegalArgumentException ignored) { return false } + !published.version.endsWith('-SNAPSHOT') + } + if (index < 0) return false + unmatched.remove(index) + true + } + if (!dependenciesMatch || !unmatched.empty) { throw new IllegalArgumentException('Published dependencies differ from manifest') } if (expected.containsKey('publishedManagement') && actual.management.collect(signature).sort() != expected.publishedManagement.collect(signature).sort()) { diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/SigningPreflightTask.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/SigningPreflightTask.groovy index 8b0a666..83f7293 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/SigningPreflightTask.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/SigningPreflightTask.groovy @@ -14,6 +14,8 @@ abstract class SigningPreflightTask extends DefaultTask { @InputFile @PathSensitive(PathSensitivity.NONE) abstract RegularFileProperty getManifestFile() @Input abstract Property getStrict() + SigningPreflightTask() { strict.convention(false) } + /** Reports missing setup in dry-run; blocks preparation without signing configuration. */ @TaskAction void checkSigning() { diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/WsClient.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/WsClient.groovy index 3f4b30e..80451f5 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/WsClient.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/WsClient.groovy @@ -68,19 +68,18 @@ abstract class WsClient { conn.setRequestProperty("Accept", "application/json") conn.setRequestProperty("Authorization", auth(username, password)) + byte[] prefix = ("${twoHyphens}${boundary}${lineEnd}" + + "Content-Disposition: form-data; name=\"${fieldName}\"; filename=\"${file.name}\"${lineEnd}" + + "Content-Type: application/zip${lineEnd}${lineEnd}").getBytes('UTF-8') + byte[] suffix = "${lineEnd}${twoHyphens}${boundary}--${lineEnd}".getBytes('UTF-8') + conn.setFixedLengthStreamingMode(file.length() + prefix.length + suffix.length) try { conn.connect() - - def out = new DataOutputStream(conn.outputStream) - out.writeBytes("${twoHyphens}${boundary}${lineEnd}") - out.writeBytes("Content-Disposition: form-data; name=\"${fieldName}\"; filename=\"${file.name}\"${lineEnd}") - out.writeBytes("Content-Type: application/zip${lineEnd}") - out.writeBytes(lineEnd) - out.write(file.bytes) - out.writeBytes(lineEnd) - out.writeBytes("${twoHyphens}${boundary}--${lineEnd}") - out.flush() - out.close() + try (OutputStream out = conn.outputStream; InputStream input = new FileInputStream(file)) { + out.write(prefix) + input.transferTo(out) + out.write(suffix) + } int responseCode = conn.responseCode ensureSuccess(responseCode, '', conn) diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CentralUploadApiTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CentralUploadApiTest.groovy index ef56811..a822502 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CentralUploadApiTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CentralUploadApiTest.groovy @@ -78,6 +78,7 @@ class CentralUploadApiTest { portalClient.upload(zipFile) def recorded = server.takeRequest() + assertEquals(recorded.bodySize.toString(), recorded.getHeader("Content-Length")) assert recorded.method == "POST" assert recorded.path.contains("publishingType=AUTOMATIC") assert recorded.getHeader("Authorization")?.startsWith("Bearer ") diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy index cd8fba2..806519f 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -89,6 +89,8 @@ class CombinedReleaseTest { } new File(directory, 'download.pom').text = 'unrelated' File zip = new File(directory, 'bundle.zip') + File jar = new File(directory, 'g/matrix-all/1/matrix-all-1.jar') + new File(jar.path + '.sha1').text = ReleaseChecksums.checksum(jar, 'SHA-1').toUpperCase(Locale.ROOT) + ' ' + jar.name CombinedBundle.create(directory, zip, [pom, all]) new ZipFile(zip).withCloseable { archive -> assertEquals(18, archive.size()) @@ -317,10 +319,86 @@ class CombinedReleaseTest { assertEquals('', release.resumeId.get()) assertEquals('https://repo.maven.apache.org/maven2', release.repositoryUrl.get()) assertEquals(CentralPortalClient.CENTRAL_PORTAL_URL, release.portalUrl.get()) + assertFalse(project.tasks.getByName('releaseAllSigningCheck').strict.get()) assertEquals([], project.tasks.getByName('releaseAllPlan').releaseNamespaces.get()) assertEquals('release-staging', project.tasks.getByName('releaseAllPrepareMaven').stagingRepositoryId.get()) } + @Test void gitWarningsStayOutOfSourceOutput() { + git(directory, 'init') + git(directory, 'config', 'alias.release-warning', '!echo warning >&2; echo revision') + assertEquals('revision', ReleaseManifest.git(directory, 'release-warning')) + def failure = assertThrows(IllegalStateException) { + ReleaseManifest.git(directory, 'rev-parse', '--verify', 'missing-ref') + } + assertTrue(failure.message.contains('fatal:')) + } + + @Test void concurrentRecordUpdatesKeepValidJournal() { + File record = new File(directory, 'journal.json') + ReleaseManifest.write(record, [state: 'UPLOADED']) + def executor = java.util.concurrent.Executors.newFixedThreadPool(3) + try { + def writers = (1..2).collect { int writer -> + executor.submit({ -> + 100.times { ReleaseManifest.write(record, [state: 'UPLOADED', writer: writer]) } + } as Runnable) + } + 300.times { assertEquals('UPLOADED', ReleaseManifest.read(record).state) } + writers.each { it.get(10, java.util.concurrent.TimeUnit.SECONDS) } + assertFalse(directory.listFiles().any { it.name.endsWith('.tmp') }) + } finally { executor.shutdownNow() } + } + + @Test void parentPomsAreRejectedClearlyAndExternalManagedVersionsCanBeFlattened() { + File pom = new File(directory, 'parent.pom') + pom.text = pomText('a', 'pom').replace('', 'gparent1') + assertTrue(assertThrows(IllegalArgumentException) { ReleasePom.read(pom) }.message.contains('Parent POM')) + String dependency = 'externalb' + pom.text = pomText('a', 'pom').replace('', dependency + '') + Map expected = ReleasePom.read(pom) + expected.publishedDependencies = expected.dependencies + pom.text = pom.text.replace('b', 'b2') + ReleasePom.compare(pom, expected) + pom.text = pom.text.replace('b', 'c') + assertThrows(IllegalArgumentException) { ReleasePom.compare(pom, expected) } + } + + @Test void wildcardDependencyVersionsMustBeConcreteReleases() { + File pom = new File(directory, 'external.pom') + String source = pomText('a', 'pom').replace('', 'externalb') + pom.text = source + Map expected = ReleasePom.read(pom) + expected.publishedDependencies = expected.dependencies + ['', '2-SNAPSHOT', '[1.0,2.0)', '${unresolved}', '2.+', 'latest.release'].each { String version -> + pom.text = source.replace('b', "b${version}") + assertThrows(IllegalArgumentException) { ReleasePom.compare(pom, expected) } + } + pom.text = source.replace('b', 'b2.0') + ReleasePom.compare(pom, expected) + } + + @Test void pollingRejectsAuthenticationImmediatelyAndPreservesStateOnBadResponses() { + [401, 403].each { int code -> + File record = new File(directory, "auth-${code}.json") + ReleaseManifest.write(record, [deploymentId: 'id', state: 'UPLOADED']) + int polls = 0 + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override Map getDeploymentStatus(String id) { polls++; throw new WsException(code, 'denied') } + } + assertThrows(WsException) { CombinedDeployment.resume(record, 'id', client, { false }, 3, 0) } + assertEquals(1, polls) + assertEquals('UPLOADED', ReleaseManifest.read(record).state) + } + File record = new File(directory, 'invalid.json') + ReleaseManifest.write(record, [deploymentId: 'id', state: 'UPLOADED']) + def client = new CentralPortalClient(org.gradle.api.logging.Logging.getLogger(CombinedReleaseTest), null, 'u', 'p') { + @Override Map getDeploymentStatus(String id) { [:] } + } + assertThrows(IllegalStateException) { CombinedDeployment.resume(record, 'id', client, { false }, 2, 0) } + assertEquals('UPLOADED', ReleaseManifest.read(record).state) + } + private static void git(File root, String... args) { ReleaseManifest.git(root, args) } From a1f6ebdf055ed06ca63250c30fd32fc5d25301c1 Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Sat, 10 Oct 2026 11:37:45 +0200 Subject: [PATCH 5/6] Reject invalid third-party release dependencies during planning --- .../plugin/release/ReleasePlanner.groovy | 12 +++++++ .../plugin/release/CombinedReleaseTest.groovy | 33 +++++++++++++++++++ .../plugin/release/CombinedTasksTest.groovy | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy index b256a9a..7d9bfe8 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy @@ -10,6 +10,18 @@ class ReleasePlanner { availability.computeIfAbsent(c.key()) { released(c) } } candidates.each { PublicationDescriptor p -> + // Published edges remain complete even when eligibility edges are filtered by namespace. + p.publishedDependencies.findAll { !(it.scope in ['test', 'system']) && it.version }.each { Map edge -> + def dependency = new ReleaseCoordinate(edge.groupId, edge.artifactId, edge.version) + try { dependency.validate() } + catch (IllegalArgumentException invalid) { + p.reasons.add("Invalid published dependency: ${dependency.key()}: ${invalid.message}") + return + } + if (dependency.version.endsWith('-SNAPSHOT')) { + p.reasons.add("SNAPSHOT dependency: ${dependency.key()} (${edge.origin})") + } + } p.coordinate.validate() if (p.coordinate.version.endsWith('-SNAPSHOT')) p.reasons.add('SNAPSHOT version') else if (lookup(p.coordinate)) p.reasons.add('Already released') diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy index 806519f..f869a1f 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -20,6 +20,39 @@ class CombinedReleaseTest { assertTrue(plot.reasons.first().contains('Unavailable')) } + @Test void invalidExternalPublishedVersionsExcludeCandidatesAndCascade() { + ['2.0-SNAPSHOT', '[1.0,2.0)', '${unresolved}', '2.+', 'latest.release'].each { String version -> + def core = candidate('core', '1') + // The external edge has already been removed from namespace-filtered dependencies. + core.publishedDependencies = [[groupId: 'external', artifactId: 'library', version: version, scope: 'runtime', origin: 'pom.dependencies']] + def dependent = candidate('dependent', '1') + dependent.dependencies = [[groupId: 'g', artifactId: 'core', version: '1', origin: 'pom.dependencies']] + def unrelated = candidate('unrelated', '1') + List lookedUp = [] + assertEquals([unrelated], ReleasePlanner.select([dependent, core, unrelated], { coordinate -> + lookedUp.add(coordinate.key()) + false + })) + assertTrue(core.reasons.any { it.contains("external:library:${version}") }) + assertTrue(dependent.reasons.any { it.contains('Unavailable dependency') }) + assertFalse(lookedUp.any { it.startsWith('external:') }) + } + } + + @Test void externalReleaseAndBomManagedVersionsRemainEligibleAndTestScopesAreIgnored() { + def core = candidate('core', '1') + core.publishedDependencies = [ + [groupId: 'external', artifactId: 'release', version: '2.0', scope: 'compile'], + [groupId: 'external', artifactId: 'managed', version: '', scope: 'runtime'], + [groupId: 'external', artifactId: 'test', version: '2-SNAPSHOT', scope: 'test'], + [groupId: 'external', artifactId: 'system', version: '[1,2)', scope: 'system'] + ] + assertEquals([core], ReleasePlanner.select([core], { coordinate -> + assertEquals('g:core:1', coordinate.key()) + false + })) + } + @Test void releasedOldBomPinDoesNotBlockUnselectedSnapshot() { def arff = candidate('matrix-arff', '0.3.1-SNAPSHOT') def bom = candidate('matrix-bom', '2.7.0') diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy index d765d0f..79ba6f0 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedTasksTest.groovy @@ -34,7 +34,7 @@ class CombinedTasksTest { String packaging = name in ['matrix-bom', 'platform'] ? 'pom' : 'jar' String xml = """g${name}1${packaging}TestTesthttps://example.orgMIThttps://example.orgTesthttps://example.orgscm:git:example""" String testOnly = name == 'matrix-all' ? 'gtest-only1-SNAPSHOTtest' : '' - if (name == 'slim') xml = xml.replace('', 'externalprivate-library[1,2)') + if (name == 'slim') xml = xml.replace('', 'externalprivate-library1.5') xml = xml.replace('', testOnly + '') new File(root, name + '.xml').text = xml File staged = new File(root, "build/staging/g/${name}/1"); staged.mkdirs() From 1e4791d1206377a35b4ac28adc642554b92cbd19 Mon Sep 17 00:00:00 2001 From: Per Nyfelt Date: Sat, 10 Oct 2026 11:46:56 +0200 Subject: [PATCH 6/6] Validate managed dependency versions and document release policy --- combined-releases.md | 11 ++++++++- .../plugin/release/ReleasePlanner.groovy | 2 +- .../plugin/release/CombinedReleaseTest.groovy | 23 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/combined-releases.md b/combined-releases.md index f574638..14b5364 100644 --- a/combined-releases.md +++ b/combined-releases.md @@ -54,7 +54,16 @@ Eligibility edges include candidate group/artifact pairs and groups covered by t dependencies remain in POM expectations but are not availability probes; test/system edges are also excluded from eligibility. Candidate JSON requires a nonempty `files` list; omitted `dependencies` defaults to an empty list. -Invalid release dependency versions exclude the dependent candidate rather than aborting planning. +All explicit non-test/system versions in published dependencies and dependency management, +including third-party dependencies and imported BOMs, must be concrete release versions. +SNAPSHOTs, version ranges, dynamic versions and unresolved expressions exclude the candidate; +these exclusions cascade to dependents. Third-party availability is still not probed. +Rejecting third-party ranges and dynamic versions is a deliberate combined-release policy +change; use concrete pins even if the repository accepts ranges in published POMs. +Dependencies whose versions come from an imported BOM can remain unspecified in the source +POM, but their flattened published versions must be concrete non-SNAPSHOT versions. +Invalid eligibility-edge versions also exclude the dependent candidate rather than aborting +planning. Unresolved source POM expressions fail static inspection before selection. Only exact non-SNAPSHOT versions whose dependencies are released or in the final bundle are selected. Exclusions cascade; a selected module's mismatched BOM pin blocks the new BOM. An unselected module can retain an already-released BOM pin with a warning. diff --git a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy index 7d9bfe8..b671b14 100644 --- a/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy +++ b/src/main/groovy/se/alipsa/gradle/plugin/release/ReleasePlanner.groovy @@ -11,7 +11,7 @@ class ReleasePlanner { } candidates.each { PublicationDescriptor p -> // Published edges remain complete even when eligibility edges are filtered by namespace. - p.publishedDependencies.findAll { !(it.scope in ['test', 'system']) && it.version }.each { Map edge -> + (p.publishedDependencies + p.publishedManagement).findAll { !(it.scope in ['test', 'system']) && it.version }.each { Map edge -> def dependency = new ReleaseCoordinate(edge.groupId, edge.artifactId, edge.version) try { dependency.validate() } catch (IllegalArgumentException invalid) { diff --git a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy index f869a1f..3b2c0f2 100644 --- a/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy +++ b/src/test/groovy/se/alipsa/gradle/plugin/release/CombinedReleaseTest.groovy @@ -39,6 +39,24 @@ class CombinedReleaseTest { } } + @Test void externalManagementAndImportedBomVersionsAreValidatedAndCascade() { + ['compile', 'import'].each { String scope -> + ['2-SNAPSHOT', '[1,2)', '2.+', '${unresolved}'].each { String version -> + def bom = candidate('matrix-bom', '1') + bom.publishedManagement = [[groupId: 'external', artifactId: 'library', version: version, + scope: scope, type: scope == 'import' ? 'pom' : 'jar', origin: 'pom.dependencyManagement']] + def aggregate = candidate('matrix-all', '1') + aggregate.dependencies = [[groupId: 'g', artifactId: 'matrix-bom', version: '1', origin: 'pom.dependencies']] + assertTrue(ReleasePlanner.select([aggregate, bom], { coordinate -> + assertFalse(coordinate.groupId == 'external') + false + }).empty) + assertTrue(bom.reasons.any { it.contains("external:library:${version}") }) + assertTrue(aggregate.reasons.any { it.contains('Unavailable dependency') }) + } + } + } + @Test void externalReleaseAndBomManagedVersionsRemainEligibleAndTestScopesAreIgnored() { def core = candidate('core', '1') core.publishedDependencies = [ @@ -47,6 +65,11 @@ class CombinedReleaseTest { [groupId: 'external', artifactId: 'test', version: '2-SNAPSHOT', scope: 'test'], [groupId: 'external', artifactId: 'system', version: '[1,2)', scope: 'system'] ] + core.publishedManagement = [ + [groupId: 'external', artifactId: 'bom', version: '2.0', scope: 'import', type: 'pom'], + [groupId: 'external', artifactId: 'test', version: '2-SNAPSHOT', scope: 'test'], + [groupId: 'external', artifactId: 'system', version: '[1,2)', scope: 'system'] + ] assertEquals([core], ReleasePlanner.select([core], { coordinate -> assertEquals('g:core:1', coordinate.key()) false