From 20d40b18b9957caccb31e829e17e4c977e08b589 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Mon, 13 Nov 2023 17:23:10 +0100 Subject: [PATCH] Use dynamically resolved Java version when creating Gradle projects Also add tests for Java 21. Fixes #37047 --- .../base/build-layout.include.qute | 9 +---- .../scala/build.tpl.qute.gradle.kts | 9 +---- .../gradle/base/build-layout.include.qute | 9 +---- .../gradle/scala/build.tpl.qute.gradle | 9 +---- .../quarkus/maven/it/CreateProjectMojoIT.java | 37 +++++++++++++++++++ 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle-kotlin-dsl/base/build-layout.include.qute b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle-kotlin-dsl/base/build-layout.include.qute index 3b643510d09a4..4b0d7655a7a45 100644 --- a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle-kotlin-dsl/base/build-layout.include.qute +++ b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle-kotlin-dsl/base/build-layout.include.qute @@ -50,13 +50,8 @@ version = "{project.version}" {#insert java} java { - {#if java.version == "17"} - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - {#else} - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 - {/if} + sourceCompatibility = JavaVersion.VERSION_{java.version} + targetCompatibility = JavaVersion.VERSION_{java.version} } {/} diff --git a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle-kotlin-dsl/scala/build.tpl.qute.gradle.kts b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle-kotlin-dsl/scala/build.tpl.qute.gradle.kts index a6c4baa92a1a3..035088350b60f 100644 --- a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle-kotlin-dsl/scala/build.tpl.qute.gradle.kts +++ b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle-kotlin-dsl/scala/build.tpl.qute.gradle.kts @@ -9,11 +9,6 @@ plugins { tasks.withType { scalaCompileOptions.encoding = "UTF-8" - {#if java.version == "11"} - sourceCompatibility = JavaVersion.VERSION_11.toString() - targetCompatibility = JavaVersion.VERSION_11.toString() - {#else} - sourceCompatibility = JavaVersion.VERSION_1_8.toString() - targetCompatibility = JavaVersion.VERSION_1_8.toString() - {/if} + sourceCompatibility = JavaVersion.VERSION_{java.version}.toString() + targetCompatibility = JavaVersion.VERSION_{java.version}.toString() } diff --git a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle/base/build-layout.include.qute b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle/base/build-layout.include.qute index 53c267137122b..dd292c9d2fbd6 100644 --- a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle/base/build-layout.include.qute +++ b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle/base/build-layout.include.qute @@ -46,13 +46,8 @@ version '{project.version}' {#insert java} java { - {#if java.version == "17"} - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - {#else} - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 - {/if} + sourceCompatibility = JavaVersion.VERSION_{java.version} + targetCompatibility = JavaVersion.VERSION_{java.version} } {/} diff --git a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle/scala/build.tpl.qute.gradle b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle/scala/build.tpl.qute.gradle index 6119eea5168af..835921cebee0b 100644 --- a/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle/scala/build.tpl.qute.gradle +++ b/independent-projects/tools/base-codestarts/src/main/resources/codestarts/quarkus/buildtool/gradle/scala/build.tpl.qute.gradle @@ -9,11 +9,6 @@ plugins { compileScala { scalaCompileOptions.encoding = 'UTF-8' - {#if java.version == "11"} - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 - {#else} - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 - {/if} + sourceCompatibility = JavaVersion.VERSION_{java.version} + targetCompatibility = JavaVersion.VERSION_{java.version} } diff --git a/integration-tests/maven/src/test/java/io/quarkus/maven/it/CreateProjectMojoIT.java b/integration-tests/maven/src/test/java/io/quarkus/maven/it/CreateProjectMojoIT.java index 39ba3756cf3af..8374d41fe2a17 100644 --- a/integration-tests/maven/src/test/java/io/quarkus/maven/it/CreateProjectMojoIT.java +++ b/integration-tests/maven/src/test/java/io/quarkus/maven/it/CreateProjectMojoIT.java @@ -494,6 +494,24 @@ public void testProjectGenerationFromScratchWithJava17() throws MavenInvocationE .contains("maven.compiler.release>17<"); } + @Test + public void testProjectGenerationFromScratchWithJava21() throws MavenInvocationException, IOException { + testDir = initEmptyProject("projects/project-generation-with-java21"); + assertThat(testDir).isDirectory(); + invoker = initInvoker(testDir); + + Properties properties = new Properties(); + properties.put("javaVersion", "21"); + + InvocationResult result = setup(properties); + assertThat(result.getExitCode()).isZero(); + + testDir = new File(testDir, "code-with-quarkus"); + assertThat(new File(testDir, "pom.xml")).isFile(); + assertThat(FileUtils.readFileToString(new File(testDir, "pom.xml"), "UTF-8")) + .contains("maven.compiler.release>21<"); + } + @Test public void testProjectGenerationFromScratchWithGradleJava11() throws MavenInvocationException, IOException { testDir = initEmptyProject("projects/project-generation-with-gradle-java11"); @@ -532,6 +550,25 @@ public void testProjectGenerationFromScratchWithGradleJava17() throws MavenInvoc .contains("sourceCompatibility = JavaVersion.VERSION_17"); } + @Test + public void testProjectGenerationFromScratchWithGradleJava21() throws MavenInvocationException, IOException { + testDir = initEmptyProject("projects/project-generation-with-gradle-java21"); + assertThat(testDir).isDirectory(); + invoker = initInvoker(testDir); + + Properties properties = new Properties(); + properties.put("javaVersion", "21"); + properties.put("buildTool", "gradle"); + + InvocationResult result = setup(properties); + assertThat(result.getExitCode()).isZero(); + + testDir = new File(testDir, "code-with-quarkus"); + assertThat(new File(testDir, "build.gradle")).isFile(); + assertThat(FileUtils.readFileToString(new File(testDir, "build.gradle"), "UTF-8")) + .contains("sourceCompatibility = JavaVersion.VERSION_21"); + } + /** * Reproducer for https://github.com/quarkusio/quarkus/issues/671 */