Skip to content

Instantly share code, notes, and snippets.

@schtritoff
Created July 20, 2023 22:56
Show Gist options
  • Save schtritoff/60a468d30707801510fd5c36d851550f to your computer and use it in GitHub Desktop.
Save schtritoff/60a468d30707801510fd5c36d851550f to your computer and use it in GitHub Desktop.
Apache Maven - unattended Windows installation (non-admin, no UAC, in user apps folder)
<#
.SYNOPSIS
Install Apache Maven as user installation (no admin rights requred)
.EXAMPLE
PS C:\> .\Install-MavenUserInstall.ps1
CMD C:\> powershell.exe -ExecutionPolicy ByPass -File Install-MavenUserInstall.ps1
.NOTES
References:
- https://gist.github.com/FlorianLatapie/3c3125a2a371dc08991313401f04db65
- https://maven.apache.org/install.html
#>
# first get which version is the latest one
Write-Host "Checking for latest version ..."
$baseUrl = 'https://dlcdn.apache.org/maven/maven-3/'
$mavenDownloadHtml = $(Invoke-WebRequest -Uri $baseUrl -UseBasicParsing).Content
#$mavenVersion = $mavenDownloadHtml -match '(\d+\.\d+\.\d+)'
#$matches
$mavenVersions = [regex]::Matches($mavenDownloadHtml, '([3-4]\.\d+\.\d+)')
$mavenVersionLatest = $($mavenVersions | Select-Object -Last 1).Value
Write-Host "Latest version: $mavenVersionLatest"
# is it installed already?
$installedPath = "$env:userprofile\AppData\Local\Programs\Apache\apache-maven-$mavenVersionLatest"
If (Test-Path "$installedPath") {
Write-Warning "Already installed: $installedPath"
Exit 1
}
# download new one
Invoke-WebRequest "$baseUrl$mavenVersionLatest/binaries/apache-maven-$mavenVersionLatest-bin.zip" -OutFile "$env:temp\temp.zip" -UseBasicParsing
# remove all previous ones
Remove-Item "$env:userprofile\AppData\Local\Programs\Apache\apache-maven-*" -Recurse -Force
# extract new one
Expand-Archive "$env:temp\temp.zip" "$env:userprofile\AppData\Local\Programs\Apache"
Remove-Item "$env:temp\temp.zip" -Force
# add to path
Write-Host "Adding to path ..."
# setx is the safest choice - see https://github.com/PowerShell/PowerShell/issues/18126
Write-Host "$env:PATH;$installedPath\bin"
setx.exe PATH "$env:PATH;$installedPath\bin"
Write-Host "Please close all cmd windows for the PATH to refresh"
# TODO - older version is still in PATH :-( - should use some util like "pathed.exe /SLIM" from http://www.p-nand-q.com/download/gtools/pathed.html
# done
Write-Host "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment