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 2023/09/06 17:01:24 UTC
[beam] branch master updated: Fix Java 11 Example PreCommit GItHub Action (#28291)
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 73a9c5fb3f6 Fix Java 11 Example PreCommit GItHub Action (#28291)
73a9c5fb3f6 is described below
commit 73a9c5fb3f60e3a0cda05f0f148b116328b480cf
Author: Yi Hu <ya...@google.com>
AuthorDate: Wed Sep 6 13:01:14 2023 -0400
Fix Java 11 Example PreCommit GItHub Action (#28291)
* Partially revert #28234 to fix Java 11 Example PreCommit
* Dedup compile args
* fix typo
* Fix compilerArgs addAll
* Move compileAndRunTestsWithJavaX after errorprone has set up
* Still use --release for java8 option
---
...eam_PreCommit_Java_Examples_Dataflow_Java11.yml | 2 +
.../org/apache/beam/gradle/BeamModulePlugin.groovy | 86 ++++++++++++++--------
2 files changed, 58 insertions(+), 30 deletions(-)
diff --git a/.github/workflows/beam_PreCommit_Java_Examples_Dataflow_Java11.yml b/.github/workflows/beam_PreCommit_Java_Examples_Dataflow_Java11.yml
index 39910471a8f..8f8a3bba405 100644
--- a/.github/workflows/beam_PreCommit_Java_Examples_Dataflow_Java11.yml
+++ b/.github/workflows/beam_PreCommit_Java_Examples_Dataflow_Java11.yml
@@ -100,6 +100,8 @@ jobs:
service_account_key: ${{ secrets.GCP_SA_KEY }}
project_id: ${{ secrets.GCP_PROJECT_ID }}
export_default_credentials: true
+ # The workflow installs java 11 and as default jvm. This is different from
+ # PreCommit_Java_Examples_Dataflow_Java17 where the build system and sources are compiled with Java8
- name: Set up Java
uses: actions/setup-java@v3.8.0
with:
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 a651f0cabea..0c0574b3485 100644
--- a/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy
+++ b/buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy
@@ -18,23 +18,24 @@
package org.apache.beam.gradle
+import static java.util.UUID.randomUUID
+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
+import java.net.ServerSocket
+import java.util.logging.Logger
import org.gradle.api.attributes.Category
import org.gradle.api.GradleException
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
-import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree
-import org.gradle.api.plugins.quality.Checkstyle
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.Copy
-import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.Exec
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.TaskProvider
@@ -43,13 +44,10 @@ import org.gradle.api.tasks.compile.CompileOptions
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.javadoc.Javadoc
import org.gradle.api.tasks.testing.Test
-import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.testing.jacoco.tasks.JacocoReport
-import java.net.ServerSocket
-import static java.util.UUID.randomUUID
/**
* This plugin adds methods to configure a module with Beam's defaults, called "natures".
*
@@ -67,6 +65,8 @@ import static java.util.UUID.randomUUID
*/
class BeamModulePlugin implements Plugin<Project> {
+ static final Logger logger = Logger.getLogger(BeamModulePlugin.class.getName())
+
/** Licence header enforced by spotless */
static final String javaLicenseHeader = """/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -453,6 +453,38 @@ class BeamModulePlugin implements Plugin<Project> {
return 'beam' + p.path.replace(':', '-')
}
+ /*
+ * Set compile args for compiling and running in different java version by modifying the compiler args in place.
+ *
+ * Replace `-source X` and `-target X` or `--release X` options if already existed in compilerArgs.
+ */
+ static def setCompileAndRuntimeJavaVersion(List<String> compilerArgs, String ver) {
+ boolean foundS = false, foundT = false
+ int foundR = -1
+ logger.fine("set java ver ${ver} to compiler args")
+ for (int i = 0; i < compilerArgs.size()-1; ++i) {
+ if (compilerArgs.get(i) == '-source') {
+ foundS = true
+ compilerArgs.set(i+1, ver)
+ } else if (compilerArgs.get(i) == '-target') {
+ foundT = true
+ compilerArgs.set(i+1, ver)
+ } else if (compilerArgs.get(i) == '--release') {
+ foundR = i
+ }
+ }
+ if (foundR != -1) {
+ compilerArgs.removeAt(foundR + 1)
+ compilerArgs.removeAt(foundR)
+ }
+ if (!foundS) {
+ compilerArgs.addAll('-source', ver)
+ }
+ if (!foundT) {
+ compilerArgs.addAll('-target', ver)
+ }
+ }
+
void apply(Project project) {
/** ***********************************************************************************************/
@@ -1061,13 +1093,12 @@ class BeamModulePlugin implements Plugin<Project> {
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 '-source 8 -target 8' as
+ // 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 += ['-source', '8']
- options.compilerArgs += ['-target', '8']
+ options.compilerArgs += ['--release', '8']
// TODO(https://github.com/apache/beam/issues/23901): Fix
// optimizerOuterThis breakage
options.compilerArgs += ['-XDoptimizeOuterThis=false']
@@ -1089,21 +1120,6 @@ class BeamModulePlugin implements Plugin<Project> {
preserveFileTimestamps(false)
}
- if (project.hasProperty("compileAndRunTestsWithJava11")) {
- def java11Home = project.findProperty("java11Home")
- project.tasks.compileTestJava {
- options.fork = true
- options.forkOptions.javaHome = java11Home as File
- options.compilerArgs += ['-Xlint:-path']
- options.compilerArgs += ['-source', '11']
- options.compilerArgs += ['-target', '11']
- }
- project.tasks.withType(Test) {
- useJUnit()
- executable = "${java11Home}/bin/java"
- }
- }
-
// 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
@@ -1240,8 +1256,6 @@ class BeamModulePlugin implements Plugin<Project> {
}
}
-
-
if (configuration.shadowClosure) {
// Ensure that tests are packaged and part of the artifact set.
project.task('packageTests', type: Jar) {
@@ -1493,18 +1507,30 @@ class BeamModulePlugin implements Plugin<Project> {
options.errorprone.errorproneArgs.add("-Xep:Slf4jLoggerShouldBeNonStatic:OFF")
}
- if (project.hasProperty("compileAndRunTestsWithJava17")) {
+ if (project.hasProperty("compileAndRunTestsWithJava11")) {
+ def java11Home = project.findProperty("java11Home")
+ project.tasks.compileTestJava {
+ options.fork = true
+ options.forkOptions.javaHome = java11Home as File
+ options.compilerArgs += ['-Xlint:-path']
+ setCompileAndRuntimeJavaVersion(options.compilerArgs, '11')
+ }
+ project.tasks.withType(Test).configureEach {
+ useJUnit()
+ executable = "${java11Home}/bin/java"
+ }
+ } else if (project.hasProperty("compileAndRunTestsWithJava17")) {
def java17Home = project.findProperty("java17Home")
project.tasks.compileTestJava {
- options.compilerArgs += ['-target', '17']
- options.compilerArgs += ['-source', '17']
+ setCompileAndRuntimeJavaVersion(options.compilerArgs, '17')
project.ext.setJava17Options(options)
}
- project.tasks.withType(Test) {
+ project.tasks.withType(Test).configureEach {
useJUnit()
executable = "${java17Home}/bin/java"
}
}
+
if (configuration.shadowClosure) {
// Enables a plugin which can perform shading of classes. See the general comments
// above about dependency management for Java projects and how the shadow plugin