<# Windows PowerShell script to install Maven 3.6.x+ (binary) on Windows. This script downloads the binary, extracts it, sets MAVEN_HOME and PATH for the user, and verifies mvn -version. ASCII-only messages to minimize encoding issues. Usage: powershell -ExecutionPolicy Bypass -File scripts/install_maven.ps1 -Version 3.6.3 powershell -ExecutionPolicy Bypass -File scripts/install_maven.ps1 -Version latest Notes: - Version must be a specific 3.6.x (currently supports 3.6.3). - InstallDir defaults to C:\Program Files\Maven. - ApiKey is accepted for future use; not used by Maven itself. """ #> param( [ValidateSet("latest","3.6.3")] [string]$Version = "3.6.3", [string]$InstallDir = "C:\\Program Files\\Maven", [string]$ApiKey = $null ) $ErrorActionPreference = "Stop" Write-Host "Installing Maven..." if (-Not (Test-Path $InstallDir)) { try { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } catch { $InstallDir = Join-Path $env:USERPROFILE "maven" if (-Not (Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } } } if ($Version -eq "latest") { Write-Error "Latest version not auto-resolved in this script. Please specify 3.6.3." exit 1 } $zipName = "apache-maven-$Version-bin.zip" $url = "https://archive.apache.org/dist/maven/maven-3/$Version/binaries/$zipName" $tempZip = Join-Path $env:TEMP $zipName $destDir = Join-Path $InstallDir "apache-maven-$Version" if (Test-Path $destDir) { Write-Host "Maven already installed at $destDir"; exit 0 } Write-Host "Downloading Maven $Version..." $maxRetries = 3 $attempt = 0 while ($attempt -lt $maxRetries) { try { Invoke-WebRequest -Uri $url -OutFile $tempZip -ErrorAction Stop break } catch { $attempt++ if ($attempt -ge $maxRetries) { Write-Error "Failed to download Maven after $maxRetries attempts. Please check network and proxy settings." exit 1 } Write-Warning "Download failed. Retrying ($attempt/$maxRetries)..." Start-Sleep -Seconds 5 } } Write-Host "Extracting to $InstallDir..." Expand-Archive -Path $tempZip -DestinationPath $InstallDir -Force $mavenHome = (Join-Path $InstallDir "apache-maven-$Version").TrimEnd('\\') if (-Not (Test-Path $mavenHome)) { Write-Error "Extraction failed: Maven directory not found: $mavenHome" exit 1 } # Set MAVEN_HOME and PATH (user scope) Write-Host "Configuring environment variables..." [Environment]::SetEnvironmentVariable("MAVEN_HOME", $mavenHome, [EnvironmentVariableTarget]::User) $binPath = Join-Path $mavenHome "bin" $sep = [System.IO.Path]::PathSeparator $pathValue = [Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::User) if (-not ($pathValue -and $pathValue -like "*$binPath*")) { if (-not $pathValue) { $newPath = $binPath } else { $newPath = $pathValue + $sep + $binPath } [Environment]::SetEnvironmentVariable("PATH", $newPath, [EnvironmentVariableTarget]::User) Write-Host "PATH updated to include $binPath" } else { Write-Host "PATH already includes Maven bin: $binPath" } Write-Host "Verifying mvn version..." try { $mvnVer = & "$mavenHome\\bin\\mvn" -version Write-Host $mvnVer } catch { Write-Error "Could not run mvn. Ensure PATH is set correctly and reopen a new terminal." exit 1 } Write-Host "Maven installation complete." if (-not [string]::IsNullOrEmpty($ApiKey)) { Write-Host "Note: API key provided, but not used by Maven." } Exit 0