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 Test task and the spring-boot-starter-test dependencies 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 @SpringBootApplication annotation is uses to register the application
  • StarterApplication is an empty class
  • The runApplication top level function runs the application when main is called
  • To run main,
    • Either click on the run icon from the Idea’s interface
    • Or do a ./gradlew bootRun which automatically calls main

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.


Refs

  1. https://spring.io/guides/tutorials/spring-boot-kotlin