Browse Source

Improve detection of JDK on Windows (#10294)

Nate McMaster 6 years ago
parent
commit
fa73edb9bf
4 changed files with 44 additions and 5 deletions
  1. 0 1
      .azure/pipelines/ci.yml
  2. 1 1
      .azure/pipelines/jobs/default-build.yml
  3. 38 0
      build.ps1
  4. 5 3
      docs/BuildFromSource.md

+ 0 - 1
.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

+ 1 - 1
.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: ''

+ 38 - 0
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 {

+ 5 - 3
docs/BuildFromSource.md

@@ -20,7 +20,9 @@ Building ASP.NET Core on Windows requires:
         ```
 * Git. <https://git-scm.org>
 * NodeJS. LTS version of 10.14.2 or newer <https://nodejs.org>
-* 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 <https://jdk.java.net/>
+    * Oracle's JDK <https://www.oracle.com/technetwork/java/javase/downloads/index.html>
 
 ### 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 <https://git-scm.org>
 * NodeJS. LTS version of 10.14.2 or newer <https://nodejs.org>
-* Java Development Kit 10 or newer. Either:
-    * OpenJDK <http://jdk.java.net/10/>
+* Java Development Kit 11 or newer. Either:
+    * OpenJDK <https://jdk.java.net/>
     * Oracle's JDK <https://www.oracle.com/technetwork/java/javase/downloads/index.html>
 
 ## Clone the source code