From a07019c726caf5df41d7d9c8a31635c455c01d2c Mon Sep 17 00:00:00 2001 From: daz Date: Sat, 19 Aug 2023 18:11:04 -0600 Subject: [PATCH] Inform Gradle where to locate pre-installed JDKs --- .../integ-test-detect-java-toolchains.yml | 24 +++++++++++++++++++ src/cache-base.ts | 16 +++++++++++++ 2 files changed, 40 insertions(+) diff --git a/.github/workflows/integ-test-detect-java-toolchains.yml b/.github/workflows/integ-test-detect-java-toolchains.yml index 34e31f3..9e4f171 100644 --- a/.github/workflows/integ-test-detect-java-toolchains.yml +++ b/.github/workflows/integ-test-detect-java-toolchains.yml @@ -80,3 +80,27 @@ jobs: run: | grep -q 'Eclipse Temurin JDK 16' output.txt || (echo "::error::Did not detect setup-java installed JDK 16" && exit 1) grep -q 'Eclipse Temurin JDK 20' output.txt || (echo "::error::Did not detect setup-java installed JDK 20" && exit 1) + + # Test that predefined JDK detection property is not overwritten by action + check-no-overwrite: + strategy: + matrix: + os: ${{fromJSON(inputs.runner-os)}} + runs-on: ${{ matrix.os }} + steps: + - name: Checkout sources + uses: actions/checkout@v3 + - name: Download distribution if required + uses: ./.github/actions/download-dist + - name: Configure java installations env var in Gradle User Home + shell: bash + run: | + mkdir -p ~/.gradle + echo "org.gradle.java.installations.fromEnv=XXXXX" > ~/.gradle/gradle.properties + - name: Setup Gradle + uses: ./ + - name: Check gradle.properties + shell: bash + run: | + cat ~/.gradle/gradle.properties + grep -q 'org.gradle.java.installations.fromEnv=JAVA_HOME' && exit 1 diff --git a/src/cache-base.ts b/src/cache-base.ts index 9fa816b..d4ac46b 100644 --- a/src/cache-base.ts +++ b/src/cache-base.ts @@ -172,6 +172,22 @@ export class GradleStateCache { } private initializeGradleUserHome(gradleUserHome: string, initScriptsDir: string): void { + // Ensure that pre-installed java versions are detected. Only add property if it isn't already defined. + const gradleProperties = path.resolve(gradleUserHome, 'gradle.properties') + const existingGradleProperties = fs.existsSync(gradleProperties) + ? fs.readFileSync(gradleProperties, 'utf8') + : '' + if (!existingGradleProperties.includes('org.gradle.java.installations.fromEnv=')) { + fs.appendFileSync( + gradleProperties, + ` +# Auto-detect pre-installed JDKs +org.gradle.java.installations.fromEnv=JAVA_HOME_8_X64,JAVA_HOME_11_X64,JAVA_HOME_17_X64 +` + ) + } + + // Copy init scripts from src/resources const initScriptFilenames = [ 'build-result-capture.init.gradle', 'build-result-capture-service.plugin.groovy',