Spring Boot
Kargo’s spring-boot framework sets up everything the Spring Boot Gradle plugin and Kotlin Spring plugins would.
Create the project
Section titled “Create the project”kargo new greeter -w spring-bootcd greeterkargo run[frameworks]spring-boot = { starters = ["web"] }The generated app has one REST endpoint:
package com.example.greeter
import org.springframework.boot.autoconfigure.SpringBootApplicationimport org.springframework.boot.runApplicationimport org.springframework.web.bind.annotation.GetMappingimport org.springframework.web.bind.annotation.RequestParamimport org.springframework.web.bind.annotation.RestController
@SpringBootApplicationclass Application
@RestControllerclass GreetingController { @GetMapping("/") fun greet( @RequestParam(defaultValue = "World") name: String, ) = greeting(name)}
fun greeting(name: String) = "Hello, $name!"
fun main(args: Array<String>) { runApplication<Application>(args = args)}$ curl 'localhost:8080/?name=Kargo'Hello, Kargo!Starters
Section titled “Starters”Each entry in starters adds org.springframework.boot:spring-boot-starter-<name>. Versions come from the Spring Boot BOM, so you never write them:
[frameworks]spring-boot = { version = "4.1.1", starters = ["web", "data-jpa", "validation", "actuator"] }
[dependencies]"com.h2database:h2" = { scope = "runtime" } # version managed by the Spring Boot BOM"org.postgresql:postgresql" = { scope = "runtime" }JPA entities
Section titled “JPA entities”With data-jpa (or jpa = true), Kargo enables the noarg and allopen compiler plugins for @Entity, @MappedSuperclass and @Embeddable, so Kotlin classes work as JPA entities:
import jakarta.persistence.*import org.springframework.data.jpa.repository.JpaRepository
@Entityclass Book( var title: String, var author: String, @Id @GeneratedValue var id: Long? = null,)
interface BookRepository : JpaRepository<Book, Long> { fun findByAuthor(author: String): List<Book>}Configuration
Section titled “Configuration”resources/application.properties (or application.yml) is on the classpath:
server.port=8081spring.jpa.hibernate.ddl-auto=updateTesting
Section titled “Testing”spring-boot-starter-test is added to the tests automatically:
package com.example.greeter
import org.junit.jupiter.api.Testimport org.springframework.beans.factory.annotation.Autowiredimport org.springframework.boot.test.context.SpringBootTestimport kotlin.test.assertEquals
@SpringBootTestclass ApplicationTest { @Autowired lateinit var controller: GreetingController
@Test fun greets() { assertEquals("Hello, Spring!", controller.greet("Spring")) }}Packaging
Section titled “Packaging”kargo package --releasejava -jar target/release/jvm/greeter-0.1.0-all.jar