From 8800e7153bf2d083d1c66437073a5f704ec453a4 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 1 Feb 2023 18:40:53 +0100 Subject: [PATCH] Don't use Gradle toolchains to specify Java version --- build-logic/build.gradle.kts | 8 +-- .../teavm/buildutil/JavaVersionExtension.java | 24 +++++++++ .../teavm/buildutil/JavaVersionPlugin.java | 52 +++++++++++++++++++ samples/settings.gradle.kts | 7 +-- settings.gradle.kts | 9 ++-- tests/build.gradle.kts | 6 +-- 6 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 build-logic/src/main/java/org/teavm/buildutil/JavaVersionExtension.java create mode 100644 build-logic/src/main/java/org/teavm/buildutil/JavaVersionPlugin.java diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index 64efcfdeb..6a8bb3297 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -52,6 +52,10 @@ gradlePlugin { id = "mavenPlugin" implementationClass = "org.teavm.buildutil.MavenPluginPlugin" } + create("javaVersion") { + id = "javaVersion" + implementationClass = "org.teavm.buildutil.JavaVersionPlugin" + } } } @@ -61,9 +65,7 @@ checkstyle { } java { - toolchain { - languageVersion.set(JavaLanguageVersion.of(11)) - } + sourceCompatibility = JavaVersion.VERSION_11 } val generatedConfigDir = project.layout.buildDirectory.dir("generated/config").get() diff --git a/build-logic/src/main/java/org/teavm/buildutil/JavaVersionExtension.java b/build-logic/src/main/java/org/teavm/buildutil/JavaVersionExtension.java new file mode 100644 index 000000000..300c68abe --- /dev/null +++ b/build-logic/src/main/java/org/teavm/buildutil/JavaVersionExtension.java @@ -0,0 +1,24 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.buildutil; + +import org.gradle.api.JavaVersion; + +public interface JavaVersionExtension { + JavaVersion getVersion(); + + void setVersion(JavaVersion version); +} diff --git a/build-logic/src/main/java/org/teavm/buildutil/JavaVersionPlugin.java b/build-logic/src/main/java/org/teavm/buildutil/JavaVersionPlugin.java new file mode 100644 index 000000000..21af41f5a --- /dev/null +++ b/build-logic/src/main/java/org/teavm/buildutil/JavaVersionPlugin.java @@ -0,0 +1,52 @@ +/* + * Copyright 2023 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.buildutil; + +import org.gradle.api.JavaVersion; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.plugins.JavaPluginExtension; + +public class JavaVersionPlugin implements Plugin { + @Override + public void apply(Project project) { + var ext = new ExtensionImpl(); + project.getExtensions().add(JavaVersionExtension.class, "javaVersion", ext); + project.afterEvaluate(p -> { + var java = p.getExtensions().findByType(JavaPluginExtension.class); + if (java != null) { + if (ext.version != null) { + java.setSourceCompatibility(ext.version); + java.setTargetCompatibility(ext.version); + } + } + }); + } + + private static class ExtensionImpl implements JavaVersionExtension { + private JavaVersion version = JavaVersion.VERSION_11; + + @Override + public JavaVersion getVersion() { + return version; + } + + @Override + public void setVersion(JavaVersion version) { + this.version = version; + } + } +} diff --git a/samples/settings.gradle.kts b/samples/settings.gradle.kts index 310db4faf..086c1f47b 100644 --- a/samples/settings.gradle.kts +++ b/samples/settings.gradle.kts @@ -84,10 +84,7 @@ gradle.afterProject { .findVersion("checkstyle").get().requiredVersion configDirectory.set(File(settings.rootDir, "../config/checkstyle")) } - java.toolchain { - if (!languageVersion.isPresent) { - languageVersion.set(JavaLanguageVersion.of(11)) - } - } + java.sourceCompatibility = JavaVersion.VERSION_11 + java.targetCompatibility = JavaVersion.VERSION_11 } } \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 6106ac00f..8b9bafb69 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -59,6 +59,10 @@ gradle.allprojects { version = teavmVersion } +gradle.allprojects { + apply(plugin = "javaVersion") +} + gradle.afterProject { val java = extensions.findByType() if (java != null) { @@ -67,11 +71,6 @@ gradle.afterProject { toolVersion = extensions.getByType().named("libs") .findVersion("checkstyle").get().requiredVersion } - java.toolchain { - if (!languageVersion.isPresent) { - languageVersion.set(JavaLanguageVersion.of(11)) - } - } } extensions.findByType()?.apply { diff --git a/tests/build.gradle.kts b/tests/build.gradle.kts index f9979c57e..e5abeaeb8 100644 --- a/tests/build.gradle.kts +++ b/tests/build.gradle.kts @@ -20,10 +20,8 @@ plugins { description = "Tests" -java { - toolchain { - languageVersion.set(JavaLanguageVersion.of(17)) - } +javaVersion { + version = JavaVersion.VERSION_17 } dependencies {