Spring -configuration-metadata.json file is not generated in IntelliJ Idea for Kotlin class @ConfigurationProperties

I am trying to generate a spring-configuration-metadata.json file for my Spring Boot project. If I use the Java class @ConfigurationProperties , it is generated correctly and automatically:

@ConfigurationProperties("myprops") public class MyProps { private String hello; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } } 

But if I use the Kotlin class, the spring-configuration-metadata.json file is not created (I tried both gradle build and Idea Project reconstruction ).

 @ConfigurationProperties("myprops") class MyProps { var hello: String? = null } 

AFAIK Kotlin generates the same class with constructor, getters and setters and should act like a regular Java bean.

Any ideas why spring-boot-configuration-processor doesn't work with Kotlin classes?

+6
source share
3 answers

Thanks for pointing me in the right direction. So the solution is to add

 dependencies { ... kapt "org.springframework.boot:spring-boot-configuration-processor" optional "org.springframework.boot:spring-boot-configuration-processor" ... } 

in build.gradle , run gradle compileJava on the command line and enable annotation processing in the IntelliJ Idea settings. Build, execute, deploy β†’ Compiler β†’ Annotation Processor β†’ Enable annotation processing . The rest of the configuration remains the same

Also note that without this line

 optional "org.springframework.boot:spring-boot-configuration-processor" 

IntelliJ Idea will complain whith

Unable to resolve configuration property

in application.properties or application.yml

+8
source

Kotlin has its own compiler. Metadata is generated by the annotation processor, which is the intercept point in the Java compiler.

I have no idea if such a hook is available in Kotlin, but in any case, Spring Boot does not support anything other than Java. Perhaps this will help?

+1
source

For those who want to use Maven instead of Gradle, you need to add kapt execution to the kotlin-maven-plugin configuration.

 <execution> <id>kapt</id> <goals> <goal>kapt</goal> </goals> <configuration> <sourceDirs> <sourceDir>src/main/kotlin</sourceDir> </sourceDirs> <annotationProcessorPaths> <annotationProcessorPath> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>1.5.3.RELEASE</version> </annotationProcessorPath> </annotationProcessorPaths> </configuration> </execution> 

There is an open problem KT-18022 that prevents this from working if a compiler plugin, such as kotlin-maven-allopen , is declared as a dependency.

+1
source

All Articles