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

问题描述

在百度搜索关键词,搜索到了 Stack Overflow 有相关问题

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

原文链接:

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

按照里面的方法试了一下,失败了,然后继续百度,在spring-boot的官方文档中找到了相关线索, 直达链接:

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

在spring官方文档中找到了kotlin的官方示例,链接地址:

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

下面是我参考上面的文档所得出来的可用方案

解决方案

一、添加插件

pom文件中添加插件,没有写版本号是因为项目继承了spring-boot-starter-parent

<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>

二、使用插件生成

我之前也是使用了同样的插件,但是始终生成不出来文件,直到看了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

转载于:https://my.oschina.net/GinkGo/blog/2244915

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值