From 813809ebeb035488f48930d0f078257d6c6a28ea Mon Sep 17 00:00:00 2001 From: daz Date: Wed, 3 Jan 2024 20:51:14 -0700 Subject: [PATCH] Merge into existing toolchains.xml --- .../integ-test-detect-java-toolchains.yml | 10 +++++++++- src/cache-base.ts | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.github/workflows/integ-test-detect-java-toolchains.yml b/.github/workflows/integ-test-detect-java-toolchains.yml index 2aad0071..7fd32e6c 100644 --- a/.github/workflows/integ-test-detect-java-toolchains.yml +++ b/.github/workflows/integ-test-detect-java-toolchains.yml @@ -79,9 +79,17 @@ jobs: run: | gradle --info javaToolchains > output.txt cat output.txt - - name: Verify detected toolchains + - name: Verify setup JDKs are detected shell: bash working-directory: .github/workflow-samples/groovy-dsl 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) + - name: Verify pre-installed toolchains are detected + shell: bash + working-directory: .github/workflow-samples/groovy-dsl + run: | + grep -q 'Eclipse Temurin JDK 1.8' output.txt || (echo "::error::Did not detect preinstalled JDK 1.8" && exit 1) + grep -q 'Eclipse Temurin JDK 11' output.txt || (echo "::error::Did not detect preinstalled JDK 11" && exit 1) + grep -q 'Eclipse Temurin JDK 17' output.txt || (echo "::error::Did not detect preinstalled JDK 17" && exit 1) + grep -q 'Eclipse Temurin JDK 21' output.txt || (echo "::error::Did not detect preinstalled JDK 21" && exit 1) diff --git a/src/cache-base.ts b/src/cache-base.ts index ce035642..467dde5b 100644 --- a/src/cache-base.ts +++ b/src/cache-base.ts @@ -204,15 +204,27 @@ export class GradleStateCache { fs.writeFileSync(initScriptPath, initScriptContent) } - // Copy the toolchain definitions file to `~/.m2/toolchains.xml` (if the file doesn't already exist) + // Copy the default toolchain definitions to `~/.m2/toolchains.xml` + const toolchainContent = this.readResourceFileAsString('toolchains.xml') const m2dir = path.resolve(this.userHome, '.m2') const toolchainXmlTarget = path.resolve(m2dir, 'toolchains.xml') if (!fs.existsSync(toolchainXmlTarget)) { + // Write a new toolchains.xml file if it doesn't exist fs.mkdirSync(m2dir, {recursive: true}) - const toolchainContent = this.readResourceFileAsString('toolchains.xml') fs.writeFileSync(toolchainXmlTarget, toolchainContent) core.info(`Wrote default JDK locations to ${toolchainXmlTarget}`) + } else { + // Merge into an existing toolchains.xml file + const existingToolchainContent = fs.readFileSync(toolchainXmlTarget, 'utf8') + const appendedContent = toolchainContent.split("").pop()! + const mergedContent = existingToolchainContent.replace('', appendedContent) + + core.info(`Merged toolchains.xml`) + core.info(mergedContent) + + fs.writeFileSync(toolchainXmlTarget, mergedContent) + core.info(`Merged default JDK locations into ${toolchainXmlTarget}`) } }