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 " ,
2020-05-16 12:31:21 +03:00
[ switch ] $Default
)
2021-03-16 17:16:16 +03:00
$javaPathPattern = Join-Path -Path $env:AGENT_TOOLSDIRECTORY -ChildPath " Java_Adopt_jdk/ ${Version} */ ${Architecture} "
$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 )
{
$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
}
}
2020-06-23 14:31:17 +00:00
function Install-JavaFromAdoptOpenJDK {
param (
[ string ] $JDKVersion ,
2021-03-16 17:16:16 +03:00
[ string ] $Architecture = " x64 "
2020-06-23 14:31:17 +00:00
)
2021-03-16 17:16:16 +03:00
# Get Java version from adopt openjdk api
$assetUrl = Invoke-RestMethod -Uri " https://api.adoptopenjdk.net/v3/assets/latest/ ${JDKVersion} /hotspot "
$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
}
$downloadUrl = $asset . binary . package . link
2021-03-27 10:50:50 +03:00
# We have to replace '+' sign in the version to '-' due to the issue with incorrect path in Android builds https://github.com/actions/virtual-environments/issues/3014
$fullJavaVersion = $asset . version . semver -replace '\+' , '-'
2020-06-23 14:31:17 +00:00
2021-03-16 17:16:16 +03:00
# Download and extract java binaries to temporary folder
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
$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
2020-05-06 07:20:11 +03:00
}
2019-12-13 09:48:00 -05:00
2021-01-13 10:48:09 +03:00
$jdkVersions = ( Get-ToolsetContent ) . java . versions
$defaultVersion = ( Get-ToolsetContent ) . java . default
2020-06-23 14:31:17 +00:00
foreach ( $jdkVersion in $jdkVersions ) {
2021-03-16 17:16:16 +03:00
Install-JavaFromAdoptOpenJDK -JDKVersion $jdkVersion
2020-06-23 14:31:17 +00:00
if ( $jdkVersion -eq $defaultVersion ) {
2021-03-16 17:16:16 +03:00
Set-JavaPath -Version $jdkVersion -Default
2020-06-23 14:31:17 +00:00
} else {
2021-03-16 17:16:16 +03:00
Set-JavaPath -Version $jdkVersion
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
2020-04-17 10:53:30 +03:00
Choco-Install -PackageName ant -ArgumentList " -i "
2021-04-26 13:48:47 +02:00
Choco-Install -PackageName maven -ArgumentList " -i " , " --version=3.8.1 "
2020-04-17 10:53:30 +03:00
Choco-Install -PackageName gradle
2019-12-13 09:48:00 -05:00
# Move maven variables to Machine. They may not be in the environment for this script so we need to read them from the registry.
2020-05-06 13:15:34 +03:00
$userEnvironmentKey = 'Registry::HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
2019-12-13 09:48:00 -05:00
$m2_home = ( Get-ItemProperty -Path $userEnvironmentKey -Name M2_HOME ) . M2_HOME
$m2 = $m2_home + '\bin'
$maven_opts = '-Xms256m'
$m2_repo = 'C:\ProgramData\m2'
New-Item -Path $m2_repo -ItemType Directory -Force
setx M2 $m2 / M
setx M2_REPO $m2_repo / M
setx MAVEN_OPTS $maven_opts / M
# Download cobertura jars
2020-09-11 08:58:54 +03:00
$uri = 'https://downloads.sourceforge.net/project/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 "