2019-12-13 09:48:00 -05:00
################################################################################
## File: Install-JavaTools.ps1
## Desc: Install various JDKs and java tools
################################################################################
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 " ,
2021-09-10 12:26:11 +03:00
[ switch ] $Default ,
[ string ] $VendorName
2020-05-16 12:31:21 +03:00
)
2021-09-10 12:26:11 +03:00
$javaPathPattern = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath " Java_ ${VendorName} _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 "
2020-05-16 12:31:21 +03:00
setx JAVA_HOME_ $ { Version } _X64 $javaPath / M
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
2020-05-16 12:31:21 +03:00
$currentPath = Get-MachinePath
$pathSegments = $currentPath . Split ( ';' )
$newPathSegments = @ ( )
foreach ( $pathSegment in $pathSegments )
{
if ( $pathSegment -notlike '*java*' )
{
$newPathSegments + = $pathSegment
}
}
$newPath = [ string ] :: Join ( ';' , $newPathSegments )
$newPath = $javaPath + '\bin;' + $newPath
Write-Host " Add $javaPath \bin to PATH "
Set-MachinePath -NewPath $newPath
Write-Host " Set JAVA_HOME environmental variable as $javaPath "
setx JAVA_HOME $javaPath / M
}
}
2021-09-10 12:26:11 +03:00
function Install-JavaJDK {
2020-06-23 14:31:17 +00:00
param (
[ string ] $JDKVersion ,
2021-09-10 12:26:11 +03:00
[ string ] $Architecture = " x64 " ,
[ string ] $VendorName
2020-06-23 14:31:17 +00:00
)
2021-09-10 12:26:11 +03:00
# 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 "
}
2021-03-16 17:16:16 +03:00
$asset = $assetUrl | Where-Object {
2020-06-23 14:31:17 +00:00
$_ . binary . os -eq " windows " `
2021-03-16 17:16:16 +03:00
-and $_ . binary . architecture -eq $Architecture `
2020-06-23 14:31:17 +00:00
-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
2020-06-23 14:31:17 +00:00
$archivePath = Start-DownloadWithRetry -Url $downloadUrl -Name $ ( [ IO.Path ] :: GetFileName ( $downloadUrl ) )
2021-03-16 17:16:16 +03: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 '\+' , '-'
2021-03-16 17:16:16 +03:00
# Create directories in toolcache path
2021-09-10 12:26:11 +03:00
$javaToolcachePath = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath " Java_ ${VendorName} _jdk "
2021-03-16 17:16:16 +03:00
$javaVersionPath = Join-Path -Path $javaToolcachePath -ChildPath $fullJavaVersion
$javaArchPath = Join-Path -Path $javaVersionPath -ChildPath $Architecture
if ( -not ( Test-Path $javaToolcachePath ) )
{
2021-09-10 12:26:11 +03:00
Write-Host " Creating ${VendorName} 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
2021-06-18 10:26:34 +03:00
Extract - 7Zip -Path $archivePath -DestinationPath $javaVersionPath
2021-11-26 17:49:05 +03:00
Invoke-SBWithRetry -Command {
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
$jdkVendors = $toolsetJava . vendors
$defaultVendor = $toolsetJava . default_vendor
$defaultVersion = $toolsetJava . default
2020-06-23 14:31:17 +00:00
2021-09-10 12:26:11 +03:00
foreach ( $jdkVendor in $jdkVendors ) {
$jdkVendorName = $jdkVendor . name
$jdkVersionsToInstall = $jdkVendor . versions
2023-03-05 11:41:43 +01:00
2021-09-10 12:26:11 +03:00
$isDefaultVendor = $jdkVendorName -eq $defaultVendor
2020-06-23 14:31:17 +00:00
2021-09-10 12:26:11 +03:00
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 $jdkVersionToInstall -VendorName $jdkVendorName
}
}
2020-06-23 14:31:17 +00:00
}
}
2019-12-13 09:48:00 -05:00
2021-09-10 12:26:11 +03:00
# 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 "
}
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
2020-04-17 10:53:30 +03:00
Choco-Install -PackageName ant -ArgumentList " -i "
2023-03-05 11:41:43 +01:00
# Maven 3.9.x has multiple compatibilities problems
$toolsetMavenVersion = ( Get-ToolsetContent ) . maven . version
$versionToInstall = Get-LatestChocoPackageVersion -TargetVersion $toolsetMavenVersion -PackageName " maven "
Choco-Install -PackageName maven -ArgumentList " --version= $versionToInstall "
2020-04-17 10:53:30 +03:00
Choco-Install -PackageName gradle
2019-12-13 09:48:00 -05:00
2021-10-25 16:30:47 +03:00
# Add maven env variables to Machine
[ string ] $m2 = ( Get-MachinePath ) . Split ( " ; " ) -match " maven "
2019-12-13 09:48:00 -05:00
$maven_opts = '-Xms256m'
$m2_repo = 'C:\ProgramData\m2'
2021-10-25 16:30:47 +03:00
New-Item -Path $m2_repo -ItemType Directory -Force | Out-Null
2019-12-13 09:48:00 -05:00
setx M2 $m2 / M
setx M2_REPO $m2_repo / M
setx MAVEN_OPTS $maven_opts / M
# 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'
2019-12-13 09:48:00 -05:00
$coberturaPath = " C:\cobertura-2.1.1 "
2020-05-06 07:20:11 +03:00
$archivePath = Start-DownloadWithRetry -Url $uri -Name " cobertura.zip "
2020-05-13 08:02:08 +03:00
Extract - 7Zip -Path $archivePath -DestinationPath " C:\ "
2019-12-13 09:48:00 -05:00
setx COBERTURA_HOME $coberturaPath / M
2020-07-09 10:53:29 +03:00
2021-02-16 13:08:50 +05:00
Invoke-PesterTests -TestFile " Java "