From 0cf21e451224281a74a004ae22fd456b5bad0f2b Mon Sep 17 00:00:00 2001 From: Mikhail Timofeev <48208649+miketimofeev@users.noreply.github.com> Date: Tue, 16 Mar 2021 17:16:16 +0300 Subject: [PATCH] [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 --- .../scripts/Installers/Install-JavaTools.ps1 | 53 +++++++++++++------ .../SoftwareReport/SoftwareReport.Common.psm1 | 24 --------- .../SoftwareReport/SoftwareReport.Java.psm1 | 12 +---- 3 files changed, 40 insertions(+), 49 deletions(-) diff --git a/images/win/scripts/Installers/Install-JavaTools.ps1 b/images/win/scripts/Installers/Install-JavaTools.ps1 index 2179008a0..c571d34fc 100644 --- a/images/win/scripts/Installers/Install-JavaTools.ps1 +++ b/images/win/scripts/Installers/Install-JavaTools.ps1 @@ -6,19 +6,19 @@ function Set-JavaPath { param ( [string] $Version, - [string] $JavaRootPath, + [string] $Architecture = "x64", [switch] $Default ) - $matchedString = "jdk-?$Version" - $javaPath = (Get-ChildItem -Path $JavaRootPath | Where-Object { $_ -match $matchedString}).FullName + $javaPathPattern = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath "Java_Adopt_jdk/${Version}*/${Architecture}" + $javaPath = (Get-Item -Path $javaPathPattern).FullName if ([string]::IsNullOrEmpty($javaPath)) { - Write-Host "Not found path to Java $Version" + Write-Host "Not found path to Java '${Version}'" 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 if ($Default) @@ -50,31 +50,54 @@ function Set-JavaPath { function Install-JavaFromAdoptOpenJDK { param( [string] $JDKVersion, - [string] $DestinationPath + [string] $Architecture = "x64" ) - $assets = Invoke-RestMethod -Uri "https://api.adoptopenjdk.net/v3/assets/latest/$JDKVersion/hotspot" - $downloadUrl = ($assets | Where-Object { + # Get Java version from adopt openjdk api + $assetUrl = Invoke-RestMethod -Uri "https://api.adoptopenjdk.net/v3/assets/latest/${JDKVersion}/hotspot" + $asset = $assetUrl | Where-Object { $_.binary.os -eq "windows" ` - -and $_.binary.architecture -eq "x64" ` + -and $_.binary.architecture -eq $Architecture ` -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)) - 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 $defaultVersion = (Get-ToolsetContent).java.default -$javaRootPath = "C:\Program Files\Java\" foreach ($jdkVersion in $jdkVersions) { - Install-JavaFromAdoptOpenJDK -JDKVersion $jdkVersion -DestinationPath $javaRootPath + Install-JavaFromAdoptOpenJDK -JDKVersion $jdkVersion if ($jdkVersion -eq $defaultVersion) { - Set-JavaPath -Version $jdkVersion -JavaRootPath $javaRootPath -Default + Set-JavaPath -Version $jdkVersion -Default } else { - Set-JavaPath -Version $jdkVersion -JavaRootPath $javaRootPath + Set-JavaPath -Version $jdkVersion } } diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 b/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 index 71f315c02..910507169 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1 @@ -13,30 +13,6 @@ function Get-BashVersion { 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 "(?.+)"' | 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 { $rustVersion = [regex]::matches($(rustc --version), "\d+\.\d+\.\d+").Value return $rustVersion diff --git a/images/win/scripts/SoftwareReport/SoftwareReport.Java.psm1 b/images/win/scripts/SoftwareReport/SoftwareReport.Java.psm1 index c319c5bea..5998b33ef 100644 --- a/images/win/scripts/SoftwareReport/SoftwareReport.Java.psm1 +++ b/images/win/scripts/SoftwareReport/SoftwareReport.Java.psm1 @@ -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 { $defaultJavaPath = $env:JAVA_HOME $javaVersions = Get-Item env:JAVA_HOME_*_X64 @@ -17,7 +8,8 @@ function Get-JavaVersions { return $javaVersions | Sort-Object $sortRules | ForEach-Object { $javaPath = $_.Value - $version = Get-JavaFullVersion "$javaPath" + # Take semver from the java path + $version = (Split-Path $javaPath) -replace "\w:\\.*\\" $defaultPostfix = ($javaPath -eq $defaultJavaPath) ? " (default)" : "" [PSCustomObject] @{