Skip to content

Commit

Permalink
Check for min java version when updating (partial)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Jun 16, 2023
1 parent 3099398 commit 6a75307
Show file tree
Hide file tree
Showing 23 changed files with 224 additions and 105 deletions.
2 changes: 1 addition & 1 deletion devtools/cli/src/main/java/io/quarkus/cli/CreateApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.quarkus.cli.create.TargetGAVGroup;
import io.quarkus.cli.create.TargetLanguageGroup;
import io.quarkus.devtools.commands.CreateProject.CreateProjectKey;
import io.quarkus.devtools.commands.SourceType;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.handlers.CreateJBangProjectCommandHandler;
import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler;
Expand Down
2 changes: 1 addition & 1 deletion devtools/cli/src/main/java/io/quarkus/cli/CreateCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.quarkus.cli.create.TargetGAVGroup;
import io.quarkus.cli.create.TargetLanguageGroup;
import io.quarkus.devtools.commands.CreateProject.CreateProjectKey;
import io.quarkus.devtools.commands.SourceType;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.handlers.CreateJBangProjectCommandHandler;
import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkus.cli.create;

import static io.quarkus.devtools.commands.CreateProjectHelper.computeJavaVersion;
import static io.quarkus.devtools.project.JavaVersion.computeJavaVersion;

import java.nio.file.Path;
import java.util.Collection;
Expand All @@ -17,7 +17,7 @@
import io.quarkus.cli.registry.ToggleRegistryClientMixin;
import io.quarkus.devtools.commands.CreateProject.CreateProjectKey;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.commands.SourceType;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.stream.Collectors;

import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.commands.SourceType;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import picocli.CommandLine;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.ParameterException;
Expand All @@ -17,13 +17,13 @@ public class TargetLanguageGroup {

static class VersionCandidates extends ArrayList<String> {
VersionCandidates() {
super(CreateProjectHelper.JAVA_VERSIONS_LTS.stream().map(String::valueOf).collect(Collectors.toList()));
super(JavaVersion.JAVA_VERSIONS_LTS.stream().map(String::valueOf).collect(Collectors.toList()));
}
}

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

@CommandLine.Option(names = { "--kotlin" }, description = "Use Kotlin")
boolean kotlin = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import org.gradle.api.GradleException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.compile.JavaCompile;

import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.devtools.project.buildfile.BuildFile;
Expand Down Expand Up @@ -118,7 +121,6 @@ protected String quarkusCoreVersion() {
}

protected QuarkusProject getQuarkusProject(boolean limitExtensionsToImportedPlatforms) {

final GradleMessageWriter log = messageWriter();
final ExtensionCatalog catalog = extensionsCatalog(limitExtensionsToImportedPlatforms, log);

Expand All @@ -136,7 +138,17 @@ protected QuarkusProject getQuarkusProject(boolean limitExtensionsToImportedPlat
throw new GradleException(
"Mixed DSL is not supported. Both build and settings file need to use either Kotlin or Groovy DSL");
}
return QuarkusProjectHelper.getProject(getProject().getProjectDir().toPath(), catalog, buildFile, log);
final JavaVersion javaVersion = resolveProjectJavaVersion();
return QuarkusProjectHelper.getProject(getProject().getProjectDir().toPath(), catalog, buildFile, javaVersion, log);
}

private JavaVersion resolveProjectJavaVersion() {
TaskCollection<JavaCompile> compileTasks = getProject().getTasks().withType(JavaCompile.class);
if (compileTasks.isEmpty()) {
return JavaVersion.NA;
}
final JavaCompile task = compileTasks.iterator().next();
return new JavaVersion(task.getTargetCompatibility());
}

protected GradleMessageWriter messageWriter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.maven.utilities.MojoUtils;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void execute() throws MojoExecutionException {
.artifactResolver(mvn)
.build();
final CreateJBangProject createJBangProject = new CreateJBangProject(QuarkusProject.of(projectDirPath, catalog,
codestartsResourceLoader, log, BuildTool.MAVEN))
codestartsResourceLoader, log, BuildTool.MAVEN, new JavaVersion(javaVersion)))
.extensions(extensions)
.javaVersion(javaVersion)
.setValue(NO_JBANG_WRAPPER, noJBangWrapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.maven.components.MavenVersionEnforcer;
Expand Down Expand Up @@ -297,7 +298,7 @@ public void execute() throws MojoExecutionException {
.artifactResolver(mvn)
.build();
QuarkusProject newProject = QuarkusProject.of(projectDirPath, catalog,
codestartsResourceLoader, log, buildToolEnum);
codestartsResourceLoader, log, buildToolEnum, new JavaVersion(javaVersion, javaVersion));
final CreateProject createProject = new CreateProject(newProject)
.groupId(projectGroupId)
.artifactId(projectArtifactId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.devtools.project.buildfile.MavenProjectBuildFile;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void execute() throws MojoExecutionException {
final List<ResourceLoader> codestartsResourceLoader = getCodestartResourceLoaders(resolveExtensionCatalog());
quarkusProject = QuarkusProject.of(baseDir(), resolveExtensionCatalog(),
codestartsResourceLoader,
log, buildTool);
log, buildTool, JavaVersion.NA);
}

doExecute(quarkusProject, getMessageWriter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import io.quarkus.devtools.codestarts.CodestartProjectInputBuilder;
import io.quarkus.devtools.codestarts.DataKey;
import io.quarkus.devtools.codestarts.utils.NestedMaps;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.extensions.Extensions;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.maven.dependency.ArtifactKey;
Expand Down Expand Up @@ -86,7 +86,7 @@ public boolean noJBangWrapper() {
public QuarkusJBangCodestartProjectInput build() {
if (!this.containsData("java")) {
this.addData(NestedMaps
.unflatten(Map.of("java.version", String.valueOf(CreateProjectHelper.determineBestJavaLtsVersion()))));
.unflatten(Map.of("java.version", String.valueOf(JavaVersion.determineBestJavaLtsVersion()))));
}
return new QuarkusJBangCodestartProjectInput(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkus.devtools.commands;

import static io.quarkus.devtools.commands.CreateProjectHelper.computeJavaVersion;
import static io.quarkus.devtools.project.JavaVersion.computeJavaVersion;
import static java.util.Objects.requireNonNull;

import java.util.HashMap;
Expand All @@ -14,6 +14,7 @@
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.commands.handlers.CreateJBangProjectCommandHandler;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.SourceType;

public class CreateJBangProject {
public interface CreateJBangProjectKey {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.quarkus.devtools.commands;

import static io.quarkus.devtools.commands.CreateProject.CreateProjectKey.*;
import static io.quarkus.devtools.commands.CreateProjectHelper.computeJavaVersion;
import static io.quarkus.devtools.commands.handlers.CreateProjectCodestartDataConverter.PlatformPropertiesKey.QUARKUS_GRADLE_PLUGIN_VERSION;
import static io.quarkus.devtools.commands.handlers.CreateProjectCodestartDataConverter.PlatformPropertiesKey.QUARKUS_MAVEN_PLUGIN_VERSION;
import static io.quarkus.devtools.project.JavaVersion.computeJavaVersion;
import static java.util.Objects.requireNonNull;

import java.util.Collections;
Expand All @@ -20,6 +20,7 @@
import io.quarkus.devtools.commands.handlers.CreateProjectCommandHandler;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.platform.tools.ToolsUtils;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.lang.model.SourceVersion;
Expand All @@ -25,20 +21,14 @@
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.devtools.commands.CreateProject.CreateProjectKey;
import io.quarkus.devtools.project.SourceType;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.paths.PathTree;
import io.quarkus.registry.catalog.Extension;
import io.quarkus.registry.catalog.ExtensionCatalog;

public class CreateProjectHelper {

// ordering is important here, so let's keep them ordered
public static final SortedSet<Integer> JAVA_VERSIONS_LTS = new TreeSet<>(List.of(11, 17));
public static final int DEFAULT_JAVA_VERSION = 11;
private static final int MAX_LTS_SUPPORTED_BY_KOTLIN = 17;
public static final String DETECT_JAVA_RUNTIME_VERSION = "<<detect java runtime version>>";
private static final Pattern JAVA_VERSION_PATTERN = Pattern.compile("(\\d+)(?:\\..*)?");

public static final String DEFAULT_GROUP_ID = "org.acme";
public static final String DEFAULT_ARTIFACT_ID = "code-with-quarkus";
public static final String DEFAULT_VERSION = "1.0.0-SNAPSHOT";
Expand Down Expand Up @@ -177,45 +167,6 @@ public static Path createOutputDirectory(String targetDirectory) {
return outputPath;
}

public static String computeJavaVersion(SourceType sourceType, String inputJavaVersion) {
Integer javaFeatureVersionTarget = null;

if (inputJavaVersion != null && !DETECT_JAVA_RUNTIME_VERSION.equals(inputJavaVersion)) {
// Probably too much as we should push only the feature version but let's be as thorough as we used to be
Matcher matcher = JAVA_VERSION_PATTERN.matcher(inputJavaVersion);
if (matcher.matches()) {
javaFeatureVersionTarget = Integer.valueOf(matcher.group(1));
}
}

if (javaFeatureVersionTarget == null) {
javaFeatureVersionTarget = Runtime.version().feature();
}

int bestJavaLtsVersion = determineBestJavaLtsVersion(javaFeatureVersionTarget);

if (SourceType.KOTLIN.equals(sourceType)
&& bestJavaLtsVersion > MAX_LTS_SUPPORTED_BY_KOTLIN) {
bestJavaLtsVersion = MAX_LTS_SUPPORTED_BY_KOTLIN;
}
return String.valueOf(bestJavaLtsVersion);
}

public static int determineBestJavaLtsVersion() {
return determineBestJavaLtsVersion(Runtime.version().feature());
}

public static int determineBestJavaLtsVersion(int runtimeVersion) {
int bestLtsVersion = DEFAULT_JAVA_VERSION;
for (int ltsVersion : JAVA_VERSIONS_LTS) {
if (ltsVersion > runtimeVersion) {
break;
}
bestLtsVersion = ltsVersion;
}
return bestLtsVersion;
}

public static Set<String> sanitizeExtensions(Set<String> extensions) {
if (extensions == null) {
return extensions = Set.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import io.quarkus.devtools.codestarts.CodestartType;
import io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog;
import io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartProjectInput;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.commands.data.QuarkusCommandInvocation;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.commands.handlers.CreateProjectCodestartDataConverter.CatalogKey;
import io.quarkus.devtools.messagewriter.MessageIcons;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.JavaVersion;
import io.quarkus.devtools.project.extensions.Extensions;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.platform.tools.ToolsUtils;
Expand Down Expand Up @@ -237,7 +237,7 @@ private static void addOrigins(final List<ExtensionOrigins> extOrigins, Extensio

private void checkMinimumJavaVersion(String javaVersionString, List<Extension> extensions) throws QuarkusCommandException {
final List<Extension> incompatibleExtensions = new ArrayList<>();
final int javaVersion = javaVersionString == null ? CreateProjectHelper.DEFAULT_JAVA_VERSION
final int javaVersion = javaVersionString == null ? JavaVersion.DEFAULT_JAVA_VERSION
: Integer.parseInt(javaVersionString);
for (Extension extension : extensions) {
Integer extMinJavaVersion = getMinimumJavaVersion(extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public class UpdateProjectCommandHandler implements QuarkusCommandHandler {

@Override
public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws QuarkusCommandException {
invocation.log().info("Detected project Java version: %s", invocation.getQuarkusProject().getJavaVersion());
final ApplicationModel appModel = invocation.getValue(UpdateProject.APP_MODEL);
final ExtensionCatalog targetCatalog = invocation.getValue(UpdateProject.TARGET_CATALOG);
final String targetPlatformVersion = invocation.getValue(UpdateProject.TARGET_PLATFORM_VERSION);

final boolean perModule = invocation.getValue(UpdateProject.PER_MODULE, false);
final ProjectState currentState = resolveProjectState(appModel,
invocation.getQuarkusProject().getExtensionsCatalog());
Expand All @@ -68,7 +68,8 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
currentState,
recommendedState);

logUpdates(currentState, recommendedState, platformUpdateInfo, extensionsUpdateInfo, false, perModule,
logUpdates(invocation.getQuarkusProject(), currentState, recommendedState, platformUpdateInfo, extensionsUpdateInfo,
false, perModule,
quarkusProject.log());
final boolean noRewrite = invocation.getValue(UpdateProject.NO_REWRITE, false);

Expand All @@ -80,7 +81,8 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
buildTool,
projectQuarkusPlatformBom.getVersion(),
targetPlatformVersion,
kotlinVersion);
kotlinVersion,
extensionsUpdateInfo.getMinJavaVersion());
Path recipe = null;
try {
recipe = Files.createTempFile("quarkus-project-recipe-", ".yaml");
Expand Down Expand Up @@ -130,7 +132,7 @@ private static ArtifactCoords getProjectQuarkusPlatformBOM(ProjectState currentS
return null;
}

private static void logUpdates(ProjectState currentState, ProjectState recommendedState,
private static void logUpdates(QuarkusProject project, ProjectState currentState, ProjectState recommendedState,
ProjectPlatformUpdateInfo platformUpdateInfo,
ProjectExtensionsUpdateInfo extensionsUpdateInfo, boolean recommendState,
boolean perModule, MessageWriter log) {
Expand All @@ -151,7 +153,6 @@ private static void logUpdates(ProjectState currentState, ProjectState recommend
ProjectInfoCommandHandler.logState(recommendedState, perModule, false, log);
return;
}

if (platformUpdateInfo.isPlatformUpdatesAvailable()) {
log.info("Recommended Quarkus platform BOM updates:");
if (!platformUpdateInfo.getImportVersionUpdates().isEmpty()) {
Expand Down Expand Up @@ -235,6 +236,13 @@ private static void logUpdates(ProjectState currentState, ProjectState recommend
log.info("");
}
}
if (extensionsUpdateInfo.getMinJavaVersion().isPresent()) {
final int extensionsMinJavaVersion = extensionsUpdateInfo.getMinJavaVersion().getAsInt();
if (extensionsMinJavaVersion > project.getJavaVersion().getAsInt()) {
log.warn("We detected that some of the updated extensions require an update of the Java version to: ",
extensionsMinJavaVersion);
}
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand Down
Loading

0 comments on commit 6a75307

Please sign in to comment.