Browse Source

Move Kotlin Gradle build config to convention

This commit moves the Kotlin build configuration from the Gradle DSL
to a dedicated convention in buildSrc.
pull/29167/head
Brian Clozel 2 years ago
parent
commit
21414bc265
  1. 27
      build.gradle
  2. 3
      buildSrc/README.md
  3. 11
      buildSrc/build.gradle
  4. 8
      buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java
  5. 2
      buildSrc/src/main/java/org/springframework/build/JavaConventions.java
  6. 47
      buildSrc/src/main/java/org/springframework/build/KotlinConventions.java
  7. 6
      gradle.properties

27
build.gradle

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
plugins {
id 'io.spring.nohttp' version '0.0.10'
id 'io.freefair.aspectj' version '6.5.0.3' apply false
id 'org.jetbrains.kotlin.jvm' version '1.7.20' apply false
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.20' apply false
// kotlinVersion is managed in gradle.properties
id 'org.jetbrains.kotlin.plugin.serialization' version "${kotlinVersion}" apply false
id 'org.jetbrains.dokka' version '1.7.20'
id 'org.asciidoctor.jvm.convert' version '3.3.2' apply false
id 'org.asciidoctor.jvm.pdf' version '3.3.2' apply false
@ -16,9 +16,6 @@ plugins { @@ -16,9 +16,6 @@ plugins {
ext {
moduleProjects = subprojects.findAll { it.name.startsWith("spring-") }
javaProjects = subprojects - project(":framework-bom") - project(":framework-platform")
withoutJclOverSlf4j = {
exclude group: "org.slf4j", name: "jcl-over-slf4j"
}
}
configure(allprojects) { project ->
@ -59,23 +56,6 @@ configure([rootProject] + javaProjects) { project -> @@ -59,23 +56,6 @@ configure([rootProject] + javaProjects) { project ->
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 {
useJUnitPlatform()
include(["**/*Tests.class", "**/*Test.class"])
@ -165,7 +145,6 @@ configure(moduleProjects) { project -> @@ -165,7 +145,6 @@ configure(moduleProjects) { project ->
configure(rootProject) {
description = "Spring Framework"
apply plugin: "kotlin"
apply plugin: "io.spring.nohttp"
apply plugin: 'org.springframework.build.api-diff'
@ -188,4 +167,4 @@ configure(rootProject) { @@ -188,4 +167,4 @@ configure(rootProject) {
maxHeapSize = "1g"
}
}
}

3
buildSrc/README.md

@ -7,7 +7,8 @@ They are declared in the `build.gradle` file in this folder. @@ -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:
* 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`

11
buildSrc/build.gradle

@ -7,7 +7,18 @@ repositories { @@ -7,7 +7,18 @@ repositories {
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 {
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 "org.gradle:test-retry-gradle-plugin:1.4.1"
}

8
buildSrc/src/main/java/org/springframework/build/ConventionsPlugin.java

@ -19,6 +19,7 @@ package org.springframework.build; @@ -19,6 +19,7 @@ package org.springframework.build;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
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.
@ -26,7 +27,9 @@ import org.gradle.api.plugins.JavaBasePlugin; @@ -26,7 +27,9 @@ import org.gradle.api.plugins.JavaBasePlugin;
*
* When the {@link JavaBasePlugin} is applied, the conventions in {@link TestConventions}
* 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.
*
* @author Brian Clozel
@ -35,7 +38,8 @@ public class ConventionsPlugin implements Plugin<Project> { @@ -35,7 +38,8 @@ public class ConventionsPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
new JavaConventions().apply(project);
new KotlinConventions().apply(project);
new TestConventions().apply(project);
new CompilerConventions().apply(project);
}
}

2
buildSrc/src/main/java/org/springframework/build/CompilerConventions.java → buildSrc/src/main/java/org/springframework/build/JavaConventions.java

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

47
buildSrc/src/main/java/org/springframework/build/KotlinConventions.java

@ -0,0 +1,47 @@ @@ -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);
}
}

6
gradle.properties

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

Loading…
Cancel
Save