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

10,467

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?

Share:
10,467
Alexander Mikhalchenko
Author by

Alexander Mikhalchenko

Updated on June 18, 2022

Comments

  • Alexander Mikhalchenko
    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
      Matt Friedman almost 8 years
      I seem to be having the same problem using groovy
    • Rakesh N
      Rakesh N almost 6 years
      I 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
    Andy Wilkinson almost 8 years
    This might help too: kotlinlang.org/docs/reference/…
  • Matt Friedman
    Matt Friedman almost 8 years
    Can you suggest a solution for the same issue with groovy?
  • Alexander Mikhalchenko
    Alexander Mikhalchenko almost 8 years
    As 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
    MrBigglesworth over 7 years
    I 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-processo‌​r"and that was it
  • Mihkel Selgal
    Mihkel Selgal over 6 years
    The 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
    naXa stands with Ukraine about 5 years
    compileOnly scope in Gradle 4