用Kotlin在IntelliJ Idea中沒法生成 spring-configuration-metadata.json 文件

問題描述

在百度搜索關鍵詞,搜索到了 Stack Overflow 有相關問題html

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

原文連接:spring

https://stackoverflow.com/questions/37858833/spring-configuration-metadata-json-file-is-not-generated-in-intellij-idea-for-koapache

按照裏面的方法試了一下,失敗了,而後繼續百度,在spring-boot的官方文檔中找到了相關線索, 直達連接:json

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-kotlin.html#boot-features-kotlin-configuration-propertiesintellij-idea

在spring官方文檔中找到了kotlin的官方示例,連接地址:jvm

https://kotlinlang.org/docs/reference/kapt.html#using-in-mavenmaven

下面是我參考上面的文檔所得出來的可用方案ide

解決方案

1、添加插件

pom文件中添加插件,沒有寫版本號是由於項目繼承了spring-boot-starter-parentspring-boot

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <proc>none</proc>
            <source>${java.version}</source>
            <target>${java.version}</target>
        </configuration>
        <executions>
            <!-- Replacing default-compile as it is treated specially by maven -->
            <execution>
                <id>default-compile</id>
                <phase>none</phase>
            </execution>
            <!-- Replacing default-testCompile as it is treated specially by maven -->
            <execution>
                <id>default-testCompile</id>
                <phase>none</phase>
            </execution>
            <execution>
                <id>java-compile</id>
                <phase>compile</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
            <execution>
                <id>java-test-compile</id>
                <phase>test-compile</phase>
                <goals>
                    <goal>testCompile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <artifactId>kotlin-maven-plugin</artifactId>
        <groupId>org.jetbrains.kotlin</groupId>
        <configuration>
            <args>
                <arg>-Xjsr305=strict</arg>
            </args>
            <compilerPlugins>
                <plugin>spring</plugin>
            </compilerPlugins>
            <jvmTarget>${java.version}</jvmTarget>
        </configuration>
        <executions>
            <execution>
                <id>kapt</id>
                <goals>
                    <goal>kapt</goal>
                </goals>
                <configuration>
                    <sourceDirs>
                        <sourceDir>src/main/kotlin</sourceDir>
                        <sourceDir>src/main/java</sourceDir>
                    </sourceDirs>
                    <annotationProcessorPaths>
                        <!-- Specify your annotation processors here. -->
                        <annotationProcessorPath>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                            <version>${spring.boot.version}</version>
                        </annotationProcessorPath>
                    </annotationProcessorPaths>
                </configuration>
            </execution>
            <execution>
                <id>compile</id>
                <phase>compile</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
            <execution>
                <id>test-compile</id>
                <phase>test-compile</phase>
                <goals>
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-allopen</artifactId>
                <version>1.2.20</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

2、使用插件生成

我以前也是使用了一樣的插件,可是始終生成不出來文件,直到看了kotlin官方文檔我才發現有這麼一句話

img

文字的意思是:

"請注意,kapt仍然不支持IntelliJ IDEA本身的構建系統。當你想要從新運行註釋處理器時,能夠從「Maven Projects」工具欄啓動構建。"

非常坑爹啊,你也不標紅也不加粗是想怎樣啊

好了,那就按照他說的作吧, 雙擊下面的插件按鈕就能夠生產spring-configuration-metadata.json文件了

img

參考文檔:

https://stackoverflow.com/questions/37858833/spring-configuration-metadata-json-file-is-not-generated-in-intellij-idea-for-ko

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-kotlin.html#boot-features-kotlin-configuration-properties

https://kotlinlang.org/docs/reference/kapt.html

相關文章
相關標籤/搜索