Spring Boots Configuration Metadata with Kotlin

Make spring-boot-configuration-processor play nice with Kotlin (and IDEA)
July 15, 2018 by Michael

Last week I decided to raffle a copy of my book (see Twitter) and I wrote a small Spring Boot Command Line Runner to raffle the retweeting winner as one does (see raffle-by-retweet, feel free to reuse this).

I wrote the application in Kotlin. Notice the use of @ConfigurationProperties in my application:

The lateinit attributes are not as nice as I want them to be, but I heard support for data-classes is coming. Anyway. A super useful thing with those configuration property classes are the metadata that can be generated for your IDE of choice, see Configuration Metadata. In a Java application it’s enough to add org.springframework.boot:spring-boot-configuration-processor as compile time dependency respectively as annotationProcessor dependency in a Gradle build.

For Kotlin, you have to use the kotlin-kapt-plugin. It takes care of annotation processing in Kotlin and Spring Boots annotation processor has to be declared in its scope like this:

apply plugin: 'kotlin-kapt'
 
dependencies {
    // Other dependencies omitted
 
    kapt("org.springframework.boot:spring-boot-configuration-processor")
}

To make IDEA recognize the generated sources (and also use the same folders for classes as the Gradle build), you can this as well:

apply plugin: 'idea'
 
idea {
    module {
        def kaptMain = file("${project.buildDir}/generated/source/kapt/main")
        sourceDirs += kaptMain
        generatedSourceDirs += kaptMain
 
        outputDir file("${project.buildDir}/classes/main")
        testOutputDir file("${project.buildDir}/classes/test")
    }
}

Find the full build script here.

No comments yet

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *