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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.jvm.toolchain.JavaToolchainSpec;
import org.gradle.util.GradleVersion;
import org.javamodularity.moduleplugin.JavaProjectHelper;
import org.javamodularity.moduleplugin.tasks.ClasspathFile;

Expand Down Expand Up @@ -74,6 +77,17 @@ private void configureMixedJavaRelease(int mainJavaRelease, int moduleInfoJavaRe
// TODO: Remove this method when Gradle supports it natively: https://github.com/gradle/gradle/issues/2510
private void setJavaRelease(JavaCompile javaCompile, int javaRelease) {
String currentJavaVersion = JavaVersion.current().toString();
if (toolchainIsSupported()) {
JavaToolchainSpec toolchain = project.getExtensions().getByType(JavaPluginExtension.class).getToolchain();
if (toolchain != null) {
// If toolchain is enabled, the version of java compiler is NOT same to the version of JVM running Gradle
// so we need to get the version of toolchain explicitly as follows
String toolchainVersion = toolchain.getLanguageVersion().map(Object::toString).getOrNull();
if (toolchainVersion != null) {
currentJavaVersion = toolchainVersion;
}
}
}
if (!javaCompile.getSourceCompatibility().equals(currentJavaVersion)) {
throw new IllegalStateException("sourceCompatibility should not be set together with --release option");
}
Expand All @@ -86,8 +100,29 @@ private void setJavaRelease(JavaCompile javaCompile, int javaRelease) {
throw new IllegalStateException("--release option is already set in compiler args");
}

compilerArgs.add("--release");
compilerArgs.add(String.valueOf(javaRelease));
if (releaseOptionIsSupported()) {
// using the `convention(Integer)` method instead of the `set(Integer)` method, to let users set overwrite explicitly
javaCompile.getOptions().getRelease().convention(javaRelease);
} else {
compilerArgs.add("--release");
compilerArgs.add(String.valueOf(javaRelease));
}
}

/**
* @see <a href="https://github.com/gradle/gradle/issues/2510#issuecomment-657436188">The comment on GitHub issue that says {@code --release} option is added in Gradle 6.6</a>
* @return true if the version of Gradle is 6.6 or later
*/
private boolean releaseOptionIsSupported() {
return GradleVersion.current().compareTo(GradleVersion.version("6.6")) >= 0;
}

/**
* @see <a href="https://docs.gradle.org/6.7/javadoc/org/gradle/api/plugins/JavaPluginExtension.html#getToolchain--">The Javadoc that says {@code JavaPluginExtension.getToolchain()} is added in Gradle 6.7</a>
* @return true if the version of Gradle is 6.7 or later
*/
private boolean toolchainIsSupported() {
return GradleVersion.current().compareTo(GradleVersion.version("6.7")) >= 0;
}

private JavaProjectHelper helper() {
Expand Down
18 changes: 18 additions & 0 deletions test-project/greeter.toolchain/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//region NO-OP (DSL testing)

// one of the supported Java version (prefer LTS version)
def toolchainVer = 11

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(toolchainVer))
}
}

modularity {
// This version should be less than `toolchainVer` because:
// * if the version is same to `toolchainVer` we cannot reproduce the existing problem
// * if the version is greather than `toolchainVer` javac cannot create class files
standardJavaRelease(9)
}
//endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package examples.greeter.api;

public interface Greeter {
String hello();
}
3 changes: 3 additions & 0 deletions test-project/greeter.toolchain/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module greeter.api {
exports examples.greeter.api;
}
4 changes: 4 additions & 0 deletions test-project/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ include 'greeter.runner'

include 'greeter.javaexec'
include 'greeter.startscripts'

if(GradleVersion.current().compareTo(GradleVersion.version("6.6")) >= 0) {
include 'greeter.toolchain'
}