You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by yh...@apache.org on 2025/05/09 14:43:35 UTC
(beam) branch master updated: Enable certain Beam module compile with newer Java version (#34858)
This is an automated email from the ASF dual-hosted git repository.
yhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new 3a967752fdd Enable certain Beam module compile with newer Java version (#34858)
3a967752fdd is described below
commit 3a967752fdd0d762d5dce9dd6348d9ee17aa097d
Author: Yi Hu <ya...@google.com>
AuthorDate: Fri May 9 10:43:28 2025 -0400
Enable certain Beam module compile with newer Java version (#34858)
* Enable certain Beam module compile with newer Java version
* Fix spotless
---
.../org/apache/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 9cf3f2548f8..a09ce2b3ec1 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<Project> {
* The set of additional maven repositories that should be added into published POM file.
*/
List<Map> mavenRepositories = []
+
+ /**
+ * Set minimal Java version needed to compile the module.
+ *
+ * <p>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<Project> {
}
// 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<Project> {
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<Project> {
}
}
+ // 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> {
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 ebb27021b41..dff97980675 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
- }
-}