Skip to content

Commit

Permalink
Ensure correct runtime Java is used when it matches build Java
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed Jul 10, 2024
1 parent 1f82b0c commit 470c03e
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -97,24 +98,25 @@ public void apply(Project project) {
JavaVersion minimumCompilerVersion = JavaVersion.toVersion(getResourceContents("/minimumCompilerVersion"));
JavaVersion minimumRuntimeVersion = JavaVersion.toVersion(getResourceContents("/minimumRuntimeVersion"));

File runtimeJavaHome = findRuntimeJavaHome();
boolean isRuntimeJavaHomeSet = Jvm.current().getJavaHome().equals(runtimeJavaHome) == false;
Optional<File> selectedRuntimeJavaHome = findRuntimeJavaHome();
File actualRuntimeJavaHome = selectedRuntimeJavaHome.orElse(Jvm.current().getJavaHome());
boolean isRuntimeJavaHomeSet = selectedRuntimeJavaHome.isPresent();

GitInfo gitInfo = GitInfo.gitInfo(project.getRootDir());

BuildParams.init(params -> {
params.reset();
params.setRuntimeJavaHome(runtimeJavaHome);
params.setRuntimeJavaHome(actualRuntimeJavaHome);
params.setJavaToolChainSpec(resolveToolchainSpecFromEnv());
params.setRuntimeJavaVersion(
determineJavaVersion(
"runtime java.home",
runtimeJavaHome,
actualRuntimeJavaHome,
isRuntimeJavaHomeSet ? minimumRuntimeVersion : Jvm.current().getJavaVersion()
)
);
params.setIsRuntimeJavaHomeSet(isRuntimeJavaHomeSet);
JvmInstallationMetadata runtimeJdkMetaData = metadataDetector.getMetadata(getJavaInstallation(runtimeJavaHome));
JvmInstallationMetadata runtimeJdkMetaData = metadataDetector.getMetadata(getJavaInstallation(actualRuntimeJavaHome));
params.setRuntimeJavaDetails(formatJavaVendorDetails(runtimeJdkMetaData));
params.setJavaVersions(getAvailableJavaVersions());
params.setMinimumCompilerVersion(minimumCompilerVersion);
Expand Down Expand Up @@ -298,19 +300,19 @@ private static void assertMinimumCompilerVersion(JavaVersion minimumCompilerVers
}
}

private File findRuntimeJavaHome() {
private Optional<File> findRuntimeJavaHome() {
String runtimeJavaProperty = System.getProperty("runtime.java");

if (runtimeJavaProperty != null) {
return resolveJavaHomeFromToolChainService(runtimeJavaProperty);
return Optional.of(resolveJavaHomeFromToolChainService(runtimeJavaProperty));
}
String env = System.getenv("RUNTIME_JAVA_HOME");
if (env != null) {
return new File(env);
return Optional.of(new File(env));
}
// fall back to tool chain if set.
env = System.getenv("JAVA_TOOLCHAIN_HOME");
return env == null ? Jvm.current().getJavaHome() : new File(env);
return env == null ? Optional.empty() : Optional.of(new File(env));
}

@NotNull
Expand Down

0 comments on commit 470c03e

Please sign in to comment.