[Ubuntu] Move preinstalled Java distributions to the toolcache directory (#2915)

* move preinstalled java to toolcache

* rework installation and fix software report
This commit is contained in:
Dibir Magomedsaygitov
2021-03-27 10:51:50 +03:00
committed by GitHub
parent 60459a5b04
commit 48f3c3e9b7
2 changed files with 33 additions and 24 deletions

View File

@@ -1,29 +1,19 @@
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
$sortRules = @{
Expression = { [Int32]$_.Name.Split("_")[2] }
Descending = $false
}
$toolcachePath = Join-Path $env:AGENT_TOOLSDIRECTORY "Java_Adopt_jdk"
$javaToolcacheVersions = Get-ChildItem $toolcachePath -Name | Sort-Object -Descending
return $javaVersions | Sort-Object $sortRules | ForEach-Object {
$javaPath = $_.Value
$version = Get-JavaFullVersion $javaPath
$defaultPostfix = ($javaPath -eq $defaultJavaPath) ? " (default)" : ""
return $javaToolcacheVersions | ForEach-Object {
$majorVersion = $_.split(".")[0]
$fullVersion = $_.Replace("-", "+")
$defaultJavaPath = $env:JAVA_HOME
$javaPath = Get-Item env:JAVA_HOME_${majorVersion}_X64
$defaultPostfix = ($javaPath.Value -eq $defaultJavaPath) ? " (default)" : ""
[PSCustomObject] @{
"Version" = $version + $defaultPostfix
"Version" = $fullVersion + $defaultPostfix
"Vendor" = "Adopt OpenJDK"
"Environment Variable" = $_.Name
"Environment Variable" = $javaPath.Name
}
}
}

View File

@@ -9,6 +9,7 @@ source $HELPER_SCRIPTS/etc-environment.sh
JAVA_VERSIONS_LIST=$(get_toolset_value '.java.versions | .[]')
DEFAULT_JDK_VERSION=$(get_toolset_value '.java.default')
JAVA_TOOLCACHE_PATH="$AGENT_TOOLSDIRECTORY/Java_Adopt_jdk"
# Install GPG Key for Adopt Open JDK. See https://adoptopenjdk.net/installation.html
wget -qO - "https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public" | apt-key add -
@@ -19,19 +20,37 @@ if isUbuntu16 || isUbuntu18 ; then
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 0xB1998361219BD9C9
apt-add-repository "deb https://repos.azul.com/azure-only/zulu/apt stable main"
fi
apt-get update
for JAVA_VERSION in ${JAVA_VERSIONS_LIST[@]}; do
apt-get -y install adoptopenjdk-$JAVA_VERSION-hotspot=\*
echo "JAVA_HOME_${JAVA_VERSION}_X64=/usr/lib/jvm/adoptopenjdk-${JAVA_VERSION}-hotspot-amd64" | tee -a /etc/environment
javaVersionPath="/usr/lib/jvm/adoptopenjdk-${JAVA_VERSION}-hotspot-amd64"
echo "JAVA_HOME_${JAVA_VERSION}_X64=$javaVersionPath" | tee -a /etc/environment
fullJavaVersion=$(cat "$javaVersionPath/release" | grep "^SEMANTIC" | cut -d "=" -f 2 | tr -d "\"" | tr "+" "-")
# If there is no semver in java release, then extract java version from -fullversion
if [[ -z $fullJavaVersion ]]; then
fullJavaVersion=$(java -fullversion 2>&1 | tr -d "\"" | tr "+" "-" | awk '{print $4}')
fi
javaToolcacheVersionPath="$JAVA_TOOLCACHE_PATH/$fullJavaVersion"
mkdir -p "$javaToolcacheVersionPath"
# Create a complete file
touch "$javaToolcacheVersionPath/x64.complete"
# Create symlink for Java
ln -s $javaVersionPath "$javaToolcacheVersionPath/x64"
done
# Set Default Java version.
# Set Default Java version
if isUbuntu16; then
# issue: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=825987
# stackoverflow: https://askubuntu.com/questions/1187136/update-java-alternatives-only-java-but-not-javac-is-changed
sed -i 's/(hl|jre|jdk|plugin|DUMMY) /(hl|jre|jdk|jdkhl|plugin|DUMMY) /g' /usr/sbin/update-java-alternatives
fi
update-java-alternatives -s /usr/lib/jvm/adoptopenjdk-${DEFAULT_JDK_VERSION}-hotspot-amd64
echo "JAVA_HOME=/usr/lib/jvm/adoptopenjdk-${DEFAULT_JDK_VERSION}-hotspot-amd64" | tee -a /etc/environment