build.gradle
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
// adds spring boot
id("org.springframework.boot") version "3.2.5"
// manages versioning of spring boot dependencies
id("io.spring.dependency-management") version "1.1.4"
// JVM conf
kotlin("jvm") version "1.9.23"
// Auto adds open qualifier to classes and methods
kotlin("plugin.spring") version "1.9.23"
}
group = "basic"
version = "0.0.1-SNAPSHOT"
java {
sourceCompatibility = JavaVersion.VERSION_21
}
// localtions wherefrom dependencies are fetched
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
// dependency for testing
testImplementation("org.springframework.boot:spring-boot-starter-test)
}
// Conf for the Kotlin compile task
tasks.withType<KotlinCompile> {
kotlinOptions {
// enforce JSR-305 annotations
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "21"
}
}
// Conf for the Test task
tasks.withType<Test> {
useJUnitPlatform()
}Know why you need them
Need for Kotlin Spring plugin Need for enforcing JSR-305 in Kotlin The
Testtask and thespring-boot-starter-testdependencies are only needed for testing, the application will run fine without them
Application registration
package starter.main
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class StarterApplication
fun main(args: Array<String>) {
runApplication<StarterApplication>(*args)
}- The
@SpringBootApplicationannotation is uses to register the application StarterApplicationis an empty class- The
runApplicationtop level function runs the application whenmainis called - To run
main,- Either click on the run icon from the Idea’s interface
- Or do a
./gradlew bootRunwhich automatically callsmain
build/ directory
Once you build you application a
build/directory would be created in the project root with all the compiled classes, JARs and other build artifacts.