Skip to content

Commit

Permalink
Use compileJava options.release (#2301)
Browse files Browse the repository at this point in the history
* Use `compileJava` `options.release`
Motivation:
Using `sourceCompatibility` and `targetCompatibility` for specifying
Java version does not always produce correct module metadata because
the setting is timing sensitive.
Modifications:
Use `options.release` whenever build JDK is >= Java 9
Result:
Correct Java variant versions exported in module files
  • Loading branch information
bondolo authored Aug 5, 2022
1 parent 987538c commit e6225ef
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import io.servicetalk.gradle.plugin.internal.Versions

if (!repositories) {
allprojects {
buildscript {
Expand Down Expand Up @@ -43,7 +45,9 @@ subprojects {
task printJavaTargetCompatibility {
doLast {
if (project.parent == project.rootProject) {
println("version: " + project.properties.targetCompatibility + " name: " + project.name)
def javaLanguage = (project.tasks.withType(JavaCompile)?.findByName("compileJava")?.options?.release?.get()) ?:
Integer.parseInt(Versions.TARGET_VERSION.getMajorVersion())
println("version: " + javaLanguage + " name: " + project.name)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions docs/generation/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

# suppress inspection "UnusedProperty" for whole file

# build configuration
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configureondemand=true
org.gradle.java.installations.auto-download=false

antoraVersion=2.3.4
jsoupVersion=1.14.3
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configureondemand=true
org.gradle.java.installations.auto-download=false
org.gradle.jvmargs=-Xms2g -Xmx4g -dsa -da -ea:io.servicetalk... -XX:+HeapDumpOnOutOfMemoryError

# project metadata used for publications
Expand Down
1 change: 1 addition & 0 deletions servicetalk-buffer-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-params"
testImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"

testFixturesImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
}
15 changes: 11 additions & 4 deletions servicetalk-concurrent-jdkflow/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@

apply plugin: "io.servicetalk.servicetalk-gradle-plugin-internal-library"

if (!JavaVersion.current().isJava9Compatible()) {
// Required version for module
def javaLanguageVersion = JavaVersion.VERSION_1_9

if (!JavaVersion.current().isCompatibleWith(javaLanguageVersion)) {
project.tasks.all { task -> task.enabled = false }
}

java {
sourceCompatibility = JavaVersion.VERSION_1_9
targetCompatibility = JavaVersion.VERSION_1_9
sourceCompatibility = javaLanguageVersion
targetCompatibility = javaLanguageVersion
}

compileJava {
options.compilerArgs.addAll(['--release', '9'])
options.release = Integer.parseInt(javaLanguageVersion.getMajorVersion())
}

compileTestJava {
options.release = compileJava.options.release
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package io.servicetalk.gradle.plugin.internal

import com.github.spotbugs.snom.SpotBugsTask
import info.solidsoft.gradle.pitest.PitestTask
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.plugins.quality.Pmd
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.tasks.AbstractPublishToMaven
import org.gradle.api.tasks.compile.JavaCompile

import static io.servicetalk.gradle.plugin.internal.ProjectUtils.addManifestAttributes
import static io.servicetalk.gradle.plugin.internal.ProjectUtils.addQualityTask
Expand Down Expand Up @@ -59,6 +61,32 @@ final class ServiceTalkLibraryPlugin extends ServiceTalkCorePlugin {
sourceCompatibility = TARGET_VERSION
targetCompatibility = TARGET_VERSION

def javaRelease = Integer.parseInt(TARGET_VERSION.getMajorVersion())

if (JavaVersion.current().isJava9Compatible()) {
compileJava {
options.release = javaRelease
}
compileTestJava {
options.release = javaRelease
}

// Not every project has compileTestFixturesJava task so we have to defer attempting configuration
project.afterEvaluate {
def compileTasks = project.tasks.withType(JavaCompile)
def compileJavaTask = compileTasks?.findByName("compileJava")
def compileJavaTestFixturesTask = compileTasks?.findByName("compileTestFixturesJava")
if (null != compileJavaTask && null != compileJavaTestFixturesTask) {
def useRelease = compileJavaTask?.options?.release?.getOrNull() ?: javaRelease
// if another action has set a higher language version, never reduce it.
if (compileJavaTestFixturesTask?.options?.release?.getOrNull() == null ||
compileJavaTestFixturesTask.options.release.get() < useRelease) {
compileJavaTestFixturesTask.options.release = useRelease
}
}
}
}

jar {
addManifestAttributes(project, manifest)
}
Expand Down

0 comments on commit e6225ef

Please sign in to comment.