2019-12-13 09:48:00 -05:00
################################################################################
## File: Install-JavaTools.ps1
## Desc: Install various JDKs and java tools
2023-10-04 14:59:44 +02:00
## Supply chain security: JDK - checksum validation
2019-12-13 09:48:00 -05:00
################################################################################
2020-05-16 12:31:21 +03:00
function Set-JavaPath {
param (
[ string ] $Version ,
2021-03-16 17:16:16 +03:00
[ string ] $Architecture = " x64 " ,
2023-08-03 10:32:48 +02:00
[ switch ] $Default
2020-05-16 12:31:21 +03:00
)
2023-08-03 10:32:48 +02:00
$javaPathPattern = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath " Java_Temurin-Hotspot_jdk/ ${Version} */ ${Architecture} "
2021-03-16 17:16:16 +03:00
$javaPath = ( Get-Item -Path $javaPathPattern ) . FullName
2020-06-23 14:31:17 +00:00
if ( [ string ] :: IsNullOrEmpty ( $javaPath ) ) {
2021-03-16 17:16:16 +03:00
Write-Host " Not found path to Java ' ${Version} ' "
2020-06-23 14:31:17 +00:00
exit 1
}
2020-05-16 12:31:21 +03:00
2021-03-16 17:16:16 +03:00
Write-Host " Set 'JAVA_HOME_ ${Version} _X64' environmental variable as $javaPath "
2023-11-17 13:52:52 +01:00
[ Environment ] :: SetEnvironmentVariable ( " JAVA_HOME_ ${Version} _X64 " , $javaPath , " Machine " )
2020-05-16 12:31:21 +03:00
2023-08-03 10:32:48 +02:00
if ( $Default ) {
2021-09-10 12:26:11 +03:00
# Clean up any other Java folders from PATH to make sure that they won't conflict with each other
2023-11-29 13:00:16 +01:00
$currentPath = [ System.Environment ] :: GetEnvironmentVariable ( " PATH " , " Machine " )
2020-05-16 12:31:21 +03:00
$pathSegments = $currentPath . Split ( ';' )
$newPathSegments = @ ( )
2023-08-03 10:32:48 +02:00
foreach ( $pathSegment in $pathSegments ) {
if ( $pathSegment -notlike '*java*' ) {
2020-05-16 12:31:21 +03:00
$newPathSegments + = $pathSegment
}
}
$newPath = [ string ] :: Join ( ';' , $newPathSegments )
$newPath = $javaPath + '\bin;' + $newPath
Write-Host " Add $javaPath \bin to PATH "
2023-12-04 10:50:53 +01:00
[ Environment ] :: SetEnvironmentVariable ( " PATH " , $newPath , " Machine " )
2020-05-16 12:31:21 +03:00
Write-Host " Set JAVA_HOME environmental variable as $javaPath "
2023-11-17 13:52:52 +01:00
[ Environment ] :: SetEnvironmentVariable ( " JAVA_HOME " , $javaPath , " Machine " )
2020-05-16 12:31:21 +03:00
}
}
2021-09-10 12:26:11 +03:00
function Install-JavaJDK {
2020-06-23 14:31:17 +00:00
param (
[ string ] $JDKVersion ,
2023-08-03 10:32:48 +02:00
[ string ] $Architecture = " x64 "
2020-06-23 14:31:17 +00:00
)
2021-09-10 12:26:11 +03:00
# Get Java version from api
2023-08-03 10:32:48 +02:00
$assetUrl = Invoke-RestMethod -Uri " https://api.adoptium.net/v3/assets/latest/ ${JDKVersion} /hotspot "
2021-03-16 17:16:16 +03:00
$asset = $assetUrl | Where-Object {
2020-06-23 14:31:17 +00:00
$_ . binary . os -eq " windows " `
2023-08-03 10:32:48 +02:00
-and $_ . binary . architecture -eq $Architecture `
-and $_ . binary . image_type -eq " jdk "
2021-03-16 17:16:16 +03:00
}
2020-06-23 14:31:17 +00:00
2021-03-16 17:16:16 +03:00
# Download and extract java binaries to temporary folder
2021-06-18 10:26:34 +03:00
$downloadUrl = $asset . binary . package . link
2023-11-29 13:02:29 +01:00
$archivePath = Invoke-DownloadWithRetry $downloadUrl
2023-11-30 09:22:14 +01:00
Test-FileChecksum $archivePath -ExpectedSHA256Sum $asset . binary . package . checksum
2023-10-04 14:59:44 +02:00
2022-08-10 13:55:34 +01:00
# We have to replace '+' sign in the version to '-' due to the issue with incorrect path in Android builds https://github.com/actions/runner-images/issues/3014
2021-06-18 10:26:34 +03:00
$fullJavaVersion = $asset . version . semver -replace '\+' , '-'
2023-10-19 11:38:41 +02:00
# Remove 'LTS' suffix from the version if present
$fullJavaVersion = $fullJavaVersion -replace '\.LTS$' , ''
2021-03-16 17:16:16 +03:00
# Create directories in toolcache path
2023-08-03 10:32:48 +02:00
$javaToolcachePath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath " Java_Temurin-Hotspot_jdk "
2021-03-16 17:16:16 +03:00
$javaVersionPath = Join-Path -Path $javaToolcachePath -ChildPath $fullJavaVersion
$javaArchPath = Join-Path -Path $javaVersionPath -ChildPath $Architecture
2023-08-03 10:32:48 +02:00
if ( -not ( Test-Path $javaToolcachePath ) ) {
Write-Host " Creating Temurin-Hotspot toolcache folder "
2021-03-16 17:16:16 +03:00
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
2022-01-13 03:53:31 -03:00
# Complete the installation by extracting Java binaries to toolcache and creating the complete file
2023-11-23 11:59:49 +01:00
Expand - 7ZipArchive -Path $archivePath -DestinationPath $javaVersionPath
2023-11-29 12:58:37 +01:00
Invoke-ScriptBlockWithRetry -Command {
2021-11-26 17:49:05 +03:00
Get-ChildItem -Path $javaVersionPath | Rename-Item -NewName $javaArchPath -ErrorAction Stop
}
2021-03-16 17:16:16 +03:00
New-Item -ItemType File -Path $javaVersionPath -Name " $Architecture .complete " | Out-Null
2020-05-06 07:20:11 +03:00
}
2019-12-13 09:48:00 -05:00
2021-09-10 12:26:11 +03:00
$toolsetJava = ( Get-ToolsetContent ) . java
$defaultVersion = $toolsetJava . default
2023-08-03 10:32:48 +02:00
$jdkVersionsToInstall = $toolsetJava . versions
2020-06-23 14:31:17 +00:00
2023-08-03 10:32:48 +02:00
foreach ( $jdkVersionToInstall in $jdkVersionsToInstall ) {
$isDefaultVersion = $jdkVersionToInstall -eq $defaultVersion
2021-09-10 12:26:11 +03:00
2023-08-03 10:32:48 +02:00
Install-JavaJDK -JDKVersion $jdkVersionToInstall
2021-09-10 12:26:11 +03:00
2023-08-03 10:32:48 +02:00
if ( $isDefaultVersion ) {
Set-JavaPath -Version $jdkVersionToInstall -Default
} else {
Set-JavaPath -Version $jdkVersionToInstall
2020-06-23 14:31:17 +00:00
}
}
2019-12-13 09:48:00 -05:00
# Install Java tools
# Force chocolatey to ignore dependencies on Ant and Maven or else they will download the Oracle JDK
2023-11-22 15:14:08 +01:00
Install-ChocoPackage ant -ArgumentList " --ignore-dependencies "
2023-03-05 11:41:43 +01:00
# Maven 3.9.x has multiple compatibilities problems
$toolsetMavenVersion = ( Get-ToolsetContent ) . maven . version
2023-11-22 15:14:08 +01:00
$versionToInstall = Resolve-ChocoPackageVersion -PackageName " maven " -TargetVersion $toolsetMavenVersion
2023-03-05 11:41:43 +01:00
2023-11-22 15:14:08 +01:00
Install-ChocoPackage maven -ArgumentList " --version= $versionToInstall "
Install-ChocoPackage gradle
2019-12-13 09:48:00 -05:00
2021-10-25 16:30:47 +03:00
# Add maven env variables to Machine
2023-12-04 10:50:53 +01:00
[ string ] $m2Path = ( [ Environment ] :: GetEnvironmentVariable ( " PATH " , " Machine " ) ) . Split ( " ; " ) -match " maven "
2019-12-13 09:48:00 -05:00
2023-12-04 10:50:53 +01:00
$m2RepoPath = 'C:\ProgramData\m2'
New-Item -Path $m2RepoPath -ItemType Directory -Force | Out-Null
2019-12-13 09:48:00 -05:00
2023-12-04 10:50:53 +01:00
[ Environment ] :: SetEnvironmentVariable ( " M2 " , $m2Path , " Machine " )
[ Environment ] :: SetEnvironmentVariable ( " M2_REPO " , $m2RepoPath , " Machine " )
2023-11-29 13:00:16 +01:00
[ Environment ] :: SetEnvironmentVariable ( " MAVEN_OPTS " , " -Xms256m " , " Machine " )
2019-12-13 09:48:00 -05:00
# Download cobertura jars
2022-05-09 12:43:19 +02:00
$uri = 'https://repo1.maven.org/maven2/net/sourceforge/cobertura/cobertura/2.1.1/cobertura-2.1.1-bin.zip'
2023-10-04 17:20:54 +02:00
$sha256sum = '79479DDE416B082F38ECD1F2F7C6DEBD4D0C2249AF80FD046D1CE05D628F2EC6'
2019-12-13 09:48:00 -05:00
$coberturaPath = " C:\cobertura-2.1.1 "
2023-11-29 13:02:29 +01:00
$archivePath = Invoke-DownloadWithRetry $uri
2023-11-30 09:22:14 +01:00
Test-FileChecksum $archivePath -ExpectedSHA256Sum $sha256sum
2023-11-23 11:59:49 +01:00
Expand - 7ZipArchive -Path $archivePath -DestinationPath " C:\ "
2019-12-13 09:48:00 -05:00
2023-11-17 13:52:52 +01:00
[ Environment ] :: SetEnvironmentVariable ( " COBERTURA_HOME " , $coberturaPath , " Machine " )
2020-07-09 10:53:29 +03:00
2021-02-16 13:08:50 +05:00
Invoke-PesterTests -TestFile " Java "