Skip to content

Commit ef7784e

Browse files
committed
Refactor Kotlin configuration in build
1 parent 222149c commit ef7784e

File tree

9 files changed

+126
-53
lines changed

9 files changed

+126
-53
lines changed

build.gradle

+2-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
plugins {
2-
id 'org.jetbrains.kotlin.jvm' version '1.7.0' apply false
3-
}
1+
description = "Spring for GraphQL"
42

53
ext {
64
moduleProjects = [project(":spring-graphql"), project(":spring-graphql-test")]
@@ -9,12 +7,8 @@ ext {
97
springBootVersion = "3.0.0"
108
}
119

12-
description = "Spring for GraphQL"
13-
1410
subprojects {
15-
apply plugin: 'org.springframework.graphql.compiler'
16-
apply plugin: 'org.springframework.graphql.deployment'
17-
11+
apply plugin: 'org.springframework.graphql.build.conventions'
1812
group = 'org.springframework.graphql'
1913

2014
repositories {
@@ -38,24 +32,6 @@ configure(moduleProjects) {
3832
}
3933
}
4034

41-
pluginManager.withPlugin("kotlin") {
42-
compileKotlin {
43-
kotlinOptions {
44-
jvmTarget = "17"
45-
languageVersion = "1.7"
46-
apiVersion = "1.7"
47-
freeCompilerArgs = ["-Xjsr305=strict", "-Xsuppress-version-warnings", "-opt-in=kotlin.RequiresOptIn"]
48-
allWarningsAsErrors = true
49-
}
50-
}
51-
compileTestKotlin {
52-
kotlinOptions {
53-
jvmTarget = "17"
54-
freeCompilerArgs = ["-Xjsr305=strict"]
55-
}
56-
}
57-
}
58-
5935
configurations {
6036
dependencyManagement {
6137
canBeConsumed = false

buildSrc/build.gradle

+14-12
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ plugins {
66
repositories {
77
mavenCentral()
88
gradlePluginPortal()
9-
maven { url "https://repo.spring.io/release" }
9+
}
10+
11+
ext {
12+
def propertiesFile = new File(new File("$projectDir").parentFile, "gradle.properties")
13+
propertiesFile.withInputStream {
14+
def properties = new Properties()
15+
properties.load(it)
16+
set("kotlinVersion", properties["kotlinVersion"])
17+
}
1018
}
1119

1220
dependencies {
1321
checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}"
22+
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
23+
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}")
1424
implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${javaFormatVersion}")
1525
}
1626

@@ -22,17 +32,9 @@ checkstyle {
2232

2333
gradlePlugin {
2434
plugins {
25-
formattingConventionsPlugin {
26-
id = "org.springframework.graphql.formatting"
27-
implementationClass = "org.springframework.graphql.build.format.FormattingConventionsPlugin"
28-
}
29-
compilerConventionsPlugin {
30-
id = "org.springframework.graphql.compiler"
31-
implementationClass = "org.springframework.graphql.build.compile.CompilerConventionsPlugin"
32-
}
33-
deploymentConventionsPlugin {
34-
id = "org.springframework.graphql.deployment"
35-
implementationClass = "org.springframework.graphql.build.deployment.DeploymentConventionsPlugin"
35+
conventionsPlugin {
36+
id = "org.springframework.graphql.build.conventions"
37+
implementationClass = "org.springframework.graphql.build.ConventionsPlugin"
3638
}
3739
}
3840
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2020-2023 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+
18+
package org.springframework.graphql.build;
19+
20+
import org.gradle.api.Plugin;
21+
import org.gradle.api.Project;
22+
import org.gradle.api.plugins.JavaBasePlugin;
23+
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
24+
25+
import org.springframework.graphql.build.conventions.DeploymentConventions;
26+
import org.springframework.graphql.build.conventions.JavaConventions;
27+
import org.springframework.graphql.build.conventions.KotlinConventions;
28+
29+
/**
30+
* Plugin to apply conventions to projects that are part of Spring Framework's build.
31+
* Conventions are applied in response to various plugins being applied.
32+
*
33+
* When the {@link JavaBasePlugin} is applied, the conventions in {@link JavaConventions}
34+
* are applied.
35+
* When the {@link KotlinBasePlugin} is applied, the conventions in {@link KotlinConventions}
36+
* are applied.
37+
* The conventions in {@link DeploymentConventions} apply and configure the {@link MavenPublishPlugin}.
38+
*
39+
* @author Brian Clozel
40+
*/
41+
public class ConventionsPlugin implements Plugin<Project> {
42+
43+
@Override
44+
public void apply(Project project) {
45+
new JavaConventions().apply(project);
46+
new KotlinConventions().apply(project);
47+
new DeploymentConventions().apply(project);
48+
}
49+
}
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.graphql.build.deployment;
17+
package org.springframework.graphql.build.conventions;
1818

19-
import org.gradle.api.Plugin;
2019
import org.gradle.api.Project;
2120
import org.gradle.api.publish.PublishingExtension;
2221
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
@@ -27,9 +26,8 @@
2726
*
2827
* @author Brian Clozel
2928
*/
30-
public class DeploymentConventionsPlugin implements Plugin<Project> {
29+
public class DeploymentConventions {
3130

32-
@Override
3331
public void apply(Project project) {
3432
project.getPlugins().apply(MavenPublishPlugin.class);
3533
project.getPlugins().withType(MavenPublishPlugin.class).forEach((mavenPublishPlugin) -> {

buildSrc/src/main/java/org/springframework/graphql/build/format/FormattingConventionsPlugin.java renamed to buildSrc/src/main/java/org/springframework/graphql/build/conventions/FormattingConventions.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,11 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.graphql.build.format;
17+
package org.springframework.graphql.build.conventions;
1818

1919
import io.spring.javaformat.gradle.FormatTask;
2020
import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
21-
import org.gradle.api.Plugin;
2221
import org.gradle.api.Project;
2322
import org.gradle.api.artifacts.DependencySet;
2423
import org.gradle.api.plugins.JavaBasePlugin;
@@ -32,9 +31,8 @@
3231
*
3332
* @author Brian Clozel
3433
*/
35-
public class FormattingConventionsPlugin implements Plugin<Project> {
34+
public class FormattingConventions {
3635

37-
@Override
3836
public void apply(Project project) {
3937
project.getPlugins().withType(JavaBasePlugin.class, (java) -> applySpringJavaFormat(project));
4038
}
+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2020-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.graphql.build.compile;
17+
package org.springframework.graphql.build.conventions;
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
@@ -33,7 +33,7 @@
3333
* @author Sam Brannen
3434
* @author Sebastien Deleuze
3535
*/
36-
public class CompilerConventionsPlugin implements Plugin<Project> {
36+
public class JavaConventions {
3737

3838
private static final List<String> COMPILER_ARGS;
3939

@@ -58,7 +58,6 @@ public class CompilerConventionsPlugin implements Plugin<Project> {
5858
"-Xlint:-deprecation", "-Xlint:-unchecked"));
5959
}
6060

61-
@Override
6261
public void apply(Project project) {
6362
project.getPlugins().withType(JavaLibraryPlugin.class, (javaPlugin) -> applyJavaCompileConventions(project));
6463
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2020-2023 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.graphql.build.conventions;
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+
* Convention for compiling Kotlin code.
28+
* @author Brian Clozel
29+
*/
30+
public class KotlinConventions {
31+
32+
public void apply(Project project) {
33+
project.getPlugins().withId("org.jetbrains.kotlin.jvm",
34+
(plugin) -> project.getTasks().withType(KotlinCompile.class, this::configure));
35+
}
36+
37+
private void configure(KotlinCompile compile) {
38+
KotlinJvmOptions kotlinOptions = compile.getKotlinOptions();
39+
kotlinOptions.setApiVersion("1.7");
40+
kotlinOptions.setLanguageVersion("1.7");
41+
kotlinOptions.setJvmTarget("17");
42+
kotlinOptions.setJavaParameters(true);
43+
kotlinOptions.setAllWarningsAsErrors(true);
44+
List<String> freeCompilerArgs = new ArrayList<>(compile.getKotlinOptions().getFreeCompilerArgs());
45+
freeCompilerArgs.addAll(List.of("-Xsuppress-version-warnings", "-Xjsr305=strict", "-opt-in=kotlin.RequiresOptIn"));
46+
compile.getKotlinOptions().setFreeCompilerArgs(freeCompilerArgs);
47+
}
48+
49+
}

gradle.properties

+2
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ org.gradle.daemon=true
55
org.gradle.parallel=true
66
org.gradle.jvmargs=-Dfile.encoding=UTF-8
77

8+
kotlinVersion=1.7.21
9+
810
kotlin.stdlib.default.dependency=false

platform/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies {
1616
api(platform("org.springframework.security:spring-security-bom:6.0.1"))
1717
api(platform("com.querydsl:querydsl-bom:5.0.0"))
1818
api(platform("io.rsocket:rsocket-bom:1.1.3"))
19-
api(platform("org.jetbrains.kotlin:kotlin-bom:1.7.10"))
19+
api(platform("org.jetbrains.kotlin:kotlin-bom:${kotlinVersion}"))
2020
api(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.4"))
2121
api(platform("org.junit:junit-bom:5.9.1"))
2222
api(platform("org.mockito:mockito-bom:4.8.1"))

0 commit comments

Comments
 (0)