diff --git a/ci/ReadBundle.java b/ci/ReadBundle.java new file mode 100644 index 000000000..5cd918547 --- /dev/null +++ b/ci/ReadBundle.java @@ -0,0 +1,77 @@ +/* ********************************************************************* + * This Original Work is copyright of 51 Degrees Mobile Experts Limited. + * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House, + * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. + * + * This Original Work is licensed under the European Union Public Licence + * (EUPL) v.1.2 and is subject to its terms as set out below. + * + * If a copy of the EUPL was not distributed with this file, You can obtain + * one at https://opensource.org/licenses/EUPL-1.2. + * + * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be + * amended by the European Commission) shall be deemed incompatible for + * the purposes of the Work and the provisions of the compatibility + * clause in Article 5 of the EUPL shall not apply. + * + * If using the Work as, or as part of, a network application, by + * including the attribution notice(s) required under Article 5 of the EUPL + * in the end user terms of the application under an appropriate heading, + * such notice(s) shall fulfill the requirements of that article. + * ********************************************************************* */ + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; + +/** + * Reads a publishing bundle from start to end as a stream, the way the Central + * Portal reads it, and fails if any entry cannot be read. A reader that starts + * from the archive's central directory, as .NET's ZipFile and unzip do, reads + * a bundle the Portal rejects. ZipInputStream takes a record it does not + * expect as the end of the archive, so the entries it reads are counted + * against the central directory as well. + * + * Run with the JDK's source launcher: java ci/ReadBundle.java bundle.zip + */ +public class ReadBundle { + public static void main(String[] args) throws IOException { + int entries = 0; + long bytes = 0; + byte[] buffer = new byte[65536]; + try (InputStream file = new BufferedInputStream(new FileInputStream(args[0])); + ZipInputStream zip = new ZipInputStream(file)) { + ZipEntry entry; + while ((entry = zip.getNextEntry()) != null) { + int read; + while ((read = zip.read(buffer)) > 0) { + bytes += read; + } + entries++; + } + } catch (IOException e) { + System.err.println("The publishing bundle " + args[0] + + " cannot be read as a stream after " + entries + " entries: " + e); + System.exit(1); + } + int listed; + try (ZipFile zip = new ZipFile(args[0])) { + listed = zip.size(); + } + if (entries != listed) { + System.err.println("The publishing bundle " + args[0] + " lists " + + listed + " entries, but only " + entries + " read as a stream."); + System.exit(1); + } + if (entries == 0) { + System.err.println("The publishing bundle " + args[0] + " has no entries."); + System.exit(1); + } + System.out.println("Read the publishing bundle as a stream: " + entries + + " entries, " + bytes + " bytes."); + } +} diff --git a/ci/build-package.Tests.ps1 b/ci/build-package.Tests.ps1 index 9c03fbef1..3167bd6ca 100644 --- a/ci/build-package.Tests.ps1 +++ b/ci/build-package.Tests.ps1 @@ -42,11 +42,36 @@ Set-Content (Join-Path $PWD 'java-home.txt') $env:JAVA_HOME $global:LASTEXITCODE = 0 '@ - # The build ends by reading the publishing bundle common-ci's script made. - $entry = Join-Path $Root 'entry.txt' - Set-Content $entry 'entry' - Compress-Archive -Path $entry ` - -DestinationPath (Join-Path $Root 'package' 'central-bundle.zip') + # The publishing plugin writes its bundle below the repository, with each + # entry followed by a data descriptor as Java's ZipOutputStream writes it, + # and with a local repository file Central rejects. common-ci's script + # copies it into the package folder, here as an archive the Portal cannot + # read as a stream. + $Made = Join-Path $Root 'repo' 'pipeline' 'target' 'central-publishing' 'central-bundle.zip' + $null = New-Item -ItemType Directory -Force (Split-Path $Made) + Set-Content (Join-Path $Root 'MakeBundle.java') @' +import java.io.FileOutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; +public class MakeBundle { + public static void main(String[] args) throws Exception { + try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(args[0]))) { + for (String name : new String[] { + "com/51degrees/pipeline.core/4.5.131/pipeline.core-4.5.131.jar", + "com/51degrees/pipeline.core/maven-metadata-local.xml", + "com/51degrees/pipeline.core/maven-metadata-local.xml.sha1", + "com/51degrees/pipeline.core/4.5.131/pipeline.core-4.5.131.pom" }) { + zip.putNextEntry(new ZipEntry(name)); + zip.write(name.getBytes("UTF-8")); + zip.closeEntry(); + } + } + } +} +'@ + java (Join-Path $Root 'MakeBundle.java') $Made + $Copied = Join-Path $Root 'package' 'central-bundle.zip' + Set-Content $Copied 'not a zip' # Pester mocks only a command it can find, so the functions the build # takes from release.ps1 are defined here as well. @@ -100,6 +125,40 @@ Describe 'build-package.ps1' { $given.VersionPaths | Should -BeNullOrEmpty } + It 'makes the bundle again from the one the plugin wrote, without local repository files' { + Invoke-Build + $LASTEXITCODE | Should -Be 0 + $zip = [IO.Compression.ZipFile]::OpenRead($Copied) + try { + $names = @($zip.Entries.FullName) + } finally { + $zip.Dispose() + } + $names | Should -Be @( + 'com/51degrees/pipeline.core/4.5.131/pipeline.core-4.5.131.jar', + 'com/51degrees/pipeline.core/4.5.131/pipeline.core-4.5.131.pom') + } + + It 'gives a bundle that reads to its end as a stream' { + Invoke-Build + java (Join-Path $Root 'repo' 'ci' 'ReadBundle.java') $Copied + $LASTEXITCODE | Should -Be 0 + } + + It 'stops when the bundle cannot be read as a stream' { + # An archive opened in Update mode and disposed, as common-ci's script + # does, which under .NET 10 loses the last entry's data descriptor. + $broken = Join-Path $Root 'broken.zip' + Copy-Item $Made $broken + [IO.Compression.ZipFile]::Open($broken, 'Update').Dispose() + java (Join-Path $Root 'repo' 'ci' 'ReadBundle.java') $broken 2>$null + if ([Environment]::Version.Major -ge 10) { + $LASTEXITCODE | Should -Be 1 + } else { + Set-ItResult -Skipped -Because '.NET before 10 writes the archive whole' + } + } + It 'builds on JDK 17 and puts JAVA_HOME back' { $javaHome = $env:JAVA_HOME Invoke-Build diff --git a/ci/build-package.ps1 b/ci/build-package.ps1 index 7d5c660c6..9587ba5a7 100644 --- a/ci/build-package.ps1 +++ b/ci/build-package.ps1 @@ -8,6 +8,47 @@ $PSNativeCommandUseErrorActionPreference = $true . "$PSScriptRoot/release.ps1" +# Write a copy of the publishing bundle at $Source to $Destination, leaving out +# the files of the local repository that Central rejects (see common-ci's +# java/build-package.ps1), with each checksum and signature of them. The copy +# is a new archive, so every entry is written whole. +function Copy-PublishingBundle([string]$Source, [string]$Destination) { + $localRepoFiles = 'maven-metadata-local.xml', '_remote.repositories' + Remove-Item $Destination -ErrorAction SilentlyContinue + $from = [IO.Compression.ZipFile]::OpenRead($Source) + try { + $to = [IO.Compression.ZipFile]::Open($Destination, 'Create') + try { + foreach ($entry in $from.Entries) { + $parts = $entry.FullName -split '[\\/]' + $name = $parts[-1] + if ($parts -contains '.locks' -or + $localRepoFiles.Where({ $name -eq $_ -or $name.StartsWith("$_.") })) { + Write-Host "Leaving $($entry.FullName) out of the publishing bundle" + continue + } + $copy = $to.CreateEntry($entry.FullName, 'Optimal') + $copy.LastWriteTime = $entry.LastWriteTime + $in = $entry.Open() + try { + $out = $copy.Open() + try { + $in.CopyTo($out) + } finally { + $out.Dispose() + } + } finally { + $in.Dispose() + } + } + } finally { + $to.Dispose() + } + } finally { + $from.Dispose() + } +} + # Builds the packages this publish releases, each at its own version from the # release plan (see release.ps1). $Version is common-ci's label for the run and # is not used. @@ -53,6 +94,27 @@ Invoke-WithJavaHome (Get-CompileJavaHome) { } $exitCode = $LASTEXITCODE +# common-ci copies the publishing bundle into the package folder and removes the +# local repository files Maven 3.10 adds to it, by opening it in .NET's Update +# mode. Under .NET 10, which the PowerShell of the ubuntu-26.04 runner uses, +# disposing an archive opened that way writes it again without the data +# descriptor of its last entry, though the entry's flags still say one follows. +# The Central Portal reads the bundle as a stream, so it fails on that entry +# ("Unexpected record signature"). So the bundle is rebuilt here from the one +# the publishing plugin wrote, as a new archive without those files, and is +# then read to its end as a stream. +if ($exitCode -eq 0) { + $made = @(Get-ChildItem -Path $RepoName -Recurse -File -Filter central-bundle.zip | + Where-Object { $_.Directory.Name -eq 'central-publishing' }) + if ($made.Count -ne 1) { + throw "Expected one central-publishing/central-bundle.zip below '$RepoName' but found $($made.Count)" + } + Copy-PublishingBundle $made[0].FullName (Join-Path $PWD 'package/central-bundle.zip') + Invoke-WithJavaHome (Get-CompileJavaHome) { + java "$PSScriptRoot/ReadBundle.java" package/central-bundle.zip + } +} + # Maven Central limits each organisation's release size and file count in a # calendar month, so the log gives both for the bundle this publish uploads. $bundle = Get-Item package/central-bundle.zip