From 121736dab794b820c6c3a83951dbea480592680b Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Mon, 23 Sep 2024 05:38:38 +0200 Subject: [PATCH] Add Convention Plugins for Spring Boot (Kotlin variant) --- app/build.gradle.kts | 8 +++-- .../org/example/product/app/AppServlet.kt | 33 +++++++++++-------- .../org/example/product/app/Application.kt | 7 +++- gradle/libs.versions.toml | 7 ++++ gradle/plugins/build.gradle.kts | 1 + ...rg.example.gradle.base.identity.gradle.kts | 2 +- ...le.gradle.component.application.gradle.kts | 5 +-- ...dle.feature.package-version-txt.gradle.kts | 3 ++ ...mple.gradle.feature.spring-boot.gradle.kts | 18 ++++++++++ .../org.example.gradle.feature.war.gradle.kts | 19 ----------- gradle/versions/build.gradle.kts | 4 +++ kamino/build.gradle.kts | 3 -- .../example/product/kamino/KaminoModule.kt | 11 +------ 13 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 gradle/plugins/src/main/kotlin/org.example.gradle.feature.package-version-txt.gradle.kts create mode 100644 gradle/plugins/src/main/kotlin/org.example.gradle.feature.spring-boot.gradle.kts delete mode 100644 gradle/plugins/src/main/kotlin/org.example.gradle.feature.war.gradle.kts diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 065900b5..cb4f3950 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,7 +1,5 @@ plugins { id("org.example.gradle.component.application") } -application { mainClass = "org.example.product.app.Application" } - // Complicated notation for 'capabilities' - upvote: https://github.com/gradle/gradle/issues/25629 dependencies { implementation(projects.bespin) @@ -14,8 +12,12 @@ dependencies { implementation(libs.guice.servlet) implementation(libs.kotlin.stdlib) implementation(libs.slf4j.api) + implementation(libs.spring.boot) + implementation(libs.spring.boot.autoconfigure) + implementation(libs.spring.context) + implementation(libs.spring.web) runtimeOnly(libs.slf4j.simple) - providedCompile(libs.jakarta.servlet.api) + runtimeOnly(libs.spring.boot.starter.web) mockApiApi(libs.guava) mockApiImplementation(projects.app) diff --git a/app/src/main/kotlin/org/example/product/app/AppServlet.kt b/app/src/main/kotlin/org/example/product/app/AppServlet.kt index de70d1f9..dd73e63c 100644 --- a/app/src/main/kotlin/org/example/product/app/AppServlet.kt +++ b/app/src/main/kotlin/org/example/product/app/AppServlet.kt @@ -1,19 +1,26 @@ package org.example.product.app -import javax.servlet.annotation.WebServlet -import javax.servlet.http.HttpServlet -import javax.servlet.http.HttpServletRequest -import javax.servlet.http.HttpServletResponse +import java.io.BufferedReader +import java.io.InputStreamReader +import java.util.* +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController -@WebServlet("/check") -class AppServlet : HttpServlet() { +@RestController +class AppServlet { - public override fun doGet(req: HttpServletRequest, res: HttpServletResponse) { - res.contentType = "text/html" - val pw = res.writer - pw.println("") - pw.println("App is running...") - pw.println("") - pw.close() + @GetMapping("/") + fun index(): String { + MainModule().run() + val version = + BufferedReader( + InputStreamReader( + Objects.requireNonNull( + AppServlet::class.java.getResourceAsStream("/version.txt") + ) + ) + ) + .readLine() + return "App is running... !!!!
Version $version" } } diff --git a/app/src/main/kotlin/org/example/product/app/Application.kt b/app/src/main/kotlin/org/example/product/app/Application.kt index 926c6566..936f8e66 100644 --- a/app/src/main/kotlin/org/example/product/app/Application.kt +++ b/app/src/main/kotlin/org/example/product/app/Application.kt @@ -1,5 +1,10 @@ package org.example.product.app +import org.springframework.boot.SpringApplication +import org.springframework.boot.autoconfigure.SpringBootApplication + fun main(args: Array) { - MainModule().run() + SpringApplication.run(Application::class.java, *args) } + +@SpringBootApplication open class Application diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d05d4636..4ca44595 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -17,6 +17,7 @@ poi = "5.2.2" resteasy = "4.7.6.Final" slf4j = "1.7.36" solrj = "7.7.3" +spring-boot = "2.7.18" typesafeconfig = "0.1.0" velocity = "2.3" zookeeper = "3.8.0" @@ -52,6 +53,12 @@ resteasy-jackson2-provider = { module = "org.jboss.resteasy:resteasy-jackson2-pr slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" } solr-solrj = { module = "org.apache.solr:solr-solrj", version.ref = "solrj" } +spring-boot = { module = "org.springframework.boot:spring-boot" } +spring-boot-autoconfigure = { module = "org.springframework.boot:spring-boot-autoconfigure" } +spring-boot-dependencies = { module = "org.springframework.boot:spring-boot-dependencies", version.ref = "spring-boot" } +spring-boot-starter-web = { module = "org.springframework.boot:spring-boot-starter-web" } +spring-context = { module = "org.springframework:spring-context" } +spring-web = { module = "org.springframework:spring-web" } typesafeconfig-guice = { module = "com.github.racc:typesafeconfig-guice", version.ref = "typesafeconfig" } velocity-engine-core = { module = "org.apache.velocity:velocity-engine-core", version.ref = "velocity" } zookeeper = { module = "org.apache.zookeeper:zookeeper", version.ref = "zookeeper" } diff --git a/gradle/plugins/build.gradle.kts b/gradle/plugins/build.gradle.kts index 6529b248..76f01793 100644 --- a/gradle/plugins/build.gradle.kts +++ b/gradle/plugins/build.gradle.kts @@ -10,6 +10,7 @@ dependencies { implementation("org.cyclonedx:cyclonedx-gradle-plugin:1.10.0") implementation("org.gradlex:jvm-dependency-conflict-resolution:2.1.2") implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.20") + implementation("org.springframework.boot:spring-boot-gradle-plugin:2.7.18") } testing.suites.named("test") { diff --git a/gradle/plugins/src/main/kotlin/org.example.gradle.base.identity.gradle.kts b/gradle/plugins/src/main/kotlin/org.example.gradle.base.identity.gradle.kts index be33eaaf..d4ead42d 100644 --- a/gradle/plugins/src/main/kotlin/org.example.gradle.base.identity.gradle.kts +++ b/gradle/plugins/src/main/kotlin/org.example.gradle.base.identity.gradle.kts @@ -2,7 +2,7 @@ plugins { id("org.gradle.base") } // Set the group required to refer to a Module "from outside". // I.e., when it is published or used in Included Builds. -group = "org.example.product.java" +group = "org.example.product.springkt" // Set the version from 'version.txt' version = providers.fileContents(isolated.rootProject.projectDirectory.file("gradle/version.txt")).asText.getOrElse("") diff --git a/gradle/plugins/src/main/kotlin/org.example.gradle.component.application.gradle.kts b/gradle/plugins/src/main/kotlin/org.example.gradle.component.application.gradle.kts index b09e7a18..63717785 100644 --- a/gradle/plugins/src/main/kotlin/org.example.gradle.component.application.gradle.kts +++ b/gradle/plugins/src/main/kotlin/org.example.gradle.component.application.gradle.kts @@ -1,5 +1,5 @@ plugins { - id("org.gradle.application") + id("org.gradle.java") id("org.example.gradle.base.dependency-rules") id("org.example.gradle.base.identity") id("org.example.gradle.base.lifecycle") @@ -7,11 +7,12 @@ plugins { id("org.example.gradle.check.format-gradle") id("org.example.gradle.check.format-java") id("org.example.gradle.check.format-kotlin") + id("org.example.gradle.feature.spring-boot") id("org.example.gradle.feature.checksum") + id("org.example.gradle.feature.package-version-txt") id("org.example.gradle.feature.compile-java") id("org.example.gradle.feature.compile-kotlin") id("org.example.gradle.feature.javadoc") id("org.example.gradle.feature.test") id("org.example.gradle.feature.test-end-to-end") - id("org.example.gradle.feature.war") } diff --git a/gradle/plugins/src/main/kotlin/org.example.gradle.feature.package-version-txt.gradle.kts b/gradle/plugins/src/main/kotlin/org.example.gradle.feature.package-version-txt.gradle.kts new file mode 100644 index 00000000..7c64e7ff --- /dev/null +++ b/gradle/plugins/src/main/kotlin/org.example.gradle.feature.package-version-txt.gradle.kts @@ -0,0 +1,3 @@ +plugins { id("org.gradle.java") } + +tasks.processResources { from(isolated.rootProject.projectDirectory.file("gradle/version.txt")) } diff --git a/gradle/plugins/src/main/kotlin/org.example.gradle.feature.spring-boot.gradle.kts b/gradle/plugins/src/main/kotlin/org.example.gradle.feature.spring-boot.gradle.kts new file mode 100644 index 00000000..ba842e47 --- /dev/null +++ b/gradle/plugins/src/main/kotlin/org.example.gradle.feature.spring-boot.gradle.kts @@ -0,0 +1,18 @@ +plugins { + id("org.gradle.java") + id("org.springframework.boot") + id("org.example.gradle.base.dependency-rules") +} + +dependencies { developmentOnly("org.springframework.boot:spring-boot-devtools") } + +configurations { + productionRuntimeClasspath { + extendsFrom(configurations["internal"]) + shouldResolveConsistentlyWith(configurations["mainRuntimeClasspath"]) + } + developmentOnly { + extendsFrom(configurations["internal"]) + shouldResolveConsistentlyWith(configurations["mainRuntimeClasspath"]) + } +} diff --git a/gradle/plugins/src/main/kotlin/org.example.gradle.feature.war.gradle.kts b/gradle/plugins/src/main/kotlin/org.example.gradle.feature.war.gradle.kts deleted file mode 100644 index 53477adf..00000000 --- a/gradle/plugins/src/main/kotlin/org.example.gradle.feature.war.gradle.kts +++ /dev/null @@ -1,19 +0,0 @@ -plugins { - id("org.gradle.war") - id("org.example.gradle.base.dependency-rules") -} - -tasks.war { setGroup(null) } - -// The war plugin used 'providedRuntime' / 'providedCompile' to resolve dependencies for packaging -// the WAR file -configurations.providedCompile { shouldResolveConsistentlyWith(configurations["mainRuntimeClasspath"]) } - -configurations.providedRuntime { shouldResolveConsistentlyWith(configurations["mainRuntimeClasspath"]) } - -tasks.register("deployWebApp") { - group = "distribution" - description = "Deploy web app into local Tomcat found via CATALINA_HOME" - from(tasks.war) { into("webapps") } - into(providers.gradleProperty("catalinaHome").orElse(providers.environmentVariable("CATALINA_HOME"))) -} diff --git a/gradle/versions/build.gradle.kts b/gradle/versions/build.gradle.kts index 5e00e850..01b52b60 100644 --- a/gradle/versions/build.gradle.kts +++ b/gradle/versions/build.gradle.kts @@ -5,6 +5,10 @@ plugins { id("org.example.gradle.check.format-gradle") } +javaPlatform.allowDependencies() + +dependencies { api(platform(libs.spring.boot.dependencies)) } + // Reject versions that should not be upgraded beyond a certain point. // This makes Dependabot PR builds fail that attempt to update these. dependencies.constraints { diff --git a/kamino/build.gradle.kts b/kamino/build.gradle.kts index 7bf82aae..9e35fc93 100644 --- a/kamino/build.gradle.kts +++ b/kamino/build.gradle.kts @@ -6,9 +6,6 @@ plugins { dependencies { api(projects.coruscant) api(libs.kotlin.stdlib) - api(libs.resteasy.core) - implementation(libs.resteasy.guice) - implementation(libs.resteasy.jackson2.provider) testImplementation(libs.junit.jupiter.api) } diff --git a/kamino/src/main/kotlin/org/example/product/kamino/KaminoModule.kt b/kamino/src/main/kotlin/org/example/product/kamino/KaminoModule.kt index 4912d014..899a1530 100644 --- a/kamino/src/main/kotlin/org/example/product/kamino/KaminoModule.kt +++ b/kamino/src/main/kotlin/org/example/product/kamino/KaminoModule.kt @@ -1,14 +1,9 @@ package org.example.product.kamino import org.example.product.coruscant.CoruscantModuleData -import org.jboss.resteasy.plugins.guice.GuiceResteasyBootstrapServletContextListener -import org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider -import org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap /** I am published and therefore I have Javadoc! */ class KaminoModule { - /** Bootstrap... */ - var bootstrap: ResteasyBootstrap? = null /** Data... */ var data: CoruscantModuleData? = null @@ -19,10 +14,6 @@ class KaminoModule { * @return all the important Classes */ fun info(): Array> { - return arrayOf( - ResteasyBootstrap::class.java, - ResteasyJackson2Provider::class.java, - GuiceResteasyBootstrapServletContextListener::class.java - ) + return arrayOf() } }