在IntelliT Idea for Kotlin @ConfigurationProperties类中未生成spring-configuration-metadata.json文件

Ale*_*nko 10 spring intellij-idea gradle kotlin spring-boot

我正在尝试为基于Spring Boot的项目生成spring-configuration-metadata.json文件.如果我使用Java @ConfigurationProperties类,它会正确并自动生成:

@ConfigurationProperties("myprops")
public class MyProps {

    private String hello;

    public String getHello() {
        return hello;
    }

    public void setHello(String hello) {
        this.hello = hello;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用Kotlin类,则不会生成spring-configuration-metadata.json文件(我已尝试过gradle构建和Idea Rebuild项目).

@ConfigurationProperties("myprops")
class MyProps {
    var hello: String? = null
}
Run Code Online (Sandbox Code Playgroud)

AFAIK Kotlin使用构造函数,getter和setter生成相同的类,并且应该充当常规Java bean.

有什么想法为什么spring-boot-configuration-processor不能与Kotlin类一起使用?

Ale*_*nko 11

谢谢你指点我正确的方向.所以解决方案是添加

dependencies {
    ...
    kapt "org.springframework.boot:spring-boot-configuration-processor"
    optional "org.springframework.boot:spring-boot-configuration-processor"
    ...
}
Run Code Online (Sandbox Code Playgroud)

to build.gradle file,在命令行中运行gradle compileJava并在IntelliJ Idea设置中打开注释处理Build,Execution,Deployment - > Compiler - > Annotation processor - >启用anotation处理.其余配置保持不变

还要注意没有这一行

optional "org.springframework.boot:spring-boot-configuration-processor"
Run Code Online (Sandbox Code Playgroud)

IntelliJ Idea会抱怨

无法解析配置属性

application.propertiesapplication.yml中的消息


Leo*_*ngs 5

对于那些想要使用 Maven 而不是 Gradle 的人,您需要kapt在 kotlin-maven-plugin 配置中添加一个执行。

<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>
Run Code Online (Sandbox Code Playgroud)

如果编译器插件(例如)被声明为依赖项,则有一个未解决的问题KT-18022会阻止此操作kotlin-maven-allopen