Skip to content

Commit

Permalink
Use non-deprecated Kotlin DSL for Kotlin compile options
Browse files Browse the repository at this point in the history
Closes gh-1495
  • Loading branch information
mhalbritter committed Jun 4, 2024
1 parent 68cc5f6 commit 98bbc9b
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 353 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand All @@ -16,36 +16,57 @@

package io.spring.initializr.generator.spring.code.kotlin;

import java.util.List;

import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
import io.spring.initializr.generator.spring.build.BuildCustomizer;

import org.springframework.util.CollectionUtils;

/**
* {@link BuildCustomizer} abstraction for Kotlin projects build with Gradle.
* {@link BuildCustomizer} for Kotlin projects build with Gradle.
*
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
* @see GroovyDslKotlinGradleBuildCustomizer
* @see KotlinDslKotlinGradleBuildCustomizer
* @author Moritz Halbritter
*/
abstract class KotlinGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {
class KotlinGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {

private final KotlinProjectSettings settings;

KotlinGradleBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) {
private final char quote;

KotlinGradleBuildCustomizer(KotlinProjectSettings kotlinProjectSettings, char quote) {
this.settings = kotlinProjectSettings;
this.quote = quote;
}

@Override
public void customize(GradleBuild build) {
build.plugins().add("org.jetbrains.kotlin.jvm", (plugin) -> plugin.setVersion(this.settings.getVersion()));
build.plugins()
.add("org.jetbrains.kotlin.plugin.spring", (plugin) -> plugin.setVersion(this.settings.getVersion()));
build.tasks()
.customizeWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
(compile) -> customizeKotlinOptions(this.settings, compile));
customizeCompilerOptions(build);
}

private void customizeCompilerOptions(GradleBuild build) {
build.extensions().customize("kotlin", (kotlin) -> kotlin.nested("compilerOptions", (compilerOptions) -> {
compilerOptions.attributeWithType("jvmTarget", getJvmTarget(), "org.jetbrains.kotlin.gradle.dsl.JvmTarget");
if (!CollectionUtils.isEmpty(this.settings.getCompilerArgs())) {
compilerOptions.invoke("freeCompilerArgs.addAll", quote(this.settings.getCompilerArgs()));
}
}));
}

private List<String> quote(List<String> compilerArgs) {
return compilerArgs.stream().map((element) -> this.quote + element + this.quote).toList();
}

protected abstract void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile);
private String getJvmTarget() {
if ("1.8".equals(this.settings.getJvmTarget())) {
return "JvmTarget.JVM_1_8";
}
return "JvmTarget.JVM_" + this.settings.getJvmTarget();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
Expand Down Expand Up @@ -46,6 +46,7 @@
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Jean-Baptiste Nizet
* @author Moritz Halbritter
*/
@Configuration
class KotlinProjectGenerationDefaultContributorsConfiguration {
Expand Down Expand Up @@ -78,6 +79,18 @@ KotlinJpaMavenBuildCustomizer kotlinJpaMavenBuildCustomizer(InitializrMetadata m
return new KotlinJpaMavenBuildCustomizer(metadata);
}

@Bean
@ConditionalOnBuildSystem(value = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_KOTLIN)
KotlinGradleBuildCustomizer kotlinBuildCustomizerKotlinDsl(KotlinProjectSettings kotlinProjectSettings) {
return new KotlinGradleBuildCustomizer(kotlinProjectSettings, '\"');
}

@Bean
@ConditionalOnBuildSystem(value = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_GROOVY)
KotlinGradleBuildCustomizer kotlinBuildCustomizerGroovyDsl(KotlinProjectSettings kotlinProjectSettings) {
return new KotlinGradleBuildCustomizer(kotlinProjectSettings, '\'');
}

/**
* Configuration for Kotlin projects using Spring Boot 2.0 and later.
*/
Expand Down Expand Up @@ -126,36 +139,4 @@ ServletInitializerCustomizer<KotlinTypeDeclaration> javaServletInitializerCustom

}

/**
* Configuration for Kotlin projects built with Gradle (Groovy DSL).
*
* @author Andy Wilkinson
*/
@Configuration
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_GROOVY)
static class KotlinGradleProjectConfiguration {

@Bean
KotlinGradleBuildCustomizer kotlinBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) {
return new GroovyDslKotlinGradleBuildCustomizer(kotlinProjectSettings);
}

}

/**
* Configuration for Kotlin projects built with Gradle (Kotlin DSL).
*
* @author Jean-Baptiste Nizet
*/
@Configuration
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_KOTLIN)
static class KotlinGradleKtsProjectConfiguration {

@Bean
KotlinDslKotlinGradleBuildCustomizer kotlinBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) {
return new KotlinDslKotlinGradleBuildCustomizer(kotlinProjectSettings);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.spring.initializr.generator.spring.code.kotlin;

import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -44,7 +43,7 @@ public interface KotlinProjectSettings {
* @return the compiler arguments
*/
default List<String> getCompilerArgs() {
return Collections.singletonList("-Xjsr305=strict");
return List.of("-Xjsr305=strict");
}

}

This file was deleted.

Loading

0 comments on commit 98bbc9b

Please sign in to comment.