diff --git a/images/win/scripts/Installers/Install-JavaTools.ps1 b/images/win/scripts/Installers/Install-JavaTools.ps1 index 34e9fb886..68f325e8d 100644 --- a/images/win/scripts/Installers/Install-JavaTools.ps1 +++ b/images/win/scripts/Installers/Install-JavaTools.ps1 @@ -5,6 +5,44 @@ Import-Module -Name ImageHelpers -Force +function Set-JavaPath { + param ( + [string] $Version, + [switch] $Default + ) + + $filter = "*azure-jdk_${version}.*" + $javaPath = (Get-ChildItem -Path 'C:\Program Files\Java' -Filter $filter | Sort-Object -Property Name -Descending | Select-Object -First 1).FullName + + Write-Host "Set JAVA_HOME_${Version}_X64 environmental variable as $javaPath" + setx JAVA_HOME_${Version}_X64 $javaPath /M + + if ($Default) + { + $currentPath = Get-MachinePath + + $pathSegments = $currentPath.Split(';') + $newPathSegments = @() + + foreach ($pathSegment in $pathSegments) + { + if ($pathSegment -notlike '*java*') + { + $newPathSegments += $pathSegment + } + } + + $newPath = [string]::Join(';', $newPathSegments) + $newPath = $javaPath + '\bin;' + $newPath + + Write-Host "Add $javaPath\bin to PATH" + Set-MachinePath -NewPath $newPath + + Write-Host "Set JAVA_HOME environmental variable as $javaPath" + setx JAVA_HOME $javaPath /M + } +} + # Download the Azul Systems Zulu JDKs # See https://www.azul.com/downloads/azure-only/zulu/ $azulJDKURLs = @( @@ -17,44 +55,14 @@ $azulJDKURLs = @( foreach ($azulJDKURL in $azulJDKURLs) { $archivePath = Start-DownloadWithRetry -Url $azulJDKURL -Name $([IO.Path]::GetFileName($azulJDKURL)) - Expand-Archive -Path $archivePath -DestinationPath "C:\Program Files\Java\" + Extract-7Zip -Path $archivePath -DestinationPath "C:\Program Files\Java\" } -$currentPath = Get-MachinePath - -$pathSegments = $currentPath.Split(';') -$newPathSegments = @() - -foreach ($pathSegment in $pathSegments) -{ - if ($pathSegment -notlike '*java*') - { - $newPathSegments += $pathSegment - } -} - -$java7Installs = Get-ChildItem -Path 'C:\Program Files\Java' -Filter '*azure-jdk*7*' | Sort-Object -Property Name -Descending | Select-Object -First 1 -$latestJava7Install = $java7Installs.FullName; - -$java8Installs = Get-ChildItem -Path 'C:\Program Files\Java' -Filter '*azure-jdk*8*' | Sort-Object -Property Name -Descending | Select-Object -First 1 -$latestJava8Install = $java8Installs.FullName; - -$java11Installs = Get-ChildItem -Path 'C:\Program Files\Java' -Filter '*azure-jdk*11*' | Sort-Object -Property Name -Descending | Select-Object -First 1 -$latestJava11Install = $java11Installs.FullName; - -$java13Installs = Get-ChildItem -Path 'C:\Program Files\Java' -Filter '*azure-jdk*13*' | Sort-Object -Property Name -Descending | Select-Object -First 1 -$latestJava13Install = $java13Installs.FullName; - -$newPath = [string]::Join(';', $newPathSegments) -$newPath = $latestJava8Install + '\bin;' + $newPath - -Set-MachinePath -NewPath $newPath - -setx JAVA_HOME $latestJava8Install /M -setx JAVA_HOME_7_X64 $latestJava7Install /M -setx JAVA_HOME_8_X64 $latestJava8Install /M -setx JAVA_HOME_11_X64 $latestJava11Install /M -setx JAVA_HOME_13_X64 $latestJava13Install /M +# Set PATH and env variables +Set-JavaPath -Version 7 +Set-JavaPath -Version 8 -Default +Set-JavaPath -Version 11 +Set-JavaPath -Version 13 # Install Java tools # Force chocolatey to ignore dependencies on Ant and Maven or else they will download the Oracle JDK diff --git a/images/win/scripts/Installers/Validate-JavaTools.ps1 b/images/win/scripts/Installers/Validate-JavaTools.ps1 index 33f906949..7530bf89c 100644 --- a/images/win/scripts/Installers/Validate-JavaTools.ps1 +++ b/images/win/scripts/Installers/Validate-JavaTools.ps1 @@ -3,6 +3,52 @@ ## Desc: Validate various JDKs and java tools ################################################################################ +Function Validate-JavaVersion { + param ( + [Parameter(Mandatory)] [string] $Version, + [switch] $Default + ) + + Write-Host "Checking Java $version" + + # Set Path to get Java + if (-not $Default) + { + # Take 7 & 8 for versions 1.7 and 1.8 + $versionNumber = $version.Split(".") | Select-Object -Last 1 + + $javaBin = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${versionNumber}_X64") + "\bin;" + $env:Path = $javaBin + $env:Path + } + + $isJavaExists = $($(& $env:comspec "/s /c java -version 2>&1") | Out-String) -match '^(?.+) version "(?.+)".*' + + if ($isJavaExists) + { + $javaVersion = $matches.version + } + else + { + Write-Host "Java $version was not found" + exit 1 + } + + $isJavaCorrect = $javaVersion.StartsWith($version) + + if($isJavaCorrect) + { + Write-Host "Java $javaVersion found" + # Reset Path to the default one in case we need to check the default Java later + $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") + return $javaVersion + } + else + { + Write-Host "Expected Java $version, but found $javaVersion" + exit 1 + } +} + if((Get-Command -Name 'java') -and (Get-Command -Name 'mvn') -and (Get-Command -Name 'ant') -and (Get-Command -Name 'gradle')) { Write-Host "Java $(java -version) on path" @@ -16,32 +62,12 @@ else exit 1 } +Write-Host "Checking installed Java versions" -if( $( $(& $env:comspec "/s /c java -version 2>&1") | Out-String) -match '^(?.+) version "(?.+)".*' ) -{ - $javaVersion = $Matches.version -} - -$env:Path = $env:JAVA_HOME_7_X64 + "\bin;" + $env:Path - -if( $( $(& $env:comspec "/s /c java -version 2>&1") | Out-String) -match '^(?.+) version "(?.+)".*' ) -{ - $java7Version = $Matches.version -} - -$env:Path = $env:JAVA_HOME_11_X64 + "\bin;" + $env:Path - -if( $( $(& $env:comspec "/s /c java -version 2>&1") | Out-String) -match '^(?.+) version "(?.+)".*' ) -{ - $java11Version = $Matches.version -} - -$env:Path = $env:JAVA_HOME_13_X64 + "\bin;" + $env:Path - -if( $( $(& $env:comspec "/s /c java -version 2>&1") | Out-String) -match '^(?.+) version "(?.+)".*' ) -{ - $java13Version = $Matches.version -} +$java7Version = Validate-JavaVersion -Version "1.7" +$java8Version = Validate-JavaVersion -Version "1.8" -Default +$java11Version = Validate-JavaVersion -Version "11" +$java13Version = Validate-JavaVersion -Version "13" if( $(ant -version) -match 'Apache Ant\(TM\) version (?.*) compiled.*' ) { @@ -62,7 +88,7 @@ if( $( $(gradle -version) | Out-String) -match 'Gradle (?.*)' ) $SoftwareName = "Java Development Kit" $Description = @" -#### $javaVersion (default) +#### $java8Version (default) _Environment:_ * JAVA_HOME: location of JDK