From 19462d2d939abaf9ea07f0a52d0a5697972aaed2 Mon Sep 17 00:00:00 2001 From: Thor Andreas Rognan Date: Sun, 28 Nov 2021 21:46:32 +0100 Subject: [PATCH] Decouple JDK running Gradle from JDK building project Gradle will by default use the same JDK for building the project as it does for running Gradle. Newer JDKs are assumed to be more performant, contain fewer bugs and to come with desirable features that we would like to utilize in our build, but doing so should not endanger backwards compatibility for the plugin itself. We can do that now by utilizing Gradle toolchains, which allows us to use one JDK/JRE for running Gradle and the other for building the project, i.e. executing `javac`. Doing this requires us to make multiple JDKs available to the Gradle build action for each individual execution of the build. The setup-java action doesn't support this at the moment, and the multiple build steps configured for actions/setup-java in this commit is at best a workaround for this, and at worst a bad assumption with respect to no environment variables or other settings being overwritten. Also, there's no handling of duplicate invocations of the action with the end result being duplicate JDKs installed. Hopefully both actions/setup-java and gradle/gradle-build-action will handle this case gracefully. For more details, see: - https://github.com/actions/setup-java/issues/44 - https://github.com/gradle/gradle/issues/14903 --- .github/workflows/main.yml | 37 ++++++++++++++++++++++++++++--------- plugin/build.gradle.kts | 13 +++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f35002c..d3495f0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,30 +10,49 @@ jobs: strategy: matrix: include: - - { os: 'macos-latest', arch: 'x64', java-version: '11', gradle-version: '7.3' } - - { os: 'ubuntu-latest', arch: 'x64', java-version: '11', gradle-version: '7.3' } - - { os: 'windows-latest', arch: 'x64', java-version: '11', gradle-version: '7.3' } - name: Gradle ${{ matrix.gradle-version }}, Java ${{ matrix.java-version }} on ${{ matrix.os }} (${{ matrix.arch }}) + # Gradle is run with `gradle-jdk` and builds with `build-jdk` by utilizing Gradle toolchain support + - { os: 'macos-latest', arch: 'x64', build-jdk: '11', gradle-jdk: '11', gradle-version: '7.3' } + - { os: 'ubuntu-latest', arch: 'x64', build-jdk: '11', gradle-jdk: '11', gradle-version: '7.3' } + - { os: 'windows-latest', arch: 'x64', build-jdk: '11', gradle-jdk: '11', gradle-version: '7.3' } + name: > + Gradle ${{ matrix.gradle-version }} on ${{ matrix.os }} (${{ matrix.arch }}) + w/JDK ${{ matrix.gradle-jdk }} building w/JDK ${{ matrix.build-jdk}} runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 - - name: Configure JDK ${{ matrix.java-version }} (${{ matrix.arch }}) + - name: Configure Gradle JDK ${{ matrix.gradle-jdk }} (${{ matrix.arch }}) + id: configure-gradle-jdk uses: actions/setup-java@v2 with: architecture: ${{ matrix.arch }} distribution: 'temurin' - java-version: ${{ matrix.java-version }} - - uses: gradle/gradle-build-action@v2 + java-version: ${{ matrix.gradle-jdk }} + - name: Configure Build JDK ${{ matrix.build-jdk }} (${{ matrix.arch }}) + id: configure-build-jdk + uses: actions/setup-java@v2 + with: + architecture: ${{ matrix.arch }} + distribution: 'temurin' + java-version: ${{ matrix.build-jdk }} + - name: > + Build w/Gradle ${{ matrix.gradle-version }} on JDK ${{ matrix.gradle-jdk }} + w/JDK ${{ matrix.build-jdk }} + uses: gradle/gradle-build-action@v2 with: # Only write to the cache on the 'main' branch cache-read-only: ${{ github.ref != 'refs/heads/main' }} gradle-version: ${{ matrix.gradle-version }} - arguments: build --scan + arguments: > + build --scan + -Porg.gradle.java.installations.auto-download=false + -Porg.gradle.java.installations.paths= + ${{ steps.configure-gradle-jdk.outputs.path }}, + ${{ steps.configure-build-jdk.outputs.path }} - name: Upload build reports uses: actions/upload-artifact@v2 if: always() with: - name: build-reports-${{ matrix.os }}-${{ matrix.arch }}-jdk-${{ matrix.java-version }} + name: build-reports-${{ matrix.os }}-${{ matrix.arch }}-jdk-${{ matrix.gradle-jdk }} path: | build/reports ./**/build/reports diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index f791b5f..7b043b8 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -11,6 +11,7 @@ plugins { id("org.jlleitschuh.gradle.ktlint") version "10.2.0" } +val jdkVersion: JavaLanguageVersion = JavaLanguageVersion.of(11) val functionalTestSourceSet: SourceSet = sourceSets.create("functionalTest") configurations["functionalTestImplementation"].extendsFrom(configurations["testImplementation"]) @@ -30,6 +31,18 @@ gradlePlugin { } } +java { + toolchain { + languageVersion.set(jdkVersion) + } +} + +kotlin { + jvmToolchain { + (this as JavaToolchainSpec).languageVersion.set(jdkVersion) + } +} + tasks.withType().configureEach { options.release.set(8) }