Switch Java installation to AdoptOpenJDK on Windows (#1067)

* Switch Java installation to AdoptOpenJDK

* Minor fix

* Setup env vars

* Reworked

* Add variable for java root path

* Validation improvement

* Minor fix

* Minor fix
This commit is contained in:
Vladimir Safonkin
2020-06-23 14:31:17 +00:00
committed by GitHub
parent 6f246a3b4f
commit 98df997b11
2 changed files with 65 additions and 21 deletions

View File

@@ -8,11 +8,21 @@ Import-Module -Name ImageHelpers -Force
function Set-JavaPath {
param (
[string] $Version,
[string] $JavaRootPath,
[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
if ($Version -eq 7) {
$matchedString = "azure-jdk_7"
} else {
$matchedString = "jdk-?$Version"
}
$javaPath = (Get-ChildItem -Path $JavaRootPath | Where-Object { $_ -match $matchedString}).FullName
if ([string]::IsNullOrEmpty($javaPath)) {
Write-Host "Not found path to Java $Version"
exit 1
}
Write-Host "Set JAVA_HOME_${Version}_X64 environmental variable as $javaPath"
setx JAVA_HOME_${Version}_X64 $javaPath /M
@@ -43,26 +53,49 @@ function Set-JavaPath {
}
}
# Download the Azul Systems Zulu JDKs
# See https://www.azul.com/downloads/azure-only/zulu/
$azulJDKURLs = @(
'https://repos.azul.com/azure-only/zulu/packages/zulu-7/7u232/zulu-7-azure-jdk_7.31.0.5-7.0.232-win_x64.zip',
'https://repos.azul.com/azure-only/zulu/packages/zulu-8/8u222/zulu-8-azure-jdk_8.40.0.25-8.0.222-win_x64.zip',
'https://repos.azul.com/azure-only/zulu/packages/zulu-11/11.0.4/zulu-11-azure-jdk_11.33.15-11.0.4-win_x64.zip',
'https://repos.azul.com/azure-only/zulu/packages/zulu-13/13.0.3/zulu-13-azure-jdk_13.31.11-13.0.3-win_x64.zip'
)
foreach ($azulJDKURL in $azulJDKURLs)
{
$archivePath = Start-DownloadWithRetry -Url $azulJDKURL -Name $([IO.Path]::GetFileName($azulJDKURL))
Extract-7Zip -Path $archivePath -DestinationPath "C:\Program Files\Java\"
function Install-Java7FromAzul {
param(
[string] $DestinationPath
)
$azulJDK7URL = 'https://repos.azul.com/azure-only/zulu/packages/zulu-7/7u232/zulu-7-azure-jdk_7.31.0.5-7.0.232-win_x64.zip'
$archivePath = Start-DownloadWithRetry -Url $azulJDK7URL -Name $([IO.Path]::GetFileName($azulJDK7URL))
Extract-7Zip -Path $archivePath -DestinationPath $DestinationPath
}
# Set PATH and env variables
Set-JavaPath -Version 7
Set-JavaPath -Version 8 -Default
Set-JavaPath -Version 11
Set-JavaPath -Version 13
function Install-JavaFromAdoptOpenJDK {
param(
[string] $JDKVersion,
[string] $DestinationPath
)
$assets = Invoke-RestMethod -Uri "https://api.adoptopenjdk.net/v3/assets/latest/$JDKVersion/hotspot"
$downloadUrl = ($assets | Where-Object {
$_.binary.os -eq "windows" `
-and $_.binary.architecture -eq "x64" `
-and $_.binary.image_type -eq "jdk"
}).binary.package.link
$archivePath = Start-DownloadWithRetry -Url $downloadUrl -Name $([IO.Path]::GetFileName($downloadUrl))
Extract-7Zip -Path $archivePath -DestinationPath $DestinationPath
}
$jdkVersions = @(7, 8, 11, 13)
$defaultVersion = 8
$javaRootPath = "C:\Program Files\Java\"
foreach ($jdkVersion in $jdkVersions) {
if ($jdkVersion -eq 7) {
Install-Java7FromAzul -DestinationPath $javaRootPath
} else {
Install-JavaFromAdoptOpenJDK -JDKVersion $jdkVersion -DestinationPath $javaRootPath
}
if ($jdkVersion -eq $defaultVersion) {
Set-JavaPath -Version $jdkVersion -JavaRootPath $javaRootPath -Default
} else {
Set-JavaPath -Version $jdkVersion -JavaRootPath $javaRootPath
}
}
# Install Java tools
# Force chocolatey to ignore dependencies on Ant and Maven or else they will download the Oracle JDK

View File

@@ -17,9 +17,20 @@ Function Validate-JavaVersion {
# 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;"
$javaPath = [System.Environment]::GetEnvironmentVariable("JAVA_HOME_${versionNumber}_X64")
if ([string]::IsNullOrEmpty($javaPath))
{
Write-Host "Environment variable 'JAVA_HOME_${versionNumber}_X64' is null"
exit 1
}
$javaBin = "$javaPath\bin;"
$env:Path = $javaBin + $env:Path
}
else
{
Validate-JavaVersion -Version $Version
}
$isJavaExists = $($(& $env:comspec "/s /c java -version 2>&1") | Out-String) -match '^(?<vendor>.+) version "(?<version>.+)".*'