Browse Source

Update to Gradle 6.7 and use new toolchains feature for testing on Ja… (#1627)

* Update to Gradle 6.7 and use new toolchains feature for testing on Java versions.

* Update docs

* Add back sum

* Use GitHub test JDK for toolchain.

* Use maxJavaVersionForTests for default test version when set.

* Fix RMI

* Fix configs

* Go back to rule

* Extract variable for default Java version.
Anuraag Agrawal 4 years ago
parent
commit
d435da4638

+ 3 - 5
.github/workflows/nightly.yaml

@@ -51,15 +51,13 @@ jobs:
       fail-fast: false
     steps:
       - uses: actions/checkout@v2
-      - name: Set up JDK ${{ matrix.java }} for running tests
+      - id: setup-test-java
+        name: Set up JDK ${{ matrix.java }} for running tests
         uses: actions/setup-java@v1
         with:
           java-version: ${{ matrix.java }}
-      - name: Set JDK ${{ matrix.java }} home
-        run: echo JAVA_${{ matrix.java }}_HOME=${{ env.JAVA_HOME }} >> $GITHUB_ENV
 
       - name: Set up JDK 11 for running Gradle
-        if: matrix.java == 8
         uses: actions/setup-java@v1
         with:
           java-version: 11
@@ -75,7 +73,7 @@ jobs:
           S3_BUILD_CACHE_ACCESS_KEY_ID: ${{ secrets.S3_BUILD_CACHE_ACCESS_KEY_ID }}
           S3_BUILD_CACHE_SECRET_KEY: ${{ secrets.S3_BUILD_CACHE_SECRET_KEY }}
         with:
-          command: ./gradlew testJava${{ matrix.java }} --stacktrace
+          command: ./gradlew testJava${{ matrix.java }} --stacktrace -Dorg.gradle.java.installations.paths=${{ steps.setup-test-java.outputs.path }}
           timeout_minutes: 90
           max_attempts: 3
 

+ 3 - 4
.github/workflows/pr.yaml

@@ -44,12 +44,11 @@ jobs:
       fail-fast: false
     steps:
       - uses: actions/checkout@v2
-      - name: Set up JDK ${{ matrix.java }} for running tests
+      - id: setup-test-java
+        name: Set up JDK ${{ matrix.java }} for running tests
         uses: actions/setup-java@v1
         with:
           java-version: ${{ matrix.java }}
-      - name: Set JDK ${{ matrix.java }} home
-        run: echo JAVA_${{ matrix.java }}_HOME=${{ env.JAVA_HOME }} >> $GITHUB_ENV
 
       - name: Set up JDK 11 for running Gradle
         uses: actions/setup-java@v1
@@ -64,7 +63,7 @@ jobs:
       - name: Test
         uses: nick-invision/retry@v1
         with:
-          command: ./gradlew testJava${{ matrix.java }} --stacktrace -x :smoke-tests:test
+          command: ./gradlew testJava${{ matrix.java }} --stacktrace -x :smoke-tests:test -Dorg.gradle.java.installations.paths=${{ steps.setup-test-java.outputs.path }}
           timeout_minutes: 90
           max_attempts: 3
 

+ 0 - 4
build.gradle

@@ -48,10 +48,6 @@ repositories {
 
 description = 'OpenTelemetry instrumentations for Java'
 
-wrapper {
-  distributionType = Wrapper.DistributionType.ALL
-}
-
 allprojects {
   apply plugin: 'idea'
 

+ 3 - 8
docs/contributing/running-tests.md

@@ -22,14 +22,9 @@ instrumented library.
 
 #### Executing tests with specific java version
 
-In order to run tests on a specific java version, just execute `./gradlew
-testJava7` (or `testJava11` etc). Then Gradle task
-rule will kick in and do the following:
-
-- check, if Gradle already runs on a java with required version
-- if not, look for an environment variable named `JAVA_N_HOME`, where `N` is the requested java version
-- if Gradle could not found requested java version, then build will fail
-- Gradle will now find all corresponding test tasks and configure them to use java executable of the requested version.
+We run all tests on Java 11 by default, along with Java 8 and 15. To run on the later, use
+`./gradlew testJava8` or `./gradlew testJava15`. If you don't have a JDK of these versions
+installed, Gradle will automatically download it for you.
 
 #### Executing tests against the latest versions of libraries under instrumentation
 

+ 6 - 0
gradle/codenarc.gradle

@@ -2,6 +2,12 @@ apply plugin: "codenarc"
 
 dependencies {
   codenarc "org.codenarc:CodeNarc:1.5"
+
+  constraints {
+    // Allows codenarc to run on JDK14+
+    // https://github.com/gradle/gradle/issues/12646
+    "codenarc"("org.codehaus.groovy:groovy:2.5.10")
+  }
 }
 
 codenarc {

+ 24 - 68
gradle/java.gradle

@@ -16,6 +16,9 @@ afterEvaluate {
   }
 }
 
+// Version to use to compile code and run tests.
+def DEFAULT_JAVA_VERSION = 11
+
 def applyCodeCoverage = !(
   project.path.startsWith(":smoke-tests") ||
     //TODO why some tests fail on java 11 if jacoco is present?
@@ -37,6 +40,11 @@ if (project.hasProperty("minJavaVersionForTests")) {
 java {
   sourceCompatibility = JavaVersion.toVersion(project.ext.release)
   targetCompatibility = JavaVersion.toVersion(project.ext.release)
+
+  toolchain {
+    languageVersion = JavaLanguageVersion.of(Math.max(project.ext.release.majorVersion.toInteger(), DEFAULT_JAVA_VERSION))
+  }
+
   // See https://docs.gradle.org/current/userguide/upgrading_version_5.html, Automatic target JVM version
   disableAutoTargetJvm()
   withJavadocJar()
@@ -44,13 +52,13 @@ java {
 }
 
 tasks.withType(JavaCompile) {
-  options.compilerArgs.addAll(['--release', project.ext.release.majorVersion])
+  options.release = project.ext.release.majorVersion.toInteger()
 }
 tasks.withType(GroovyCompile) {
-  options.compilerArgs.addAll(['--release', project.ext.release.majorVersion])
+  options.release = project.ext.release.majorVersion.toInteger()
 }
 tasks.withType(ScalaCompile) {
-  options.compilerArgs.addAll(['--release', project.ext.release.majorVersion])
+  options.release = project.ext.release.majorVersion.toInteger()
 }
 
 apply plugin: "eclipse"
@@ -175,39 +183,6 @@ project.afterEvaluate {
   }
 }
 
-if (!rootProject.ext.has("javaExecutableVersionCache")) {
-  rootProject.ext.javaExecutableVersionCache = [:]
-}
-
-/**
- * Returns version of java from a given java home.
- */
-JavaVersion getJavaHomeVersion(String javaHome) {
-  def cache = rootProject.ext.javaExecutableVersionCache
-  if (cache.containsKey(javaHome)) {
-    return cache.get(javaHome)
-  }
-  new ByteArrayOutputStream().withStream { stream ->
-    exec {
-      commandLine = [toExecutable(javaHome), "-version"]
-      errorOutput = stream
-    }
-    def output = stream.toString()
-    for (def line : output.split('\n')) {
-      line = line.trim()
-      def matcher = line =~ /^(?:java|openjdk) version "([^"]+)"/
-      if (matcher) {
-        def version = JavaVersion.toVersion(matcher.group(1))
-        cache.put(javaHome, version)
-        return version
-      }
-    }
-
-    // Getting here means we didn't find a line matching the version pattern.
-    throw new GradleScriptException("Cannot determine java version. Executable: ${javaHome}, output: ${output}", null)
-  }
-}
-
 def isJavaVersionAllowed(JavaVersion version) {
   if (project.hasProperty('minJavaVersionForTests') && project.getProperty('minJavaVersionForTests').compareTo(version) > 0) {
     return false
@@ -218,28 +193,15 @@ def isJavaVersionAllowed(JavaVersion version) {
   return true
 }
 
-/**
- * For a given java home return the location of java executable
- */
-static String toExecutable(String javaHome) {
-  return Objects.requireNonNull(javaHome) + "/bin/java"
-}
-
-/**
- * Returns java home for a given version or {@code null} if not found
- */
-String findJavaHome(JavaVersion version) {
-  def javaHome = System.getenv("JAVA_${version.majorVersion}_HOME")
-  if (javaHome == null) {
-    return null
+// We default to testing with Java 11 for most tests, but some tests don't support it, where we change
+// the default test task's version so commands like `./gradlew check` can test all projects regardless
+// of Java version.
+if (!isJavaVersionAllowed(JavaVersion.toVersion(DEFAULT_JAVA_VERSION))) {
+  tasks.withType(Test) {
+    javaLauncher = javaToolchains.launcherFor {
+      languageVersion = JavaLanguageVersion.of(project.getProperty('maxJavaVersionForTests').majorVersion)
+    }
   }
-  def foundVersion = getJavaHomeVersion(javaHome)
-  return version == foundVersion ? javaHome : null
-}
-
-ext {
-  findJavaHome = this.&findJavaHome
-  toExecutable = this.&toExecutable
 }
 
 def addTestRule(String testTaskName) {
@@ -250,15 +212,11 @@ def addTestRule(String testTaskName) {
       def gradleJavaVersion = JavaVersion.current()
 
       if (gradleJavaVersion != requestedJavaVersion) {
-        def javaHomeForTests = findJavaHome(requestedJavaVersion)
-        if (javaHomeForTests != null) {
-
-          tasks.withType(Test).all {
-            executable = toExecutable(javaHomeForTests)
-            enabled = isJavaVersionAllowed(requestedJavaVersion)
+        tasks.withType(Test).all {
+          javaLauncher = javaToolchains.launcherFor {
+            languageVersion = JavaLanguageVersion.of(requestedJavaVersion.majorVersion)
           }
-        } else {
-          throw new BuildCancelledException("Requested java version $requestedJavaVersion not found")
+          enabled = isJavaVersionAllowed(requestedJavaVersion)
         }
       }
 
@@ -272,7 +230,6 @@ def addTestRule(String testTaskName) {
 }
 
 addTestRule("test")
-addTestRule("latestDepTest")
 
 tasks.withType(Test).configureEach {
   useJUnitPlatform()
@@ -281,9 +238,8 @@ tasks.withType(Test).configureEach {
   // This value is quite big because with lower values (3 mins) we were experiencing large number of false positives
   timeout = Duration.ofMinutes(15)
 
-  // Disable all tests if current JVM doesn't match version requirements
   // Disable all tests if skipTests property was specified
-  enabled = isJavaVersionAllowed(JavaVersion.current()) && !project.rootProject.hasProperty("skipTests")
+  enabled = !project.rootProject.hasProperty("skipTests")
 
   retry {
     // You can see tests that were retried by this mechanism in the collected test reports and build scans.

+ 2 - 2
gradle/wrapper/gradle-wrapper.properties

@@ -1,6 +1,6 @@
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionSha256Sum=83fa7c3e5ab84c3c5c4a04fb16947338209efa9aab1f6bf09a5d0e3d2ed87742
+distributionSha256Sum=8ad57759019a9233dc7dc4d1a530cefe109dc122000d57f7e623f8cf4ba9dfc4

+ 12 - 4
instrumentation/rmi/rmi.gradle

@@ -14,10 +14,18 @@ task "rmic", dependsOn: testClasses {
 
 test.dependsOn "rmic"
 
-//We cannot use "--release 7" javac option here because that will forbid importing "sun.rmi" package.
+// We cannot use "--release" javac option here because that will forbid importing "sun.rmi" package.
+// We also can't seem to use the toolchain without the "--release" option. So disable everything.
+
+java {
+  toolchain {
+    languageVersion = null
+  }
+}
+
 tasks.withType(JavaCompile) {
-  options.compilerArgs = []
+  options.release = null
 }
 tasks.withType(GroovyCompile) {
-  options.compilerArgs = []
-}
+  options.release = null
+}