spring-configuration-metadata.json file is not generated in IntelliJ Idea for Kotlin @ConfigurationProperties class
Solution 1
Thank you 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"
...
}
to build.gradle file, run gradle compileJava in command line and turn on annotation processing in IntelliJ Idea settings Build, Execution, Deployment -> Compiler -> Annotation processor -> Enable anotation processing. The rest of configuration remains the same
Also note that without this line
optional "org.springframework.boot:spring-boot-configuration-processor"
IntelliJ Idea will complain whith
Cannot resolve configuration property
message in your application.properties or application.yml
Solution 2
For those who want to use Maven instead of Gradle, you need to add an 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 issue KT-18022 that prevents this from working if an compiler plugin such as kotlin-maven-allopen
is declared as dependency.
Solution 3
Kotlin has its own compiler. The meta-data is generated by an annotation processor that is a hook-point in the Java compiler.
I have no idea if such hook-point is available in Kotlin but in any case, Spring Boot does not support anything else than Java at the moment. Maybe this would help?
Alexander Mikhalchenko
Updated on June 18, 2022Comments
-
Alexander Mikhalchenko almost 2 years
I'm trying to generate spring-configuration-metadata.json file for my Spring Boot based project. If I use Java @ConfigurationProperties class 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 Kotlin class the spring-configuration-metadata.json file is not generated (I've tried both gradle build and Idea Rebuild Project).
@ConfigurationProperties("myprops") class MyProps { var hello: String? = null }
AFAIK Kotlin generates the same class with constructor, getters and setters and should act as regular Java bean.
Any ideas why spring-boot-configuration-processor doesn't work with Kotlin classes?
-
Matt Friedman almost 8 yearsI seem to be having the same problem using groovy
-
Rakesh N almost 6 yearsI have the same problem despite trying all the suggestions. My environment - IntelliJ 2018.1.3 CE, SpringBoot 2.0.1, Kotlin 1.2.41
-
-
Andy Wilkinson almost 8 yearsThis might help too: kotlinlang.org/docs/reference/…
-
Matt Friedman almost 8 yearsCan you suggest a solution for the same issue with groovy?
-
Alexander Mikhalchenko almost 8 yearsAs Stéphane Nicoll said, currently Spring Boot supports nothing but Java. During work on Kotlin-based project I came to conclusion that it is better to use Java classes for custom properties. Everything else is a hacks and workarounds (at the moment) for compiler, programming language and IDE.
-
MrBigglesworth over 7 yearsI got this working fine in kotlin, using spring boot 1.4.0. Without using propdeps or anything on the java side. In my web module, where all the config is used I added kapt "org.springframework.boot:spring-boot-configuration-processor"and that was it
-
Mihkel Selgal over 6 yearsThe mentioned issue is fixed and I can confirm that the Maven solution works out of the box when using with Kotlin 1.1.4 and Spring Boot 1.5.6.
-
naXa stands with Ukraine about 5 years
compileOnly
scope in Gradle 4