Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter for Java version with CLI & Maven #22848

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions devtools/cli/src/main/java/io/quarkus/cli/CreateApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CreateApp extends BaseCreateCommand {
@CommandLine.ArgGroup(order = 3, heading = "%nBuild tool (Maven):%n")
TargetBuildToolGroup targetBuildTool = new TargetBuildToolGroup();

@CommandLine.ArgGroup(order = 4, heading = "%nTarget language (Java):%n")
@CommandLine.ArgGroup(order = 4, exclusive = false, heading = "%nTarget language:%n")
TargetLanguageGroup targetLanguage = new TargetLanguageGroup();

@CommandLine.ArgGroup(order = 5, exclusive = false, heading = "%nCode Generation:%n")
Expand All @@ -58,7 +58,8 @@ public Integer call() throws Exception {
}

BuildTool buildTool = targetBuildTool.getBuildTool(BuildTool.MAVEN);
SourceType sourceType = targetLanguage.getSourceType(buildTool, extensions, output);
SourceType sourceType = targetLanguage.getSourceType(spec, buildTool, extensions, output);
setJavaVersion(targetLanguage.getJavaVersion());
setSourceTypeExtensions(extensions, sourceType);
setCodegenOptions(codeGeneration);

Expand Down
5 changes: 3 additions & 2 deletions devtools/cli/src/main/java/io/quarkus/cli/CreateCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CreateCli extends BaseCreateCommand {
@CommandLine.ArgGroup(order = 3, heading = "%nBuild tool (Maven):%n")
TargetBuildToolGroup targetBuildTool = new TargetBuildToolGroup();

@CommandLine.ArgGroup(order = 4, heading = "%nTarget language (Java):%n")
@CommandLine.ArgGroup(order = 4, exclusive = false, heading = "%nTarget language:%n")
TargetLanguageGroup targetLanguage = new TargetLanguageGroup();

@CommandLine.ArgGroup(order = 5, exclusive = false, heading = "%nCode Generation:%n")
Expand All @@ -60,7 +60,8 @@ public Integer call() throws Exception {
}

BuildTool buildTool = targetBuildTool.getBuildTool(BuildTool.MAVEN);
SourceType sourceType = targetLanguage.getSourceType(buildTool, extensions, output);
SourceType sourceType = targetLanguage.getSourceType(spec, buildTool, extensions, output);
setJavaVersion(targetLanguage.getJavaVersion());
setSourceTypeExtensions(extensions, sourceType);
setCodegenOptions(codeGeneration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import io.quarkus.devtools.project.codegen.SourceType;
import io.quarkus.registry.RegistryResolutionException;
import picocli.CommandLine;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Spec;

public class BaseCreateCommand implements Callable<Integer> {
@CommandLine.Mixin
Expand Down Expand Up @@ -169,6 +167,11 @@ public void setSourceTypeExtensions(Set<String> extensions, SourceType sourceTyp
setValue(ProjectGenerator.EXTENSIONS, extensions);
}

/** Set Java source level */
public void setJavaVersion(String javaVersion) {
CreateProjectHelper.setJavaVersion(values, javaVersion);
}

/**
* Process code generation options (save values)
*
Expand Down Expand Up @@ -201,8 +204,6 @@ public QuarkusCommandInvocation build(BuildTool buildTool, TargetQuarkusVersionG
Map<String, String> properties)
throws RegistryResolutionException {

// TODO: Allow the Java version to be configured? infer from active Java version?
CreateProjectHelper.setJavaVersion(values, null);
CreateProjectHelper.handleSpringConfiguration(values);
output.debug("Creating an app using the following settings: %s", values);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package io.quarkus.cli.create;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.codegen.CreateProjectHelper;
import io.quarkus.devtools.project.codegen.SourceType;
import picocli.CommandLine;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.ParameterException;

public class TargetLanguageGroup {
SourceType sourceType;

@CommandLine.Option(names = { "--java" }, description = "Use Java")
boolean java = false;
static class VersionCandidates extends ArrayList<String> {
VersionCandidates() {
super(List.copyOf(CreateProjectHelper.JAVA_VERSIONS_LTS));
}
}

@CommandLine.Option(names = {
"--java" }, description = "Target Java version.\n Valid values: ${COMPLETION-CANDIDATES}", completionCandidates = VersionCandidates.class, defaultValue = CreateProjectHelper.DEFAULT_JAVA_VERSION)
String javaVersion = CreateProjectHelper.DEFAULT_JAVA_VERSION;

@CommandLine.Option(names = { "--kotlin" }, description = "Use Kotlin")
boolean kotlin = false;

@CommandLine.Option(names = { "--scala" }, description = "Use Scala")
boolean scala = false;

public SourceType getSourceType(BuildTool buildTool, Set<String> extensions, OutputOptionMixin output) {
public SourceType getSourceType(CommandSpec spec, BuildTool buildTool, Set<String> extensions, OutputOptionMixin output) {
if (kotlin && scala) {
throw new ParameterException(spec.commandLine(),
"Invalid source type. Projects can target either Kotlin (--kotlin) or Scala (--scala), not both.");
}

if (sourceType == null) {
if (buildTool == null) {
// Buildless/JBang only works with Java, atm
Expand All @@ -39,9 +55,16 @@ public SourceType getSourceType(BuildTool buildTool, Set<String> extensions, Out
return sourceType;
}

public String getJavaVersion() {
return javaVersion;
}

@Override
public String toString() {
return "TargetLanguageGroup [java=" + java + ", kotlin=" + kotlin + ", scala=" + scala + ", sourceType=" + sourceType
return "TargetLanguageGroup [java=" + javaVersion
+ ", kotlin=" + kotlin
+ ", scala=" + scala
+ ", sourceType=" + sourceType
+ "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,37 @@ public void testCreateArgPassthrough() throws Exception {
"Should contain JAVA, found: " + result.stdout);
}

@Test
public void testCreateArgJava11() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle",
"-e", "-B", "--verbose",
"--java", "11");

// We don't need to retest this, just need to make sure all of the arguments were passed through
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);

Path buildGradle = project.resolve("build.gradle");
String buildGradleContent = CliDriver.readFileAsString(project, buildGradle);
Assertions.assertTrue(buildGradleContent.contains("sourceCompatibility = JavaVersion.VERSION_11"),
"Java 11 should be used when specified. Found:\n" + buildGradle);
}

@Test
public void testCreateArgJava17() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle",
"-e", "-B", "--verbose",
"--java", "17");

// We don't need to retest this, just need to make sure all of the arguments were passed through
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);

Path buildGradle = project.resolve("build.gradle");
String buildGradleContent = CliDriver.readFileAsString(project, buildGradle);

Assertions.assertTrue(buildGradleContent.contains("sourceCompatibility = JavaVersion.VERSION_17"),
"Java 17 should be used when specified. Found:\n" + buildGradleContent);
}

String validateBasicIdentifiers(Path project, String group, String artifact, String version) throws Exception {
Path buildGradle = project.resolve("build.gradle");
Assertions.assertTrue(buildGradle.toFile().exists(),
Expand Down
32 changes: 32 additions & 0 deletions devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,38 @@ public void testCreateArgPassthrough() throws Exception {
"Should contain JAVA, found: " + result.stdout);
}

@Test
public void testCreateArgJava11() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app",
"-e", "-B", "--verbose",
"--java", "11");

// We don't need to retest this, just need to make sure all of the arguments were passed through
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);

Path pom = project.resolve("pom.xml");
String pomContent = CliDriver.readFileAsString(project, pom);

Assertions.assertTrue(pomContent.contains("maven.compiler.release>11<"),
"Java 11 should be used when specified. Found:\n" + pomContent);
}

@Test
public void testCreateArgJava17() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app",
"-e", "-B", "--verbose",
"--java", "17");

// We don't need to retest this, just need to make sure all of the arguments were passed through
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);

Path pom = project.resolve("pom.xml");
String pomContent = CliDriver.readFileAsString(project, pom);

Assertions.assertTrue(pomContent.contains("maven.compiler.release>17<"),
"Java 17 should be used when specified. Found:\n" + pomContent);
}

String validateBasicIdentifiers(String group, String artifact, String version) throws Exception {
Path pom = project.resolve("pom.xml");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ public class CreateProjectMojo extends AbstractMojo {
@Parameter(property = "platformVersion", required = false)
private String bomVersion;

@Parameter(property = "javaVersion")
private String javaVersion;

/**
* The {@link #path} will define the REST path of the generated code when picking only one of those extensions resteasy,
* resteasy-reactive and spring-web.
Expand Down Expand Up @@ -287,6 +290,7 @@ public void execute() throws MojoExecutionException {
.artifactId(projectArtifactId)
.version(projectVersion)
.sourceType(sourceType)
.javaTarget(javaVersion)
.className(className)
.packageName(packageName)
.extensions(extensions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

public class CreateProjectHelper {

private static final Set<String> JAVA_VERSIONS_LTS = Set.of("11", "17");
private static final String DEFAULT_JAVA_VERSION = "11";
public static final Set<String> JAVA_VERSIONS_LTS = Set.of("11", "17");
public static final String DEFAULT_JAVA_VERSION = "11";
private static final Pattern JAVA_VERSION_PATTERN = Pattern.compile("(?:1\\.)?(\\d+)(?:\\..*)?");

public static final String DEFAULT_GROUP_ID = "org.acme";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,80 @@ public void testProjectGenerationFromScratchWithAppConfigParameter() throws Mave

}

@Test
public void testProjectGenerationFromScratchWithJava11() throws MavenInvocationException, IOException {
testDir = initEmptyProject("projects/project-generation-with-java11");
assertThat(testDir).isDirectory();
invoker = initInvoker(testDir);

Properties properties = new Properties();
properties.put("javaVersion", "11");

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>11<");
}

@Test
public void testProjectGenerationFromScratchWithJava17() throws MavenInvocationException, IOException {
testDir = initEmptyProject("projects/project-generation-with-java17");
assertThat(testDir).isDirectory();
invoker = initInvoker(testDir);

Properties properties = new Properties();
properties.put("javaVersion", "17");

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>17<");
}

@Test
public void testProjectGenerationFromScratchWithGradleJava11() throws MavenInvocationException, IOException {
testDir = initEmptyProject("projects/project-generation-with-gradle-java11");
assertThat(testDir).isDirectory();
invoker = initInvoker(testDir);

Properties properties = new Properties();
properties.put("javaVersion", "11");
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_11");
}

@Test
public void testProjectGenerationFromScratchWithGradleJava17() throws MavenInvocationException, IOException {
testDir = initEmptyProject("projects/project-generation-with-gradle-java17");
assertThat(testDir).isDirectory();
invoker = initInvoker(testDir);

Properties properties = new Properties();
properties.put("javaVersion", "17");
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_17");
}

/**
* Reproducer for https://github.com/quarkusio/quarkus/issues/671
*/
Expand Down