From 3ac25a05716ed41fbc7ea08783f9cba7d9817c88 Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Mon, 23 Sep 2024 05:37:40 +0200 Subject: [PATCH] Add Convention Plugins for Spring Boot --- app/build.gradle.kts | 8 +++-- .../org/example/product/app/AppServlet.java | 30 ++++++++++--------- .../org/example/product/app/Application.java | 8 +++-- 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.java | 12 +------- 13 files changed, 64 insertions(+), 56 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 ad5bfa1d..7be9b029 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) @@ -13,8 +11,12 @@ dependencies { implementation(libs.guice) implementation(libs.guice.servlet) 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) mockApiImplementation(projects.app) mockApiImplementation(libs.guava) diff --git a/app/src/main/java/org/example/product/app/AppServlet.java b/app/src/main/java/org/example/product/app/AppServlet.java index 030cc158..ec7469fe 100644 --- a/app/src/main/java/org/example/product/app/AppServlet.java +++ b/app/src/main/java/org/example/product/app/AppServlet.java @@ -1,20 +1,22 @@ package org.example.product.app; +import static java.util.Objects.requireNonNull; + +import java.io.BufferedReader; import java.io.IOException; -import java.io.PrintWriter; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import java.io.InputStreamReader; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class AppServlet { -@WebServlet("/check") -public class AppServlet extends HttpServlet { - public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { - res.setContentType("text/html"); - PrintWriter pw = res.getWriter(); - pw.println(""); - pw.println("App is running..."); - pw.println(""); - pw.close(); + @GetMapping("/") + public String index() throws IOException { + new MainModule().run(); + String version = new BufferedReader( + new InputStreamReader(requireNonNull(AppServlet.class.getResourceAsStream("/version.txt")))) + .readLine(); + return "" + "App is running... !!!!" + "
Version " + version + ""; } } diff --git a/app/src/main/java/org/example/product/app/Application.java b/app/src/main/java/org/example/product/app/Application.java index 511fdc2c..185a297b 100644 --- a/app/src/main/java/org/example/product/app/Application.java +++ b/app/src/main/java/org/example/product/app/Application.java @@ -1,10 +1,12 @@ package org.example.product.app; -import java.io.IOException; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +@SpringBootApplication public class Application { - public static void main(String[] args) throws IOException { - new MainModule().run(); + public static void main(String[] args) { + SpringApplication.run(Application.class, args); } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index bf943923..83ac0beb 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" @@ -50,6 +51,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 4921fb0c..27da48ce 100644 --- a/gradle/plugins/build.gradle.kts +++ b/gradle/plugins/build.gradle.kts @@ -9,6 +9,7 @@ dependencies { implementation("io.fuchs.gradle.classpath-collision-detector:classpath-collision-detector:1.0.0") implementation("org.cyclonedx:cyclonedx-gradle-plugin:1.10.0") implementation("org.gradlex:jvm-dependency-conflict-resolution:2.1.2") + 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..a2be379a 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.spring" // 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 6cd73278..9074c00e 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,15 +1,16 @@ 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") id("org.example.gradle.check.dependencies") id("org.example.gradle.check.format-gradle") id("org.example.gradle.check.format-java") + 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.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 dbc6bab8..cbc2f6dc 100644 --- a/kamino/build.gradle.kts +++ b/kamino/build.gradle.kts @@ -5,9 +5,6 @@ plugins { dependencies { api(projects.coruscant) - api(libs.resteasy.core) - implementation(libs.resteasy.guice) - implementation(libs.resteasy.jackson2.provider) testImplementation(libs.junit.jupiter.api) } diff --git a/kamino/src/main/java/org/example/product/kamino/KaminoModule.java b/kamino/src/main/java/org/example/product/kamino/KaminoModule.java index e9b559b2..1e4c30b0 100644 --- a/kamino/src/main/java/org/example/product/kamino/KaminoModule.java +++ b/kamino/src/main/java/org/example/product/kamino/KaminoModule.java @@ -1,20 +1,12 @@ 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! */ public class KaminoModule { - /** - * Bootstrap... - */ - public ResteasyBootstrap bootstrap; - /** * Data... */ @@ -26,8 +18,6 @@ public class KaminoModule { * @return all the important Classes */ public Class[] info() { - return new Class[] { - ResteasyBootstrap.class, ResteasyJackson2Provider.class, GuiceResteasyBootstrapServletContextListener.class - }; + return new Class[] {}; } }