From 42c435f3592322ac9010725aeeb6be6ed134cebc Mon Sep 17 00:00:00 2001 From: khalilou88 <32600911+khalilou88@users.noreply.github.com> Date: Tue, 2 Jan 2024 16:54:08 +0100 Subject: [PATCH] add ktlint for spring boot projects (#19) * add ktlint for spring boot projects * work in progress * work in progress --- nx-gradle/spring-boot/g-sb-kt-app/build.gradle.kts | 1 + nx-gradle/spring-boot/g-sb-kt-app/project.json | 12 ++++++++++++ .../springboot/springbootktapp/HelloController.kt | 6 ++---- .../SpringBootSpringBootKtAppApplication.kt | 2 -- .../springbootktapp/HelloControllerTests.kt | 7 +++---- .../SpringBootSpringBootKtAppApplicationTests.kt | 8 +++----- nx-gradle/spring-boot/g-sb-kt-lib/build.gradle.kts | 13 +++++++------ nx-gradle/spring-boot/g-sb-kt-lib/project.json | 12 ++++++++++++ .../springboot/springbootktlib/HelloService.kt | 7 +++---- .../springboot/springbootktlib/HelloServiceTests.kt | 9 +++------ .../springboot/springbootktlib/TestConfiguration.kt | 3 +-- 11 files changed, 47 insertions(+), 33 deletions(-) diff --git a/nx-gradle/spring-boot/g-sb-kt-app/build.gradle.kts b/nx-gradle/spring-boot/g-sb-kt-app/build.gradle.kts index bd69b1d..88ed0b6 100644 --- a/nx-gradle/spring-boot/g-sb-kt-app/build.gradle.kts +++ b/nx-gradle/spring-boot/g-sb-kt-app/build.gradle.kts @@ -7,6 +7,7 @@ plugins { id("io.spring.dependency-management") kotlin("jvm") kotlin("plugin.spring") + id("org.jlleitschuh.gradle.ktlint") version "12.0.3" } group = "com.example" diff --git a/nx-gradle/spring-boot/g-sb-kt-app/project.json b/nx-gradle/spring-boot/g-sb-kt-app/project.json index 6a03c97..e297b89 100644 --- a/nx-gradle/spring-boot/g-sb-kt-app/project.json +++ b/nx-gradle/spring-boot/g-sb-kt-app/project.json @@ -29,6 +29,18 @@ "options": { "task": "test" } + }, + "ktlint": { + "executor": "@jnxplus/nx-gradle:run-task", + "options": { + "task": "ktlintCheck" + } + }, + "ktlint-format": { + "executor": "@jnxplus/nx-gradle:run-task", + "options": { + "task": "ktlintFormat" + } } }, "tags": [] diff --git a/nx-gradle/spring-boot/g-sb-kt-app/src/main/kotlin/com/example/springboot/springbootktapp/HelloController.kt b/nx-gradle/spring-boot/g-sb-kt-app/src/main/kotlin/com/example/springboot/springbootktapp/HelloController.kt index c141a98..1baeb36 100644 --- a/nx-gradle/spring-boot/g-sb-kt-app/src/main/kotlin/com/example/springboot/springbootktapp/HelloController.kt +++ b/nx-gradle/spring-boot/g-sb-kt-app/src/main/kotlin/com/example/springboot/springbootktapp/HelloController.kt @@ -9,8 +9,6 @@ import org.springframework.web.bind.annotation.RestController class HelloController( @Autowired val helloService: HelloService, ) { - - @GetMapping("/") - fun greeting():String = helloService.message() - + @GetMapping("/") + fun greeting(): String = helloService.message() } diff --git a/nx-gradle/spring-boot/g-sb-kt-app/src/main/kotlin/com/example/springboot/springbootktapp/SpringBootSpringBootKtAppApplication.kt b/nx-gradle/spring-boot/g-sb-kt-app/src/main/kotlin/com/example/springboot/springbootktapp/SpringBootSpringBootKtAppApplication.kt index ea3702c..8fad90f 100644 --- a/nx-gradle/spring-boot/g-sb-kt-app/src/main/kotlin/com/example/springboot/springbootktapp/SpringBootSpringBootKtAppApplication.kt +++ b/nx-gradle/spring-boot/g-sb-kt-app/src/main/kotlin/com/example/springboot/springbootktapp/SpringBootSpringBootKtAppApplication.kt @@ -9,5 +9,3 @@ class SpringBootSpringBootKtAppApplication fun main(args: Array) { runApplication(*args) } - - diff --git a/nx-gradle/spring-boot/g-sb-kt-app/src/test/kotlin/com/example/springboot/springbootktapp/HelloControllerTests.kt b/nx-gradle/spring-boot/g-sb-kt-app/src/test/kotlin/com/example/springboot/springbootktapp/HelloControllerTests.kt index e2a26d5..8a3bbef 100644 --- a/nx-gradle/spring-boot/g-sb-kt-app/src/test/kotlin/com/example/springboot/springbootktapp/HelloControllerTests.kt +++ b/nx-gradle/spring-boot/g-sb-kt-app/src/test/kotlin/com/example/springboot/springbootktapp/HelloControllerTests.kt @@ -11,8 +11,9 @@ import org.springframework.boot.test.web.client.getForEntity import org.springframework.http.HttpStatus @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -class HelloControllerTests(@Autowired val restTemplate: TestRestTemplate) { - +class HelloControllerTests( + @Autowired val restTemplate: TestRestTemplate, +) { @BeforeAll fun setup() { println(">> Setup") @@ -26,10 +27,8 @@ class HelloControllerTests(@Autowired val restTemplate: TestRestTemplate) { assertThat(entity.body).contains("Hello World") } - @AfterAll fun teardown() { println(">> Tear down") } - } diff --git a/nx-gradle/spring-boot/g-sb-kt-app/src/test/kotlin/com/example/springboot/springbootktapp/SpringBootSpringBootKtAppApplicationTests.kt b/nx-gradle/spring-boot/g-sb-kt-app/src/test/kotlin/com/example/springboot/springbootktapp/SpringBootSpringBootKtAppApplicationTests.kt index 4a99f14..5d16452 100644 --- a/nx-gradle/spring-boot/g-sb-kt-app/src/test/kotlin/com/example/springboot/springbootktapp/SpringBootSpringBootKtAppApplicationTests.kt +++ b/nx-gradle/spring-boot/g-sb-kt-app/src/test/kotlin/com/example/springboot/springbootktapp/SpringBootSpringBootKtAppApplicationTests.kt @@ -5,9 +5,7 @@ import org.springframework.boot.test.context.SpringBootTest @SpringBootTest class SpringBootSpringBootKtAppApplicationTests { - - @Test - fun contextLoads() { - } - + @Test + fun contextLoads() { + } } diff --git a/nx-gradle/spring-boot/g-sb-kt-lib/build.gradle.kts b/nx-gradle/spring-boot/g-sb-kt-lib/build.gradle.kts index 912c9a3..ff9e32e 100644 --- a/nx-gradle/spring-boot/g-sb-kt-lib/build.gradle.kts +++ b/nx-gradle/spring-boot/g-sb-kt-lib/build.gradle.kts @@ -3,10 +3,11 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile val javaVersion: String by project plugins { - id("org.springframework.boot") apply false - id("io.spring.dependency-management") + id("org.springframework.boot") apply false + id("io.spring.dependency-management") kotlin("jvm") kotlin("plugin.spring") + id("org.jlleitschuh.gradle.ktlint") version "12.0.3" } group = "com.example" @@ -17,13 +18,13 @@ java { } repositories { - mavenCentral() + mavenCentral() } dependencyManagement { - imports { - mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES) - } + imports { + mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES) + } } dependencies { diff --git a/nx-gradle/spring-boot/g-sb-kt-lib/project.json b/nx-gradle/spring-boot/g-sb-kt-lib/project.json index 0dc0a56..9fbc263 100644 --- a/nx-gradle/spring-boot/g-sb-kt-lib/project.json +++ b/nx-gradle/spring-boot/g-sb-kt-lib/project.json @@ -16,6 +16,18 @@ "options": { "task": "test" } + }, + "ktlint": { + "executor": "@jnxplus/nx-gradle:run-task", + "options": { + "task": "ktlintCheck" + } + }, + "ktlint-format": { + "executor": "@jnxplus/nx-gradle:run-task", + "options": { + "task": "ktlintFormat" + } } }, "tags": [] diff --git a/nx-gradle/spring-boot/g-sb-kt-lib/src/main/kotlin/com/example/springboot/springbootktlib/HelloService.kt b/nx-gradle/spring-boot/g-sb-kt-lib/src/main/kotlin/com/example/springboot/springbootktlib/HelloService.kt index 122e717..10a645b 100644 --- a/nx-gradle/spring-boot/g-sb-kt-lib/src/main/kotlin/com/example/springboot/springbootktlib/HelloService.kt +++ b/nx-gradle/spring-boot/g-sb-kt-lib/src/main/kotlin/com/example/springboot/springbootktlib/HelloService.kt @@ -4,8 +4,7 @@ import org.springframework.stereotype.Service @Service class HelloService { - - fun message():String { - return "Hello World!" - } + fun message(): String { + return "Hello World!" + } } diff --git a/nx-gradle/spring-boot/g-sb-kt-lib/src/test/kotlin/com/example/springboot/springbootktlib/HelloServiceTests.kt b/nx-gradle/spring-boot/g-sb-kt-lib/src/test/kotlin/com/example/springboot/springbootktlib/HelloServiceTests.kt index 7b6b6cb..9a027e7 100644 --- a/nx-gradle/spring-boot/g-sb-kt-lib/src/test/kotlin/com/example/springboot/springbootktlib/HelloServiceTests.kt +++ b/nx-gradle/spring-boot/g-sb-kt-lib/src/test/kotlin/com/example/springboot/springbootktlib/HelloServiceTests.kt @@ -7,10 +7,10 @@ import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest - @SpringBootTest -class HelloServiceTests(@Autowired val helloService: HelloService) { - +class HelloServiceTests( + @Autowired val helloService: HelloService, +) { @BeforeAll fun setup() { println(">> Setup") @@ -22,11 +22,8 @@ class HelloServiceTests(@Autowired val helloService: HelloService) { assertThat(helloService.message()).contains("Hello World") } - @AfterAll fun teardown() { println(">> Tear down") } - - } diff --git a/nx-gradle/spring-boot/g-sb-kt-lib/src/test/kotlin/com/example/springboot/springbootktlib/TestConfiguration.kt b/nx-gradle/spring-boot/g-sb-kt-lib/src/test/kotlin/com/example/springboot/springbootktlib/TestConfiguration.kt index 438bf3f..f3943e5 100644 --- a/nx-gradle/spring-boot/g-sb-kt-lib/src/test/kotlin/com/example/springboot/springbootktlib/TestConfiguration.kt +++ b/nx-gradle/spring-boot/g-sb-kt-lib/src/test/kotlin/com/example/springboot/springbootktlib/TestConfiguration.kt @@ -3,5 +3,4 @@ package com.example.springboot.springbootktlib import org.springframework.boot.autoconfigure.SpringBootApplication @SpringBootApplication -open class TestConfiguration { -} +open class TestConfiguration