Refactor Kotlin configuration in build

This commit is contained in:
Brian Clozel
2023-01-11 10:04:02 +01:00
parent 222149c142
commit ef7784ee1c
9 changed files with 126 additions and 53 deletions

View File

@@ -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

View File

@@ -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"
}
}
}

View File

@@ -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<Project> {
@Override
public void apply(Project project) {
new JavaConventions().apply(project);
new KotlinConventions().apply(project);
new DeploymentConventions().apply(project);
}
}

View File

@@ -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<Project> {
public class DeploymentConventions {
@Override
public void apply(Project project) {
project.getPlugins().apply(MavenPublishPlugin.class);
project.getPlugins().withType(MavenPublishPlugin.class).forEach((mavenPublishPlugin) -> {

View File

@@ -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<Project> {
public class FormattingConventions {
@Override
public void apply(Project project) {
project.getPlugins().withType(JavaBasePlugin.class, (java) -> applySpringJavaFormat(project));
}

View File

@@ -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<Project> {
public class JavaConventions {
private static final List<String> COMPILER_ARGS;
@@ -58,7 +58,6 @@ public class CompilerConventionsPlugin implements Plugin<Project> {
"-Xlint:-deprecation", "-Xlint:-unchecked"));
}
@Override
public void apply(Project project) {
project.getPlugins().withType(JavaLibraryPlugin.class, (javaPlugin) -> applyJavaCompileConventions(project));
}

View File

@@ -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<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

@@ -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

View File

@@ -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"))