Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Java 11 Example PreCommit GItHub Action #28291

Merged
merged 6 commits into from
Sep 6, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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".
*
Expand All @@ -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
Expand Down Expand Up @@ -451,6 +451,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) {

/** ***********************************************************************************************/
Expand Down Expand Up @@ -1055,13 +1087,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']
Expand All @@ -1083,21 +1114,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
Expand Down Expand Up @@ -1234,8 +1250,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) {
Expand Down Expand Up @@ -1487,18 +1501,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
Expand Down