Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 101 additions & 34 deletions buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
47 changes: 2 additions & 45 deletions sdks/java/container/agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
}
Loading