From ef7784ee1ca1bcb052e8b38819001bc357186b14 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 11 Jan 2023 10:04:02 +0100 Subject: [PATCH] Refactor Kotlin configuration in build --- build.gradle | 28 +---------- buildSrc/build.gradle | 26 +++++----- .../graphql/build/ConventionsPlugin.java | 49 +++++++++++++++++++ .../DeploymentConventions.java} | 8 ++- .../FormattingConventions.java} | 8 ++- .../JavaConventions.java} | 7 ++- .../build/conventions/KotlinConventions.java | 49 +++++++++++++++++++ gradle.properties | 2 + platform/build.gradle | 2 +- 9 files changed, 126 insertions(+), 53 deletions(-) create mode 100644 buildSrc/src/main/java/org/springframework/graphql/build/ConventionsPlugin.java rename buildSrc/src/main/java/org/springframework/graphql/build/{deployment/DeploymentConventionsPlugin.java => conventions/DeploymentConventions.java} (86%) rename buildSrc/src/main/java/org/springframework/graphql/build/{format/FormattingConventionsPlugin.java => conventions/FormattingConventions.java} (90%) rename buildSrc/src/main/java/org/springframework/graphql/build/{compile/CompilerConventionsPlugin.java => conventions/JavaConventions.java} (94%) create mode 100644 buildSrc/src/main/java/org/springframework/graphql/build/conventions/KotlinConventions.java diff --git a/build.gradle b/build.gradle index d0e15d06..5999bda6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,4 @@ -plugins { - id 'org.jetbrains.kotlin.jvm' version '1.7.0' apply false -} +description = "Spring for GraphQL" ext { moduleProjects = [project(":spring-graphql"), project(":spring-graphql-test")] @@ -9,12 +7,8 @@ ext { springBootVersion = "3.0.0" } -description = "Spring for GraphQL" - subprojects { - apply plugin: 'org.springframework.graphql.compiler' - apply plugin: 'org.springframework.graphql.deployment' - + apply plugin: 'org.springframework.graphql.build.conventions' group = 'org.springframework.graphql' repositories { @@ -38,24 +32,6 @@ configure(moduleProjects) { } } - pluginManager.withPlugin("kotlin") { - compileKotlin { - kotlinOptions { - jvmTarget = "17" - languageVersion = "1.7" - apiVersion = "1.7" - freeCompilerArgs = ["-Xjsr305=strict", "-Xsuppress-version-warnings", "-opt-in=kotlin.RequiresOptIn"] - allWarningsAsErrors = true - } - } - compileTestKotlin { - kotlinOptions { - jvmTarget = "17" - freeCompilerArgs = ["-Xjsr305=strict"] - } - } - } - configurations { dependencyManagement { canBeConsumed = false diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index d3439996..cbedf431 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -6,11 +6,21 @@ plugins { repositories { mavenCentral() gradlePluginPortal() - maven { url "https://repo.spring.io/release" } +} + +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 { checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}" + implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}") + implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:${kotlinVersion}") implementation("io.spring.javaformat:spring-javaformat-gradle-plugin:${javaFormatVersion}") } @@ -22,17 +32,9 @@ checkstyle { gradlePlugin { plugins { - formattingConventionsPlugin { - id = "org.springframework.graphql.formatting" - implementationClass = "org.springframework.graphql.build.format.FormattingConventionsPlugin" - } - compilerConventionsPlugin { - id = "org.springframework.graphql.compiler" - implementationClass = "org.springframework.graphql.build.compile.CompilerConventionsPlugin" - } - deploymentConventionsPlugin { - id = "org.springframework.graphql.deployment" - implementationClass = "org.springframework.graphql.build.deployment.DeploymentConventionsPlugin" + conventionsPlugin { + id = "org.springframework.graphql.build.conventions" + implementationClass = "org.springframework.graphql.build.ConventionsPlugin" } } } diff --git a/buildSrc/src/main/java/org/springframework/graphql/build/ConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/graphql/build/ConventionsPlugin.java new file mode 100644 index 00000000..59da5043 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/graphql/build/ConventionsPlugin.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020-2023 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.graphql.build; + +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; + +import org.springframework.graphql.build.conventions.DeploymentConventions; +import org.springframework.graphql.build.conventions.JavaConventions; +import org.springframework.graphql.build.conventions.KotlinConventions; + +/** + * Plugin to apply conventions to projects that are part of Spring Framework's build. + * Conventions are applied in response to various plugins being applied. + * + * 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. + * The conventions in {@link DeploymentConventions} apply and configure the {@link MavenPublishPlugin}. + * + * @author Brian Clozel + */ +public class ConventionsPlugin implements Plugin { + + @Override + public void apply(Project project) { + new JavaConventions().apply(project); + new KotlinConventions().apply(project); + new DeploymentConventions().apply(project); + } +} diff --git a/buildSrc/src/main/java/org/springframework/graphql/build/deployment/DeploymentConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/graphql/build/conventions/DeploymentConventions.java similarity index 86% rename from buildSrc/src/main/java/org/springframework/graphql/build/deployment/DeploymentConventionsPlugin.java rename to buildSrc/src/main/java/org/springframework/graphql/build/conventions/DeploymentConventions.java index 0329893b..3f2cd321 100644 --- a/buildSrc/src/main/java/org/springframework/graphql/build/deployment/DeploymentConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/graphql/build/conventions/DeploymentConventions.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2022 the original author or authors. + * Copyright 2020-2023 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. @@ -14,9 +14,8 @@ * limitations under the License. */ -package org.springframework.graphql.build.deployment; +package org.springframework.graphql.build.conventions; -import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.publish.PublishingExtension; import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; @@ -27,9 +26,8 @@ import org.gradle.api.publish.maven.plugins.MavenPublishPlugin; * * @author Brian Clozel */ -public class DeploymentConventionsPlugin implements Plugin { +public class DeploymentConventions { - @Override public void apply(Project project) { project.getPlugins().apply(MavenPublishPlugin.class); project.getPlugins().withType(MavenPublishPlugin.class).forEach((mavenPublishPlugin) -> { diff --git a/buildSrc/src/main/java/org/springframework/graphql/build/format/FormattingConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/graphql/build/conventions/FormattingConventions.java similarity index 90% rename from buildSrc/src/main/java/org/springframework/graphql/build/format/FormattingConventionsPlugin.java rename to buildSrc/src/main/java/org/springframework/graphql/build/conventions/FormattingConventions.java index 9623c211..55471f46 100644 --- a/buildSrc/src/main/java/org/springframework/graphql/build/format/FormattingConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/graphql/build/conventions/FormattingConventions.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 the original author or authors. + * Copyright 2020-2023 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. @@ -14,11 +14,10 @@ * limitations under the License. */ -package org.springframework.graphql.build.format; +package org.springframework.graphql.build.conventions; import io.spring.javaformat.gradle.FormatTask; import io.spring.javaformat.gradle.SpringJavaFormatPlugin; -import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.artifacts.DependencySet; import org.gradle.api.plugins.JavaBasePlugin; @@ -32,9 +31,8 @@ import org.gradle.api.plugins.quality.CheckstylePlugin; * * @author Brian Clozel */ -public class FormattingConventionsPlugin implements Plugin { +public class FormattingConventions { - @Override public void apply(Project project) { project.getPlugins().withType(JavaBasePlugin.class, (java) -> applySpringJavaFormat(project)); } diff --git a/buildSrc/src/main/java/org/springframework/graphql/build/compile/CompilerConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/graphql/build/conventions/JavaConventions.java similarity index 94% rename from buildSrc/src/main/java/org/springframework/graphql/build/compile/CompilerConventionsPlugin.java rename to buildSrc/src/main/java/org/springframework/graphql/build/conventions/JavaConventions.java index d6c68fb6..b78cd3d9 100644 --- a/buildSrc/src/main/java/org/springframework/graphql/build/compile/CompilerConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/graphql/build/conventions/JavaConventions.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2020-2023 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.graphql.build.compile; +package org.springframework.graphql.build.conventions; import java.util.ArrayList; import java.util.Arrays; @@ -33,7 +33,7 @@ import org.gradle.api.tasks.compile.JavaCompile; * @author Sam Brannen * @author Sebastien Deleuze */ -public class CompilerConventionsPlugin implements Plugin { +public class JavaConventions { private static final List COMPILER_ARGS; @@ -58,7 +58,6 @@ public class CompilerConventionsPlugin implements Plugin { "-Xlint:-deprecation", "-Xlint:-unchecked")); } - @Override public void apply(Project project) { project.getPlugins().withType(JavaLibraryPlugin.class, (javaPlugin) -> applyJavaCompileConventions(project)); } diff --git a/buildSrc/src/main/java/org/springframework/graphql/build/conventions/KotlinConventions.java b/buildSrc/src/main/java/org/springframework/graphql/build/conventions/KotlinConventions.java new file mode 100644 index 00000000..b503eb92 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/graphql/build/conventions/KotlinConventions.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020-2023 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.graphql.build.conventions; + +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; + +/** + * Convention for compiling Kotlin code. + * @author Brian Clozel + */ +public class KotlinConventions { + + public 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.setJavaParameters(true); + kotlinOptions.setAllWarningsAsErrors(true); + List freeCompilerArgs = new ArrayList<>(compile.getKotlinOptions().getFreeCompilerArgs()); + freeCompilerArgs.addAll(List.of("-Xsuppress-version-warnings", "-Xjsr305=strict", "-opt-in=kotlin.RequiresOptIn")); + compile.getKotlinOptions().setFreeCompilerArgs(freeCompilerArgs); + } + +} diff --git a/gradle.properties b/gradle.properties index adca0c3e..a96a10a4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,4 +5,6 @@ org.gradle.daemon=true org.gradle.parallel=true org.gradle.jvmargs=-Dfile.encoding=UTF-8 +kotlinVersion=1.7.21 + kotlin.stdlib.default.dependency=false diff --git a/platform/build.gradle b/platform/build.gradle index c16e235e..213371b6 100644 --- a/platform/build.gradle +++ b/platform/build.gradle @@ -16,7 +16,7 @@ dependencies { api(platform("org.springframework.security:spring-security-bom:6.0.1")) api(platform("com.querydsl:querydsl-bom:5.0.0")) api(platform("io.rsocket:rsocket-bom:1.1.3")) - api(platform("org.jetbrains.kotlin:kotlin-bom:1.7.10")) + api(platform("org.jetbrains.kotlin:kotlin-bom:${kotlinVersion}")) api(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.4")) api(platform("org.junit:junit-bom:5.9.1")) api(platform("org.mockito:mockito-bom:4.8.1"))