From f7d10af8fdb21db2d4bda5d565c2f567796aafc4 Mon Sep 17 00:00:00 2001 From: Yi Hu Date: Tue, 6 May 2025 10:04:44 -0400 Subject: [PATCH 1/2] Enable certain Beam module compile with newer Java version --- .../beam/gradle/BeamModulePlugin.groovy | 135 +++++++++++++----- sdks/java/container/agent/build.gradle | 47 +----- 2 files changed, 103 insertions(+), 79 deletions(-) diff --git a/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy b/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy index 9cf3f2548f85..2913f53f7803 100644 --- a/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy +++ b/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy @@ -179,6 +179,15 @@ class BeamModulePlugin implements Plugin { * The set of additional maven repositories that should be added into published POM file. */ List mavenRepositories = [] + + /** + * Set minimal Java version needed to compile the module. + * + *

Valid values are LTS versions greater than the lowest supported + * Java version. Used when newer Java byte code version required than Beam's + * byte code compatibility version. + */ + JavaVersion requireJavaVersion = null } /** A class defining the set of configurable properties accepted by applyPortabilityNature. */ @@ -1106,7 +1115,44 @@ class BeamModulePlugin implements Plugin { } // Configure the Java compiler source language and target compatibility levels. Also ensure that - // we configure the Java compiler to use UTF-8. + def requireJavaVersion = JavaVersion.toVersion(project.javaVersion) + if (configuration.requireJavaVersion != null) { + // Overwrite project.javaVersion if requested. + if (JavaVersion.VERSION_11.equals(configuration.requireJavaVersion)) { + project.javaVersion = '11' + } else if (JavaVersion.VERSION_17.equals(configuration.requireJavaVersion)) { + project.javaVersion = '17' + } else if (JavaVersion.VERSION_21.equals(configuration.requireJavaVersion)) { + project.javaVersion = '21' + } else { + throw new GradleException( + "requireJavaVersion has to be supported LTS version greater than the default Java version. Actual: " + + configuration.requireJavaVersion + ) + } + requireJavaVersion = configuration.requireJavaVersion + } + + String forkJavaVersion = null + if (requireJavaVersion.compareTo(JavaVersion.current()) > 0) { + // If compiled on older SDK, compile with JDK configured with compatible javaXXHome + // The order is intended here + if (requireJavaVersion.compareTo(JavaVersion.VERSION_11) <= 0 && + project.hasProperty('java11Home')) { + forkJavaVersion = '11' + } else if (requireJavaVersion.compareTo(JavaVersion.VERSION_17) <= 0 && + project.hasProperty('java17Home')) { + forkJavaVersion = '17' + } else if (requireJavaVersion.compareTo(JavaVersion.VERSION_21) <= 0 && + project.hasProperty('java21Home')) { + forkJavaVersion = '21' + } else { + logger.config("Module ${project.name} disabled. To enable, either " + + "compile on newer Java version or pass java${project.javaVersion}Home project property") + forkJavaVersion = '' + } + } + project.sourceCompatibility = project.javaVersion project.targetCompatibility = project.javaVersion @@ -1129,37 +1175,6 @@ class BeamModulePlugin implements Plugin { defaultLintSuppressions += ['this-escape'] } - project.tasks.withType(JavaCompile).configureEach { - options.encoding = "UTF-8" - // Use -source 8 -target 8 when targeting Java 8 and running on JDK > 8 - // - // Consider migrating compilation and testing to use JDK 9+ and setting '--release 8' as - // the default allowing 'applyJavaNature' to override it for the few modules that need JDK 9+ - // artifacts. See https://stackoverflow.com/a/43103038/4368200 for additional details. - if (JavaVersion.VERSION_1_8.compareTo(JavaVersion.toVersion(project.javaVersion)) == 0 - && JavaVersion.VERSION_1_8.compareTo(JavaVersion.current()) < 0) { - options.compilerArgs += ['--release', '8'] - // TODO(https://github.com/apache/beam/issues/23901): Fix - // optimizerOuterThis breakage - options.compilerArgs += ['-XDoptimizeOuterThis=false'] - } - // As we want to add '-Xlint:-deprecation' we intentionally remove '-Xlint:deprecation' from compilerArgs here, - // as intellij is adding this, see https://youtrack.jetbrains.com/issue/IDEA-196615 - options.compilerArgs -= [ - "-Xlint:deprecation", - ] - options.compilerArgs += ([ - '-parameters', - '-Xlint:all', - '-Werror' - ] - + (defaultLintSuppressions + configuration.disableLintWarnings).collect { "-Xlint:-${it}" }) - } - - project.tasks.withType(Jar).configureEach { - preserveFileTimestamps(false) - } - // Configure the default test tasks set of tests executed // to match the equivalent set that is executed by the maven-surefire-plugin. // See http://maven.apache.org/components/surefire/maven-surefire-plugin/test-mojo.html @@ -1561,6 +1576,45 @@ class BeamModulePlugin implements Plugin { } } + // Handle compile Java versions + project.tasks.withType(JavaCompile).configureEach { + // we configure the Java compiler to use UTF-8. + options.encoding = "UTF-8" + // If compiled on newer JDK, set byte code compatibility + if (requireJavaVersion.compareTo(JavaVersion.current()) < 0) { + def compatVersion = project.javaVersion == '1.8' ? '8' : project.javaVersion + options.compilerArgs += ['--release', compatVersion] + // TODO(https://github.com/apache/beam/issues/23901): Fix + // optimizerOuterThis breakage + options.compilerArgs += ['-XDoptimizeOuterThis=false'] + } else if (forkJavaVersion) { + // If compiled on older SDK, compile with JDK configured with compatible javaXXHome + setCompileAndRuntimeJavaVersion(options.compilerArgs, requireJavaVersion as String) + project.ext.setJavaVerOptions(options, forkJavaVersion) + } + // As we want to add '-Xlint:-deprecation' we intentionally remove '-Xlint:deprecation' from compilerArgs here, + // as intellij is adding this, see https://youtrack.jetbrains.com/issue/IDEA-196615 + options.compilerArgs -= [ + "-Xlint:deprecation", + ] + options.compilerArgs += ([ + '-parameters', + '-Xlint:all', + '-Werror' + ] + + (defaultLintSuppressions + configuration.disableLintWarnings).collect { "-Xlint:-${it}" }) + } + + if (forkJavaVersion) { + project.tasks.withType(Javadoc) { + executable = project.findProperty('java' + forkJavaVersion + 'Home') + '/bin/javadoc' + } + } + + project.tasks.withType(Jar).configureEach { + preserveFileTimestamps(false) + } + // if specified test java version, modify the compile and runtime versions accordingly if (['8', '11', '17', '21'].contains(project.findProperty('testJavaVersion'))) { String ver = project.getProperty('testJavaVersion') @@ -1791,8 +1845,21 @@ class BeamModulePlugin implements Plugin { project.ext.includeInJavaBom = configuration.publish project.ext.exportJavadoc = configuration.exportJavadoc - if ((isRelease(project) || project.hasProperty('publishing')) && - configuration.publish) { + boolean publishEnabledByCommand = isRelease(project) || project.hasProperty('publishing') + if (forkJavaVersion == '') { + // project needs newer version and not served. + // If not publishing ,disable the project. Otherwise, fail the build + def msg = "project ${project.name} needs newer Java version to compile. Consider set -Pjava${project.javaVersion}Home" + if (publishEnabledByCommand) { + throw new GradleException("Publish enabled but " + msg + ".") + } else { + logger.config(msg + " if needed.") + project.tasks.each { + it.enabled = false + } + } + } + if (publishEnabledByCommand && configuration.publish) { project.apply plugin: "maven-publish" // plugin to support repository authentication via ~/.m2/settings.xml diff --git a/sdks/java/container/agent/build.gradle b/sdks/java/container/agent/build.gradle index ebb27021b41f..dff97980675d 100644 --- a/sdks/java/container/agent/build.gradle +++ b/sdks/java/container/agent/build.gradle @@ -20,22 +20,10 @@ plugins { id 'org.apache.beam.module' } -// the order is intended here -// Overwrite javaVersion global property if corresponding project property specified -if (project.hasProperty('java11Home')) { - javaVersion = "1.11" -} else if (project.hasProperty('java17Home')) { - javaVersion = "1.17" -} else if (project.hasProperty('java21Home')) { - javaVersion = "1.21" -} else if (JavaVersion.VERSION_1_8.compareTo(JavaVersion.current()) < 0) { - // Otherwise, compile the project with java11 spec - javaVersion = "1.11" -} - applyJavaNature( exportJavadoc: false, - publish: false + publish: false, + requireJavaVersion: JavaVersion.VERSION_11 ) description = "Apache Beam :: SDKs :: Java :: Container :: Agent" @@ -48,34 +36,3 @@ jar { "Premain-Class": "org.apache.beam.agent.OpenModuleAgent") } } - -// the order is intended here -if (project.hasProperty('java11Home')) { - project.tasks.withType(JavaCompile) { - setJavaVerOptions(options, '11') - } -} else if (project.hasProperty('java17Home')) { - project.tasks.withType(JavaCompile) { - setJavaVerOptions(options, '17') - - checkerFramework { - skipCheckerFramework = true - } - } -} else if (project.hasProperty('java21Home')) { - project.tasks.withType(JavaCompile) { - setJavaVerOptions(options, '21') - - checkerFramework { - skipCheckerFramework = true - } - } -} - -// Module classes requires JDK > 8 -project.tasks.each { - it.onlyIf { - project.hasProperty('java11Home') || project.hasProperty('java17Home') || project.hasProperty('java21Home') - || JavaVersion.VERSION_1_8.compareTo(JavaVersion.current()) < 0 - } -} From d6b23a1f46e73b4157149026e858316ec23de224 Mon Sep 17 00:00:00 2001 From: Yi Hu Date: Tue, 6 May 2025 11:39:24 -0400 Subject: [PATCH 2/2] Fix spotless --- .../beam/gradle/BeamModulePlugin.groovy | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy b/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy index 2913f53f7803..a09ce2b3ec15 100644 --- a/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy +++ b/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy @@ -1126,8 +1126,8 @@ class BeamModulePlugin implements Plugin { project.javaVersion = '21' } else { throw new GradleException( - "requireJavaVersion has to be supported LTS version greater than the default Java version. Actual: " + - configuration.requireJavaVersion + "requireJavaVersion has to be supported LTS version greater than the default Java version. Actual: " + + configuration.requireJavaVersion ) } requireJavaVersion = configuration.requireJavaVersion @@ -1138,17 +1138,17 @@ class BeamModulePlugin implements Plugin { // If compiled on older SDK, compile with JDK configured with compatible javaXXHome // The order is intended here if (requireJavaVersion.compareTo(JavaVersion.VERSION_11) <= 0 && - project.hasProperty('java11Home')) { + project.hasProperty('java11Home')) { forkJavaVersion = '11' } else if (requireJavaVersion.compareTo(JavaVersion.VERSION_17) <= 0 && - project.hasProperty('java17Home')) { + project.hasProperty('java17Home')) { forkJavaVersion = '17' } else if (requireJavaVersion.compareTo(JavaVersion.VERSION_21) <= 0 && - project.hasProperty('java21Home')) { + project.hasProperty('java21Home')) { forkJavaVersion = '21' } else { logger.config("Module ${project.name} disabled. To enable, either " + - "compile on newer Java version or pass java${project.javaVersion}Home project property") + "compile on newer Java version or pass java${project.javaVersion}Home project property") forkJavaVersion = '' } } @@ -1595,14 +1595,14 @@ class BeamModulePlugin implements Plugin { // As we want to add '-Xlint:-deprecation' we intentionally remove '-Xlint:deprecation' from compilerArgs here, // as intellij is adding this, see https://youtrack.jetbrains.com/issue/IDEA-196615 options.compilerArgs -= [ - "-Xlint:deprecation", + "-Xlint:deprecation", ] options.compilerArgs += ([ - '-parameters', - '-Xlint:all', - '-Werror' + '-parameters', + '-Xlint:all', + '-Werror' ] - + (defaultLintSuppressions + configuration.disableLintWarnings).collect { "-Xlint:-${it}" }) + + (defaultLintSuppressions + configuration.disableLintWarnings).collect { "-Xlint:-${it}" }) } if (forkJavaVersion) {