Move Kotlin Gradle build config to convention

This commit moves the Kotlin build configuration from the Gradle DSL
to a dedicated convention in buildSrc.
This commit is contained in:
Brian Clozel 2022-11-02 10:59:39 +01:00
parent acd9016fc3
commit 21414bc265
7 changed files with 75 additions and 29 deletions

View File

@ -1,8 +1,8 @@
plugins { plugins {
id 'io.spring.nohttp' version '0.0.10' id 'io.spring.nohttp' version '0.0.10'
id 'io.freefair.aspectj' version '6.5.0.3' apply false id 'io.freefair.aspectj' version '6.5.0.3' apply false
id 'org.jetbrains.kotlin.jvm' version '1.7.20' apply false // kotlinVersion is managed in gradle.properties
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.20' apply false id 'org.jetbrains.kotlin.plugin.serialization' version "${kotlinVersion}" apply false
id 'org.jetbrains.dokka' version '1.7.20' id 'org.jetbrains.dokka' version '1.7.20'
id 'org.asciidoctor.jvm.convert' version '3.3.2' apply false id 'org.asciidoctor.jvm.convert' version '3.3.2' apply false
id 'org.asciidoctor.jvm.pdf' version '3.3.2' apply false id 'org.asciidoctor.jvm.pdf' version '3.3.2' apply false
@ -16,9 +16,6 @@ plugins {
ext { ext {
moduleProjects = subprojects.findAll { it.name.startsWith("spring-") } moduleProjects = subprojects.findAll { it.name.startsWith("spring-") }
javaProjects = subprojects - project(":framework-bom") - project(":framework-platform") javaProjects = subprojects - project(":framework-bom") - project(":framework-platform")
withoutJclOverSlf4j = {
exclude group: "org.slf4j", name: "jcl-over-slf4j"
}
} }
configure(allprojects) { project -> configure(allprojects) { project ->
@ -59,23 +56,6 @@ configure([rootProject] + javaProjects) { project ->
matching { it.name.endsWith("Classpath") }.all { it.extendsFrom(dependencyManagement) } matching { it.name.endsWith("Classpath") }.all { it.extendsFrom(dependencyManagement) }
} }
pluginManager.withPlugin("kotlin") {
compileKotlin {
kotlinOptions {
languageVersion = "1.7"
apiVersion = "1.7"
freeCompilerArgs = ["-Xjsr305=strict", "-Xsuppress-version-warnings", "-opt-in=kotlin.RequiresOptIn"]
allWarningsAsErrors = true
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xjsr305=strict", "-opt-in=kotlin.RequiresOptIn"]
}
}
}
test { test {
useJUnitPlatform() useJUnitPlatform()
include(["**/*Tests.class", "**/*Test.class"]) include(["**/*Tests.class", "**/*Test.class"])
@ -165,7 +145,6 @@ configure(moduleProjects) { project ->
configure(rootProject) { configure(rootProject) {
description = "Spring Framework" description = "Spring Framework"
apply plugin: "kotlin"
apply plugin: "io.spring.nohttp" apply plugin: "io.spring.nohttp"
apply plugin: 'org.springframework.build.api-diff' apply plugin: 'org.springframework.build.api-diff'
@ -188,4 +167,4 @@ configure(rootProject) {
maxHeapSize = "1g" maxHeapSize = "1g"
} }
} }

View File

@ -7,7 +7,8 @@ They are declared in the `build.gradle` file in this folder.
The `org.springframework.build.conventions` plugin applies all conventions to the Framework build: The `org.springframework.build.conventions` plugin applies all conventions to the Framework build:
* Configuring the Java compiler, see `CompilerConventions` * Configuring the Java compiler, see `JavaConventions`
* Configuring the Kotlin compiler, see `KotlinConventions`
* Configuring testing in the build with `TestConventions` * Configuring testing in the build with `TestConventions`

View File

@ -7,7 +7,18 @@ repositories {
gradlePluginPortal() gradlePluginPortal()
} }
ext {
def propertiesFile = new File(new File("$projectDir").parentFile, "gradle.properties")
propertiesFile.withInputStream {
def properties = new Properties()
properties.load(it)
set("kotlinVersion", properties["kotlinVersion"])
}
}
dependencies { dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
implementation "me.champeau.gradle:japicmp-gradle-plugin:0.3.0" implementation "me.champeau.gradle:japicmp-gradle-plugin:0.3.0"
implementation "org.gradle:test-retry-gradle-plugin:1.4.1" implementation "org.gradle:test-retry-gradle-plugin:1.4.1"
} }

View File

@ -19,6 +19,7 @@ package org.springframework.build;
import org.gradle.api.Plugin; import org.gradle.api.Plugin;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.plugins.JavaBasePlugin; import org.gradle.api.plugins.JavaBasePlugin;
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin;
/** /**
* Plugin to apply conventions to projects that are part of Spring Framework's build. * Plugin to apply conventions to projects that are part of Spring Framework's build.
@ -26,7 +27,9 @@ import org.gradle.api.plugins.JavaBasePlugin;
* *
* When the {@link JavaBasePlugin} is applied, the conventions in {@link TestConventions} * When the {@link JavaBasePlugin} is applied, the conventions in {@link TestConventions}
* are applied. * are applied.
* When the {@link JavaBasePlugin} is applied, the conventions in {@link CompilerConventions} * When the {@link JavaBasePlugin} is applied, the conventions in {@link JavaConventions}
* are applied.
* When the {@link KotlinBasePlugin} is applied, the conventions in {@link KotlinConventions}
* are applied. * are applied.
* *
* @author Brian Clozel * @author Brian Clozel
@ -35,7 +38,8 @@ public class ConventionsPlugin implements Plugin<Project> {
@Override @Override
public void apply(Project project) { public void apply(Project project) {
new JavaConventions().apply(project);
new KotlinConventions().apply(project);
new TestConventions().apply(project); new TestConventions().apply(project);
new CompilerConventions().apply(project);
} }
} }

View File

@ -33,7 +33,7 @@ import org.gradle.api.tasks.compile.JavaCompile;
* @author Sam Brannen * @author Sam Brannen
* @author Sebastien Deleuze * @author Sebastien Deleuze
*/ */
public class CompilerConventions { public class JavaConventions {
private static final List<String> COMPILER_ARGS; private static final List<String> COMPILER_ARGS;

View File

@ -0,0 +1,47 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* 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
*
* https://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.springframework.build;
import java.util.ArrayList;
import java.util.List;
import org.gradle.api.Project;
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions;
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile;
/**
* @author Brian Clozel
*/
public class KotlinConventions {
void apply(Project project) {
project.getPlugins().withId("org.jetbrains.kotlin.jvm",
(plugin) -> project.getTasks().withType(KotlinCompile.class, this::configure));
}
private void configure(KotlinCompile compile) {
KotlinJvmOptions kotlinOptions = compile.getKotlinOptions();
kotlinOptions.setApiVersion("1.7");
kotlinOptions.setLanguageVersion("1.7");
kotlinOptions.setJvmTarget("17");
kotlinOptions.setAllWarningsAsErrors(true);
List<String> freeCompilerArgs = new ArrayList<>(compile.getKotlinOptions().getFreeCompilerArgs());
freeCompilerArgs.addAll(List.of("-Xsuppress-version-warnings", "-Xjsr305=strict", "-opt-in=kotlin.RequiresOptIn"));
compile.getKotlinOptions().setFreeCompilerArgs(freeCompilerArgs);
}
}

View File

@ -1,5 +1,9 @@
version=6.0.0-SNAPSHOT version=6.0.0-SNAPSHOT
org.gradle.jvmargs=-Xmx2048m
org.gradle.caching=true org.gradle.caching=true
org.gradle.jvmargs=-Xmx2048m
org.gradle.parallel=true org.gradle.parallel=true
kotlinVersion=1.7.20
kotlin.stdlib.default.dependency=false kotlin.stdlib.default.dependency=false