|
| 1 | +[CmdletBinding()] |
| 2 | +param( |
| 3 | + [ValidateSet('test', 'verify')] |
| 4 | + [string] $Goal = 'verify', |
| 5 | + |
| 6 | + [switch] $StrictDependencies |
| 7 | +) |
| 8 | + |
| 9 | +Set-StrictMode -Version Latest |
| 10 | +$ErrorActionPreference = 'Stop' |
| 11 | + |
| 12 | +$repoRoot = Split-Path -Parent $PSScriptRoot |
| 13 | +$pomPath = Join-Path $repoRoot 'pom.xml' |
| 14 | +$maven = Get-Command mvn -ErrorAction Stop |
| 15 | +$localRepository = if ($env:MAVEN_REPO_LOCAL) { |
| 16 | + $env:MAVEN_REPO_LOCAL |
| 17 | +} else { |
| 18 | + Join-Path $env:USERPROFILE '.m2\repository' |
| 19 | +} |
| 20 | + |
| 21 | +[xml] $pom = Get-Content -LiteralPath $pomPath -Raw |
| 22 | +$configuredJUnit = [string] $pom.project.properties.'junit.version' |
| 23 | +$configuredPlatform = [string] $pom.project.properties.'junit.platform.version' |
| 24 | +$configuredBom = Join-Path $localRepository ( |
| 25 | + 'org\junit\junit-bom\{0}\junit-bom-{0}.pom' -f $configuredJUnit |
| 26 | +) |
| 27 | + |
| 28 | +$mavenArguments = @( |
| 29 | + '--offline' |
| 30 | + '--batch-mode' |
| 31 | + '--no-transfer-progress' |
| 32 | + '--file' |
| 33 | + $pomPath |
| 34 | +) |
| 35 | + |
| 36 | +if (-not (Test-Path -LiteralPath $configuredBom)) { |
| 37 | + if ($StrictDependencies) { |
| 38 | + throw "The configured JUnit BOM $configuredJUnit is absent from the local Maven cache." |
| 39 | + } |
| 40 | + |
| 41 | + $configuredMajor = ([version] $configuredJUnit).Major |
| 42 | + $bomRoot = Join-Path $localRepository 'org\junit\junit-bom' |
| 43 | + $fallback = Get-ChildItem -LiteralPath $bomRoot -Directory -ErrorAction SilentlyContinue | |
| 44 | + Where-Object { |
| 45 | + $_.Name -match '^\d+\.\d+\.\d+$' -and |
| 46 | + ([version] $_.Name).Major -eq $configuredMajor -and |
| 47 | + (Test-Path -LiteralPath (Join-Path $_.FullName ("junit-bom-{0}.pom" -f $_.Name))) |
| 48 | + } | |
| 49 | + Sort-Object { [version] $_.Name } -Descending | |
| 50 | + Select-Object -First 1 |
| 51 | + |
| 52 | + if ($null -eq $fallback) { |
| 53 | + throw "No JUnit $configuredMajor.x BOM is available in the local Maven cache." |
| 54 | + } |
| 55 | + |
| 56 | + Write-Warning (( |
| 57 | + "JUnit BOM $configuredJUnit is not cached; offline validation uses cached {0}. " + |
| 58 | + "Run with -StrictDependencies to require the exact POM version." |
| 59 | + ) -f $fallback.Name) |
| 60 | + $mavenArguments += "-Djunit.version=$($fallback.Name)" |
| 61 | + |
| 62 | + if ($configuredPlatform -eq $configuredJUnit) { |
| 63 | + $mavenArguments += "-Djunit.platform.version=$($fallback.Name)" |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +Push-Location $repoRoot |
| 68 | +try { |
| 69 | + & $maven.Source @mavenArguments $Goal |
| 70 | + if ($LASTEXITCODE -ne 0) { |
| 71 | + throw "Offline Maven gate failed with exit code $LASTEXITCODE." |
| 72 | + } |
| 73 | +} finally { |
| 74 | + Pop-Location |
| 75 | +} |
0 commit comments