From fa73edb9bf724b339e4ea63f8e64802b496fd465 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Thu, 16 May 2019 12:41:50 -0700 Subject: [PATCH] Improve detection of JDK on Windows (#10294) --- .azure/pipelines/ci.yml | 1 - .azure/pipelines/jobs/default-build.yml | 2 +- build.ps1 | 38 +++++++++++++++++++++++++ docs/BuildFromSource.md | 8 ++++-- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/.azure/pipelines/ci.yml b/.azure/pipelines/ci.yml index 5fab3f3a16..7a582b243b 100644 --- a/.azure/pipelines/ci.yml +++ b/.azure/pipelines/ci.yml @@ -23,7 +23,6 @@ jobs: jobName: Code_check jobDisplayName: Code check agentOs: Windows - installJdk: false steps: - powershell: ./eng/scripts/CodeCheck.ps1 -ci displayName: Run eng/scripts/CodeCheck.ps1 diff --git a/.azure/pipelines/jobs/default-build.yml b/.azure/pipelines/jobs/default-build.yml index 34f5cc2da6..05e6719b5c 100644 --- a/.azure/pipelines/jobs/default-build.yml +++ b/.azure/pipelines/jobs/default-build.yml @@ -120,7 +120,7 @@ jobs: BuildDirectory: ${{ parameters.buildDirectory }} DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true TeamName: AspNetCore - ${{ if eq(parameters.agentOs, 'Windows') }}: + ${{ if and(eq(parameters.installJdk, 'true'), eq(parameters.agentOs, 'Windows')) }}: JAVA_HOME: $(Agent.BuildDirectory)\.tools\jdk ${{ if or(ne(parameters.codeSign, true), ne(variables['System.TeamProject'], 'internal')) }}: _SignType: '' diff --git a/build.ps1 b/build.ps1 index 86fe4a6c5f..2cf4e51853 100644 --- a/build.ps1 +++ b/build.ps1 @@ -303,6 +303,44 @@ $MSBuildArguments += "/p:_RunSign=$Sign" $MSBuildArguments += "/p:TargetArchitecture=$Architecture" $MSBuildArguments += "/p:TargetOsName=win" +if (($All -or $BuildJava) -and -not $NoBuildJava) { + $foundJdk = $false + $javac = Get-Command javac -ErrorAction Ignore -CommandType Application + if ($env:JAVA_HOME) { + if (-not (Test-Path "${env:JAVA_HOME}\bin\javac.exe")) { + Write-Error "The environment variable JAVA_HOME was set, but ${env:JAVA_HOME}\bin\javac.exe does not exist. Remove JAVA_HOME or update it to the correct location for the JDK. See https://www.bing.com/search?q=java_home for details." + } + else { + Write-Host -f Magenta "Detected JDK in ${env:JAVA_HOME} (via JAVA_HOME)" + $foundJdk = $true + } + } + elseif ($javac) { + $foundJdk = $true + $javaHome = Split-Path -Parent (Split-Path -Parent $javac.Path) + $env:JAVA_HOME = $javaHome + Write-Host -f Magenta "Detected JDK in $javaHome (via PATH)" + } + else { + try { + $jdkVersion = (Get-Item HKLM:\SOFTWARE\JavaSoft\JDK | Get-ItemProperty -name CurrentVersion).CurrentVersion + $javaHome = (Get-Item HKLM:\SOFTWARE\JavaSoft\JDK\$jdkVersion | Get-ItemProperty -Name JavaHome).JavaHome + if (Test-Path "${env:JAVA_HOME}\bin\java.exe") { + $env:JAVA_HOME = $javaHome + Write-Host -f Magenta "Detected JDK $jdkVersion in $env:JAVA_HOME (via registry)" + $foundJdk = $true + } + } + catch { + Write-Verbose "Failed to detect Java: $_" + } + } + + if (-not $foundJdk) { + Write-Error "Could not find the JDK. See $PSScriptRoot\docs\BuildFromSource.md for details on this requirement." + } +} + Import-Module -Force -Scope Local (Join-Path $korebuildPath 'KoreBuild.psd1') try { diff --git a/docs/BuildFromSource.md b/docs/BuildFromSource.md index 0839d8d9c3..16ab546892 100644 --- a/docs/BuildFromSource.md +++ b/docs/BuildFromSource.md @@ -20,7 +20,9 @@ Building ASP.NET Core on Windows requires: ``` * Git. * NodeJS. LTS version of 10.14.2 or newer -* Java Development Kit (JDK) v8 with Java Runtime Environment (JRE) v8. See https://www.oracle.com/technetwork/java/javase/downloads/index.html +* Java Development Kit 11 or newer. Either: + * OpenJDK + * Oracle's JDK ### macOS/Linux @@ -31,8 +33,8 @@ Building ASP.NET Core on macOS or Linux requires: * At least 10 GB of disk space and a good internet connection (our build scripts download a lot of tools and dependencies) * Git * NodeJS. LTS version of 10.14.2 or newer -* Java Development Kit 10 or newer. Either: - * OpenJDK +* Java Development Kit 11 or newer. Either: + * OpenJDK * Oracle's JDK ## Clone the source code