Skip to content

Commit

Permalink
Use dynamically resolved Java version when creating Gradle projects
Browse files Browse the repository at this point in the history
Also add tests for Java 21.
Fixes #37047
  • Loading branch information
gsmet committed Nov 13, 2023
1 parent 7ebe3dc commit 20d40b1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
{/}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ plugins {

tasks.withType<ScalaCompile> {
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
{/}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 20d40b1

Please sign in to comment.