Don't use Gradle toolchains to specify Java version

This commit is contained in:
Alexey Andreev 2023-02-01 18:40:53 +01:00
parent 630fae3093
commit 8800e7153b
6 changed files with 89 additions and 17 deletions

View File

@ -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()

View File

@ -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);
}

View File

@ -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<Project> {
@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;
}
}
}

View File

@ -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
}
}

View File

@ -59,6 +59,10 @@ gradle.allprojects {
version = teavmVersion
}
gradle.allprojects {
apply(plugin = "javaVersion")
}
gradle.afterProject {
val java = extensions.findByType<JavaPluginExtension>()
if (java != null) {
@ -67,11 +71,6 @@ gradle.afterProject {
toolVersion = extensions.getByType<VersionCatalogsExtension>().named("libs")
.findVersion("checkstyle").get().requiredVersion
}
java.toolchain {
if (!languageVersion.isPresent) {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
}
extensions.findByType<PublishingExtension>()?.apply {

View File

@ -20,10 +20,8 @@ plugins {
description = "Tests"
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
javaVersion {
version = JavaVersion.VERSION_17
}
dependencies {