From 878e4dc2a7ac6c2cc51d019d8cb3d2be2395dc23 Mon Sep 17 00:00:00 2001 From: Vincent Guilpain Date: Thu, 6 Jun 2024 12:01:03 +0900 Subject: [PATCH] Revert behavior : the Gradle build does not require to use JDK 8 (temurin) (#1837) --- .../com/scalar/db/JdkConfigurationPlugin.java | 61 +++++++++++++------ 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/buildSrc/src/main/java/com/scalar/db/JdkConfigurationPlugin.java b/buildSrc/src/main/java/com/scalar/db/JdkConfigurationPlugin.java index 3d46261c6a..01b469cd6d 100644 --- a/buildSrc/src/main/java/com/scalar/db/JdkConfigurationPlugin.java +++ b/buildSrc/src/main/java/com/scalar/db/JdkConfigurationPlugin.java @@ -16,18 +16,17 @@ /** * Plugin configuring all tasks and integration test tasks to use a given JDK defined by the - * following Gradle properties: + * following Gradle project properties: * * * *

Usage example using the CLI: @@ -38,7 +37,8 @@ * gradle integrationTestJdbc -PjavaVersion=11 -PjavaVendor=amazon * * - * 2. To use JDK 11 (amazon) for all Java tasks while having integration test use JDK 17 (microsoft) + *

2. To use JDK 11 (amazon) for all Java tasks while having integration test use JDK 17 + * (microsoft) * *


  *   gradle integrationTestJdbc -PjavaVersion=11 -PjavaVendor=amazon -PintegrationTestJavaRuntimeVersion=17 -PintegrationTestJavaRuntimeVendor=microsoft
@@ -47,9 +47,6 @@
 public class JdkConfigurationPlugin implements Plugin {
 
   private static final Logger logger = LoggerFactory.getLogger(JdkConfigurationPlugin.class);
-  // JDK 8 (temurin) is used as default for all tasks (compilation, tests, etc.)
-  private static final JavaLanguageVersion DEFAULT_JAVA_VERSION = JavaLanguageVersion.of(8);
-  private static final JvmVendorSpec DEFAULT_JAVA_VENDOR = JvmVendorSpec.ADOPTIUM;
 
   private static final String JAVA_VERSION_PROP = "javaVersion";
   private static final String JAVA_VENDOR_PROP = "javaVendor";
@@ -58,23 +55,34 @@ public class JdkConfigurationPlugin implements Plugin {
   private static final String INTEGRATION_TEST_JAVA_RUNTIME_VENDOR_PROP =
       "integrationTestJavaRuntimeVendor";
 
-  private JavaLanguageVersion javaVersion = DEFAULT_JAVA_VERSION;
-  private JvmVendorSpec javaVendor = DEFAULT_JAVA_VENDOR;
+  @Nullable private JavaLanguageVersion javaVersion;
+  @Nullable private JvmVendorSpec javaVendor;
   @Nullable private JavaLanguageVersion integrationTestJavaVersion;
   @Nullable private JvmVendorSpec integrationTestJavaVendor;
 
   @Override
   public void apply(@NotNull Project project) {
     parseIntegrationTestInputProperties(project);
+    validateInput();
     configureJdkForAllJavaTasks(project);
     configureJdkForIntegrationTestTasks(project);
   }
 
   private void configureJdkForAllJavaTasks(Project project) {
     JavaPluginExtension javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class);
-    javaPlugin.getToolchain().getLanguageVersion().set(javaVersion);
-    javaPlugin.getToolchain().getVendor().set(javaVendor);
-    logger.debug("Configure JDK {} ({}) for Java tasks", javaVersion, javaVendor);
+    if (javaVersion == null) {
+      // When the JDK is not set explicitly with the `javaVersion` property, this ensures the
+      // project is compiled to Java 8 bytecode
+      javaPlugin.setSourceCompatibility(JavaLanguageVersion.of(8));
+      javaPlugin.setTargetCompatibility(JavaLanguageVersion.of(8));
+    } else {
+      javaPlugin.getToolchain().getLanguageVersion().set(javaVersion);
+      logger.debug("Configure to use JDK {} for Java tasks", javaVersion);
+    }
+    if (javaVendor != null) {
+      javaPlugin.getToolchain().getVendor().set(javaVendor);
+      logger.debug("Configure to use JDK from {} vendor for Java tasks", javaVendor);
+    }
   }
 
   private void configureJdkForIntegrationTestTasks(Project project) {
@@ -121,6 +129,25 @@ private void parseIntegrationTestInputProperties(Project project) {
         (vendor) -> integrationTestJavaVendor = vendor);
   }
 
+  private void validateInput() {
+    if (javaVersion == null && javaVendor != null) {
+      throw new IllegalArgumentException(
+          "Setting the '"
+              + JAVA_VENDOR_PROP
+              + "' Gradle project property also requires setting the '"
+              + JAVA_VERSION_PROP
+              + "' property.");
+    }
+    if (integrationTestJavaVersion == null && integrationTestJavaVendor != null) {
+      throw new IllegalArgumentException(
+          "Setting the '"
+              + INTEGRATION_TEST_JAVA_RUNTIME_VENDOR_PROP
+              + "' Gradle project property also requires setting the '"
+              + INTEGRATION_TEST_JAVA_RUNTIME_VERSION_PROP
+              + "' property.");
+    }
+  }
+
   private void parseVersionInputProperty(
       Project project, String property, Consumer attributeSetter) {
     if (project.hasProperty(property)) {