Skip to content

Commit 21414bc

Browse files
committed
Move Kotlin Gradle build config to convention
This commit moves the Kotlin build configuration from the Gradle DSL to a dedicated convention in buildSrc.
1 parent acd9016 commit 21414bc

File tree

7 files changed

+75
-29
lines changed

7 files changed

+75
-29
lines changed

build.gradle

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
plugins {
22
id 'io.spring.nohttp' version '0.0.10'
33
id 'io.freefair.aspectj' version '6.5.0.3' apply false
4-
id 'org.jetbrains.kotlin.jvm' version '1.7.20' apply false
5-
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.20' apply false
4+
// kotlinVersion is managed in gradle.properties
5+
id 'org.jetbrains.kotlin.plugin.serialization' version "${kotlinVersion}" apply false
66
id 'org.jetbrains.dokka' version '1.7.20'
77
id 'org.asciidoctor.jvm.convert' version '3.3.2' apply false
88
id 'org.asciidoctor.jvm.pdf' version '3.3.2' apply false
@@ -16,9 +16,6 @@ plugins {
1616
ext {
1717
moduleProjects = subprojects.findAll { it.name.startsWith("spring-") }
1818
javaProjects = subprojects - project(":framework-bom") - project(":framework-platform")
19-
withoutJclOverSlf4j = {
20-
exclude group: "org.slf4j", name: "jcl-over-slf4j"
21-
}
2219
}
2320

2421
configure(allprojects) { project ->
@@ -59,23 +56,6 @@ configure([rootProject] + javaProjects) { project ->
5956
matching { it.name.endsWith("Classpath") }.all { it.extendsFrom(dependencyManagement) }
6057
}
6158

62-
pluginManager.withPlugin("kotlin") {
63-
64-
compileKotlin {
65-
kotlinOptions {
66-
languageVersion = "1.7"
67-
apiVersion = "1.7"
68-
freeCompilerArgs = ["-Xjsr305=strict", "-Xsuppress-version-warnings", "-opt-in=kotlin.RequiresOptIn"]
69-
allWarningsAsErrors = true
70-
}
71-
}
72-
compileTestKotlin {
73-
kotlinOptions {
74-
freeCompilerArgs = ["-Xjsr305=strict", "-opt-in=kotlin.RequiresOptIn"]
75-
}
76-
}
77-
}
78-
7959
test {
8060
useJUnitPlatform()
8161
include(["**/*Tests.class", "**/*Test.class"])
@@ -165,7 +145,6 @@ configure(moduleProjects) { project ->
165145
configure(rootProject) {
166146
description = "Spring Framework"
167147

168-
apply plugin: "kotlin"
169148
apply plugin: "io.spring.nohttp"
170149
apply plugin: 'org.springframework.build.api-diff'
171150

@@ -188,4 +167,4 @@ configure(rootProject) {
188167
maxHeapSize = "1g"
189168
}
190169

191-
}
170+
}

buildSrc/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ They are declared in the `build.gradle` file in this folder.
77

88
The `org.springframework.build.conventions` plugin applies all conventions to the Framework build:
99

10-
* Configuring the Java compiler, see `CompilerConventions`
10+
* Configuring the Java compiler, see `JavaConventions`
11+
* Configuring the Kotlin compiler, see `KotlinConventions`
1112
* Configuring testing in the build with `TestConventions`
1213

1314

buildSrc/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ repositories {
77
gradlePluginPortal()
88
}
99

10+
ext {
11+
def propertiesFile = new File(new File("$projectDir").parentFile, "gradle.properties")
12+
propertiesFile.withInputStream {
13+
def properties = new Properties()
14+
properties.load(it)
15+
set("kotlinVersion", properties["kotlinVersion"])
16+
}
17+
}
18+
1019
dependencies {
20+
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
21+
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
1122
implementation "me.champeau.gradle:japicmp-gradle-plugin:0.3.0"
1223
implementation "org.gradle:test-retry-gradle-plugin:1.4.1"
1324
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
import org.gradle.api.Plugin;
2020
import org.gradle.api.Project;
2121
import org.gradle.api.plugins.JavaBasePlugin;
22+
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin;
2223

2324
/**
2425
* Plugin to apply conventions to projects that are part of Spring Framework's build.
2526
* Conventions are applied in response to various plugins being applied.
2627
*
2728
* When the {@link JavaBasePlugin} is applied, the conventions in {@link TestConventions}
2829
* are applied.
29-
* When the {@link JavaBasePlugin} is applied, the conventions in {@link CompilerConventions}
30+
* When the {@link JavaBasePlugin} is applied, the conventions in {@link JavaConventions}
31+
* are applied.
32+
* When the {@link KotlinBasePlugin} is applied, the conventions in {@link KotlinConventions}
3033
* are applied.
3134
*
3235
* @author Brian Clozel
@@ -35,7 +38,8 @@ public class ConventionsPlugin implements Plugin<Project> {
3538

3639
@Override
3740
public void apply(Project project) {
41+
new JavaConventions().apply(project);
42+
new KotlinConventions().apply(project);
3843
new TestConventions().apply(project);
39-
new CompilerConventions().apply(project);
4044
}
4145
}

buildSrc/src/main/java/org/springframework/build/CompilerConventions.java renamed to buildSrc/src/main/java/org/springframework/build/JavaConventions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @author Sam Brannen
3434
* @author Sebastien Deleuze
3535
*/
36-
public class CompilerConventions {
36+
public class JavaConventions {
3737

3838
private static final List<String> COMPILER_ARGS;
3939

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.build;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.gradle.api.Project;
23+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions;
24+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile;
25+
26+
/**
27+
* @author Brian Clozel
28+
*/
29+
public class KotlinConventions {
30+
31+
void apply(Project project) {
32+
project.getPlugins().withId("org.jetbrains.kotlin.jvm",
33+
(plugin) -> project.getTasks().withType(KotlinCompile.class, this::configure));
34+
}
35+
36+
private void configure(KotlinCompile compile) {
37+
KotlinJvmOptions kotlinOptions = compile.getKotlinOptions();
38+
kotlinOptions.setApiVersion("1.7");
39+
kotlinOptions.setLanguageVersion("1.7");
40+
kotlinOptions.setJvmTarget("17");
41+
kotlinOptions.setAllWarningsAsErrors(true);
42+
List<String> freeCompilerArgs = new ArrayList<>(compile.getKotlinOptions().getFreeCompilerArgs());
43+
freeCompilerArgs.addAll(List.of("-Xsuppress-version-warnings", "-Xjsr305=strict", "-opt-in=kotlin.RequiresOptIn"));
44+
compile.getKotlinOptions().setFreeCompilerArgs(freeCompilerArgs);
45+
}
46+
47+
}

gradle.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
version=6.0.0-SNAPSHOT
2-
org.gradle.jvmargs=-Xmx2048m
2+
33
org.gradle.caching=true
4+
org.gradle.jvmargs=-Xmx2048m
45
org.gradle.parallel=true
6+
7+
kotlinVersion=1.7.20
8+
59
kotlin.stdlib.default.dependency=false

0 commit comments

Comments
 (0)