[Windows] Add adoptium to javatools installer (#3985)

This commit is contained in:
Mikhail Koliada
2021-09-10 12:26:11 +03:00
committed by GitHub
parent d1d592805b
commit 045e638562
6 changed files with 95 additions and 25 deletions

View File

@@ -7,10 +7,11 @@ function Set-JavaPath {
param (
[string] $Version,
[string] $Architecture = "x64",
[switch] $Default
[switch] $Default,
[string] $VendorName
)
$javaPathPattern = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath "Java_Adopt_jdk/${Version}*/${Architecture}"
$javaPathPattern = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath "Java_${VendorName}_jdk/${Version}*/${Architecture}"
$javaPath = (Get-Item -Path $javaPathPattern).FullName
if ([string]::IsNullOrEmpty($javaPath)) {
@@ -23,6 +24,7 @@ function Set-JavaPath {
if ($Default)
{
# Clean up any other Java folders from PATH to make sure that they won't conflict with each other
$currentPath = Get-MachinePath
$pathSegments = $currentPath.Split(';')
@@ -47,14 +49,21 @@ function Set-JavaPath {
}
}
function Install-JavaFromAdoptOpenJDK {
function Install-JavaJDK {
param(
[string] $JDKVersion,
[string] $Architecture = "x64"
[string] $Architecture = "x64",
[string] $VendorName
)
# Get Java version from adopt openjdk api
# Get Java version from api
if ($VendorName -eq "Temurin-Hotspot") {
$assetUrl = Invoke-RestMethod -Uri "https://api.adoptium.net/v3/assets/latest/${JDKVersion}/hotspot"
} elseif ($VendorName -eq "Adopt") {
$assetUrl = Invoke-RestMethod -Uri "https://api.adoptopenjdk.net/v3/assets/latest/${JDKVersion}/hotspot"
} else {
throw "$VendorName is invalid vendor name. 'Adopt' and 'Temurin-Hotspot' are allowed values"
}
$asset = $assetUrl | Where-Object {
$_.binary.os -eq "windows" `
-and $_.binary.architecture -eq $Architecture `
@@ -68,13 +77,13 @@ function Install-JavaFromAdoptOpenJDK {
# We have to replace '+' sign in the version to '-' due to the issue with incorrect path in Android builds https://github.com/actions/virtual-environments/issues/3014
$fullJavaVersion = $asset.version.semver -replace '\+', '-'
# Create directories in toolcache path
$javaToolcachePath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath "Java_Adopt_jdk"
$javaToolcachePath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath "Java_${VendorName}_jdk"
$javaVersionPath = Join-Path -Path $javaToolcachePath -ChildPath $fullJavaVersion
$javaArchPath = Join-Path -Path $javaVersionPath -ChildPath $Architecture
if (-not (Test-Path $javaToolcachePath))
{
Write-Host "Creating Adopt openjdk toolcache folder"
Write-Host "Creating ${VendorName} toolcache folder"
New-Item -ItemType Directory -Path $javaToolcachePath | Out-Null
}
@@ -87,18 +96,37 @@ function Install-JavaFromAdoptOpenJDK {
New-Item -ItemType File -Path $javaVersionPath -Name "$Architecture.complete" | Out-Null
}
$jdkVersions = (Get-ToolsetContent).java.versions
$defaultVersion = (Get-ToolsetContent).java.default
$toolsetJava = (Get-ToolsetContent).java
$jdkVendors = $toolsetJava.vendors
$defaultVendor = $toolsetJava.default_vendor
$defaultVersion = $toolsetJava.default
foreach ($jdkVersion in $jdkVersions) {
Install-JavaFromAdoptOpenJDK -JDKVersion $jdkVersion
foreach ($jdkVendor in $jdkVendors) {
$jdkVendorName = $jdkVendor.name
$jdkVersionsToInstall = $jdkVendor.versions
if ($jdkVersion -eq $defaultVersion) {
Set-JavaPath -Version $jdkVersion -Default
$isDefaultVendor = $jdkVendorName -eq $defaultVendor
foreach ($jdkVersionToInstall in $jdkVersionsToInstall) {
$isDefaultVersion = $jdkVersionToInstall -eq $defaultVersion
Install-JavaJDK -VendorName $jdkVendorName -JDKVersion $jdkVersionToInstall
if ($isDefaultVendor) {
if ($isDefaultVersion) {
Set-JavaPath -Version $jdkVersionToInstall -VendorName $jdkVendorName -Default
} else {
Set-JavaPath -Version $jdkVersion
Set-JavaPath -Version $jdkVersionToInstall -VendorName $jdkVendorName
}
}
}
}
# Setup JAVA_HOME_13_X64 as this is the only Adopt version needed
# There is no jdk 13 on Windows 2022
if (-not (Test-IsWin22)) {
Set-JavaPath -Version "13" -VendorName "Adopt"
}
# Install Java tools
# Force chocolatey to ignore dependencies on Ant and Maven or else they will download the Oracle JDK

View File

@@ -13,10 +13,11 @@ function Get-JavaVersions {
$versionInPath = (Split-Path $javaPath) -replace "\w:\\.*\\"
$version = $versionInPath -replace '-', '+'
$defaultPostfix = ($javaPath -eq $defaultJavaPath) ? " (default)" : ""
$VendorName = ($javaPath -like '*Java_Adopt_jdk*') ? "Adopt OpenJDK" : "Eclipse Temurin"
[PSCustomObject] @{
"Version" = $version + $defaultPostfix
"Vendor" = "Adopt OpenJDK"
"Vendor" = $VendorName
"Environment Variable" = $_.Name
}
}

View File

@@ -1,6 +1,11 @@
Describe "Java" {
[array]$jdkVersions = (Get-ToolsetContent).java.versions | ForEach-Object { @{Version = $_} }
$defaultVersion = (Get-ToolsetContent).java.default
$toolsetJava = (Get-ToolsetContent).java
$defaultVendor = $toolsetJava.default_vendor
$javaVendors = $toolsetJava.vendors
$defaultVersion = $toolsetJava.default
[array]$jdkVersions = ($javaVendors | Where-Object {$_.name -eq $defaultVendor}).versions | ForEach-Object { @{Version = $_} }
[array]$adoptJdkVersions = ($javaVendors | Where-Object {$_.name -eq "Adopt"}).versions | ForEach-Object { @{Version = $_} }
It "Java <DefaultJavaVersion> is default" -TestCases @(@{ DefaultJavaVersion = $defaultVersion }) {
$actualJavaPath = Get-EnvironmentVariable "JAVA_HOME"
@@ -33,4 +38,16 @@ Describe "Java" {
}
$result.Output[0] | Should -Match ([regex]::Escape("openjdk version `"${Version}."))
}
It "Java Adopt Jdk <Version>" -TestCases $adoptJdkVersions {
$adoptPath = Join-Path (Get-ChildItem ${env:AGENT_TOOLSDIRECTORY}\Java_Adopt_jdk\${Version}*) "x64\bin\java"
$result = Get-CommandResult "`"$adoptPath`" -version"
$result.ExitCode | Should -Be 0
if ($Version -eq 8) {
$Version = "1.${Version}"
}
$result.Output[0] | Should -Match ([regex]::Escape("openjdk version `"${Version}."))
}
}

View File

@@ -142,8 +142,16 @@
],
"java": {
"default": "8",
"versions": [
"8", "11", "13"
"default_vendor": "Temurin-Hotspot",
"vendors": [
{
"name": "Temurin-Hotspot",
"versions": [ "8", "11" ]
},
{
"name": "Adopt",
"versions": [ "8", "11", "13" ]
}
]
},
"android": {

View File

@@ -142,8 +142,16 @@
],
"java": {
"default": "8",
"versions": [
"8", "11", "13"
"default_vendor": "Temurin-Hotspot",
"vendors": [
{
"name": "Temurin-Hotspot",
"versions": [ "8", "11" ]
},
{
"name": "Adopt",
"versions": [ "8", "11", "13" ]
}
]
},
"android": {

View File

@@ -106,8 +106,16 @@
],
"java": {
"default": "8",
"versions": [
"8", "11"
"default_vendor": "Temurin-Hotspot",
"vendors": [
{
"name": "Temurin-Hotspot",
"versions": [ "8", "11" ]
},
{
"name": "Adopt",
"versions": [ "8", "11"]
}
]
},
"android": {