Skip to content

Commit

Permalink
Add Convention Plugins for Spring Boot (Kotlin variant)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Sep 2, 2024
1 parent 23513bc commit c686409
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 52 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
33 changes: 20 additions & 13 deletions app/src/main/kotlin/org/example/product/app/AppServlet.kt
Original file line number Diff line number Diff line change
@@ -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("<html><body>")
pw.println("App is running...")
pw.println("</body></html>")
pw.close()
@GetMapping("/")
fun index(): String {
MainModule().run()
val version =
BufferedReader(
InputStreamReader(
Objects.requireNonNull(
AppServlet::class.java.getResourceAsStream("/version.txt")
)
)
)
.readLine()
return "<html><body>App is running... !!!!<br/>Version $version</body></html>"
}
}
7 changes: 6 additions & 1 deletion app/src/main/kotlin/org/example/product/app/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.example.product.app

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

fun main(args: Array<String>) {
MainModule().run()
SpringApplication.run(Application::class.java, *args)
}

@SpringBootApplication open class Application
7 changes: 7 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions gradle/plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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.0")
implementation("org.springframework.boot:spring-boot-gradle-plugin:2.7.18")
}

testing.suites.named<JvmTestSuite>("test") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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.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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins { id("org.gradle.java") }

tasks.processResources { from(isolated.rootProject.projectDirectory.file("gradle/version.txt")) }
Original file line number Diff line number Diff line change
@@ -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"])
}
}

This file was deleted.

4 changes: 4 additions & 0 deletions gradle/versions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions kamino/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,10 +14,6 @@ class KaminoModule {
* @return all the important Classes
*/
fun info(): Array<Class<*>> {
return arrayOf(
ResteasyBootstrap::class.java,
ResteasyJackson2Provider::class.java,
GuiceResteasyBootstrapServletContextListener::class.java
)
return arrayOf()
}
}

0 comments on commit c686409

Please sign in to comment.