[Windows] Move preinstalled java distributions to the toolcache directory (#2903)

* Move installation to toolcache folder

* Change Set-JavaPath function

* remove old java report function

* Change report function to output full semver

* small improvments

* nitpicks + remove java root path

* Adoptium -> Adopt
This commit is contained in:
Mikhail Timofeev
2021-03-16 17:16:16 +03:00
committed by GitHub
parent ef6e8c905f
commit 0cf21e4512
3 changed files with 40 additions and 49 deletions

View File

@@ -6,19 +6,19 @@
function Set-JavaPath { function Set-JavaPath {
param ( param (
[string] $Version, [string] $Version,
[string] $JavaRootPath, [string] $Architecture = "x64",
[switch] $Default [switch] $Default
) )
$matchedString = "jdk-?$Version" $javaPathPattern = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath "Java_Adopt_jdk/${Version}*/${Architecture}"
$javaPath = (Get-ChildItem -Path $JavaRootPath | Where-Object { $_ -match $matchedString}).FullName $javaPath = (Get-Item -Path $javaPathPattern).FullName
if ([string]::IsNullOrEmpty($javaPath)) { if ([string]::IsNullOrEmpty($javaPath)) {
Write-Host "Not found path to Java $Version" Write-Host "Not found path to Java '${Version}'"
exit 1 exit 1
} }
Write-Host "Set JAVA_HOME_${Version}_X64 environmental variable as $javaPath" Write-Host "Set 'JAVA_HOME_${Version}_X64' environmental variable as $javaPath"
setx JAVA_HOME_${Version}_X64 $javaPath /M setx JAVA_HOME_${Version}_X64 $javaPath /M
if ($Default) if ($Default)
@@ -50,31 +50,54 @@ function Set-JavaPath {
function Install-JavaFromAdoptOpenJDK { function Install-JavaFromAdoptOpenJDK {
param( param(
[string] $JDKVersion, [string] $JDKVersion,
[string] $DestinationPath [string] $Architecture = "x64"
) )
$assets = Invoke-RestMethod -Uri "https://api.adoptopenjdk.net/v3/assets/latest/$JDKVersion/hotspot" # Get Java version from adopt openjdk api
$downloadUrl = ($assets | Where-Object { $assetUrl = Invoke-RestMethod -Uri "https://api.adoptopenjdk.net/v3/assets/latest/${JDKVersion}/hotspot"
$asset = $assetUrl | Where-Object {
$_.binary.os -eq "windows" ` $_.binary.os -eq "windows" `
-and $_.binary.architecture -eq "x64" ` -and $_.binary.architecture -eq $Architecture `
-and $_.binary.image_type -eq "jdk" -and $_.binary.image_type -eq "jdk"
}).binary.package.link }
$downloadUrl = $asset.binary.package.link
$fullJavaVersion = $asset.version.semver
# Download and extract java binaries to temporary folder
$archivePath = Start-DownloadWithRetry -Url $downloadUrl -Name $([IO.Path]::GetFileName($downloadUrl)) $archivePath = Start-DownloadWithRetry -Url $downloadUrl -Name $([IO.Path]::GetFileName($downloadUrl))
Extract-7Zip -Path $archivePath -DestinationPath $DestinationPath $javaTempPath = Join-Path -Path $env:TEMP -ChildPath "Java_$fullJavaVersion"
Extract-7Zip -Path $archivePath -DestinationPath $javaTempPath
$javaTempBinariesPath = Join-Path -Path $javaTempPath -ChildPath "\jdk*\"
# Create directories in toolcache path
$javaToolcachePath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath "Java_Adopt_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"
New-Item -ItemType Directory -Path $javaToolcachePath | Out-Null
}
Write-Host "Creating Java '${fullJavaVersion}' folder in '${javaVersionPath}'"
New-Item -ItemType Directory -Path $javaVersionPath -Force | Out-Null
# Complete the installation by moving Java binaries from temporary directory to toolcache and creating the complete file
Move-Item -Path $javaTempBinariesPath -Destination $javaArchPath
New-Item -ItemType File -Path $javaVersionPath -Name "$Architecture.complete" | Out-Null
} }
$jdkVersions = (Get-ToolsetContent).java.versions $jdkVersions = (Get-ToolsetContent).java.versions
$defaultVersion = (Get-ToolsetContent).java.default $defaultVersion = (Get-ToolsetContent).java.default
$javaRootPath = "C:\Program Files\Java\"
foreach ($jdkVersion in $jdkVersions) { foreach ($jdkVersion in $jdkVersions) {
Install-JavaFromAdoptOpenJDK -JDKVersion $jdkVersion -DestinationPath $javaRootPath Install-JavaFromAdoptOpenJDK -JDKVersion $jdkVersion
if ($jdkVersion -eq $defaultVersion) { if ($jdkVersion -eq $defaultVersion) {
Set-JavaPath -Version $jdkVersion -JavaRootPath $javaRootPath -Default Set-JavaPath -Version $jdkVersion -Default
} else { } else {
Set-JavaPath -Version $jdkVersion -JavaRootPath $javaRootPath Set-JavaPath -Version $jdkVersion
} }
} }

View File

@@ -13,30 +13,6 @@ function Get-BashVersion {
return "Bash $version" return "Bash $version"
} }
function Get-JavaVersionsList {
param(
[string] $DefaultVersion
)
$postfix = ""
$javaDir = Join-Path $env:PROGRAMFILES "Java"
return Get-ChildItem $javaDir | ForEach-Object {
$javaBinPath = Join-Path $_ "bin"
$rawVersion = & cmd /c "`"$javaBinPath\java.exe`" -version 2>&1" | Out-String
$rawVersion -match 'openjdk version "(?<version>.+)"' | Out-Null
$version = $Matches.Version
if ($version -match $DefaultVersion) {
$postfix = "(default)"
} else {
$postfix = ""
}
return "Java $version $postfix"
} | Sort-Object {
$version = ($_.Split(" ")[1]).Split("_")[0]
return [System.Version]$version
}
}
function Get-RustVersion { function Get-RustVersion {
$rustVersion = [regex]::matches($(rustc --version), "\d+\.\d+\.\d+").Value $rustVersion = [regex]::matches($(rustc --version), "\d+\.\d+\.\d+").Value
return $rustVersion return $rustVersion

View File

@@ -1,12 +1,3 @@
function Get-JavaFullVersion {
param($JavaRootPath)
$javaBinPath = Join-Path "$javaRootPath" "/bin/java"
$javaVersionOutput = (Get-CommandResult "`"$javaBinPath`" -version").Output
$matchResult = $javaVersionOutput | Select-String '^openjdk version \"([\d\._]+)\"'
return $matchResult.Matches.Groups[1].Value
}
function Get-JavaVersions { function Get-JavaVersions {
$defaultJavaPath = $env:JAVA_HOME $defaultJavaPath = $env:JAVA_HOME
$javaVersions = Get-Item env:JAVA_HOME_*_X64 $javaVersions = Get-Item env:JAVA_HOME_*_X64
@@ -17,7 +8,8 @@ function Get-JavaVersions {
return $javaVersions | Sort-Object $sortRules | ForEach-Object { return $javaVersions | Sort-Object $sortRules | ForEach-Object {
$javaPath = $_.Value $javaPath = $_.Value
$version = Get-JavaFullVersion "$javaPath" # Take semver from the java path
$version = (Split-Path $javaPath) -replace "\w:\\.*\\"
$defaultPostfix = ($javaPath -eq $defaultJavaPath) ? " (default)" : "" $defaultPostfix = ($javaPath -eq $defaultJavaPath) ? " (default)" : ""
[PSCustomObject] @{ [PSCustomObject] @{