Skip to content

Commit

Permalink
[Windows] Move preinstalled java distributions to the toolcache direc…
Browse files Browse the repository at this point in the history
…tory (#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
  • Loading branch information
miketimofeev authored Mar 16, 2021
1 parent ef6e8c9 commit 0cf21e4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 49 deletions.
53 changes: 38 additions & 15 deletions images/win/scripts/Installers/Install-JavaTools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}

Expand Down
24 changes: 0 additions & 24 deletions images/win/scripts/SoftwareReport/SoftwareReport.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 "(?<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
Expand Down
12 changes: 2 additions & 10 deletions images/win/scripts/SoftwareReport/SoftwareReport.Java.psm1
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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] @{
Expand Down

0 comments on commit 0cf21e4

Please sign in to comment.