diff --git a/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/build.gradle-template.ftl b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/build.gradle-template.ftl index 84873ec16ae9d..1598513be40b9 100644 --- a/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/build.gradle-template.ftl +++ b/devtools/platform-descriptor-json/src/main/resources/templates/basic-rest/scala/build.gradle-template.ftl @@ -21,5 +21,12 @@ group '${project_groupId}' version '${project_version}' compileScala { - scalaCompileOptions.encoding = 'UTF-8' + scalaCompileOptions.encoding = 'UTF-8' + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 } diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProject.java b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProject.java index db5df11fb01dc..fcc0e19e8c965 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProject.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/cli/commands/CreateProject.java @@ -2,6 +2,7 @@ import static io.quarkus.generators.ProjectGenerator.CLASS_NAME; import static io.quarkus.generators.ProjectGenerator.IS_SPRING; +import static io.quarkus.generators.ProjectGenerator.JAVA_TARGET; import static io.quarkus.generators.ProjectGenerator.PROJECT_ARTIFACT_ID; import static io.quarkus.generators.ProjectGenerator.PROJECT_GROUP_ID; import static io.quarkus.generators.ProjectGenerator.PROJECT_VERSION; @@ -18,6 +19,8 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.lang.model.SourceVersion; /** @@ -27,6 +30,8 @@ */ public class CreateProject { + private static final Pattern JAVA_VERSION_PATTERN = Pattern.compile("(?:1\\.)?(\\d+)(?:\\..*)?"); + public static SourceType determineSourceType(Set extensions) { Optional sourceType = extensions.stream() .map(SourceType::parse) @@ -42,6 +47,8 @@ private static boolean isSpringStyle(Collection extensions) { private QuarkusCommandInvocation invocation; + private String javaTarget; + /** * @deprecated since 1.3.0.CR1 * Please use {@link #CreateProject(ProjectWriter, QuarkusPlatformDescriptor)} instead. @@ -76,6 +83,11 @@ public CreateProject sourceType(SourceType sourceType) { return this; } + public CreateProject javaTarget(String javaTarget) { + this.javaTarget = javaTarget; + return this; + } + public CreateProject className(String className) { if (className == null) { return this; @@ -126,6 +138,7 @@ public boolean doCreateProject(final Map context) throws IOExcep } } } + try { return execute().isSuccess(); } catch (QuarkusCommandException e) { @@ -134,6 +147,15 @@ public boolean doCreateProject(final Map context) throws IOExcep } public QuarkusCommandOutcome execute() throws QuarkusCommandException { + // Define the Java version to use determined from the one specified or the one creating the project + Matcher matcher = JAVA_VERSION_PATTERN + .matcher(this.javaTarget != null ? this.javaTarget : System.getProperty("java.version", "")); + if (matcher.matches() && Integer.parseInt(matcher.group(1)) < 11) { + invocation.setProperty(JAVA_TARGET, "8"); + } else { + invocation.setProperty(JAVA_TARGET, "11"); + } + return new CreateProjectCommandHandler().execute(invocation); } } diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/generators/ProjectGenerator.java b/independent-projects/tools/common/src/main/java/io/quarkus/generators/ProjectGenerator.java index fb4686a450e90..ef9a66f4a60cc 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/generators/ProjectGenerator.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/generators/ProjectGenerator.java @@ -22,6 +22,7 @@ public interface ProjectGenerator { String CLASS_NAME = "class_name"; String IS_SPRING = "is_spring"; String RESOURCE_PATH = "path"; + String JAVA_TARGET = "java_target"; String getName(); diff --git a/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java b/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java index 2049877a58c39..ad20146203a15 100644 --- a/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java +++ b/independent-projects/tools/common/src/main/java/io/quarkus/generators/rest/BasicRestProjectGenerator.java @@ -1,5 +1,6 @@ package io.quarkus.generators.rest; +import static io.quarkus.generators.ProjectGenerator.JAVA_TARGET; import static java.lang.String.format; import io.quarkus.cli.commands.QuarkusCommandInvocation; @@ -144,6 +145,24 @@ private void generate(final String templateName, QuarkusCommandInvocation invoca template = template.replace(format("${%s}", e.getKey().toString()), e.getValue().toString()); } } + + // do some nasty replacements for Java target if we want to generate Java 11 projects + if ("11".equals(invocation.getProperty(JAVA_TARGET))) { + if (BuildTool.GRADLE.equals(invocation.getBuildTool())) { + template = template.replace("JavaVersion.VERSION_1_8", "JavaVersion.VERSION_11"); + } else { + template = template.replace("1.8", + "11"); + template = template.replace("1.8", + "11"); + // Kotlin + template = template.replace("1.8", "11"); + // Scala + // For now, we keep Java 8 as a target for Scala as we don't want to upgrade to 2.13 + // template = template.replace("-target:jvm-1.8", "-target:jvm-11"); + } + } + writer.write(outputFilePath, template); } }