Cannot generate spring-configuration-metadata.json file in IntelliJ Idea with Kotlin

Problem description

In Baidu search keywords, found Stack Overflow related problems

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

Original link:

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

Try to follow the method inside, fail, then continue to Baidu, find relevant clues in the official document of spring boot, direct link:

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

An official example of kotlin is found in the official document of spring. The link address is:

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

Here's what I can do with reference to the above documents

Solution

I. add plug-ins

Add plug-in in pom file without writing version number because the project inherits 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>

2. Use plug-ins to generate

I used the same plug-in before, but I still couldn't generate files. I didn't find such a sentence until I read the official kotlin document

The meaning of the words is:

"Note that kapt still does not support IntelliJ IDEA's own build system. When you want to rerun the annotation processor, start the build from the Maven Projects toolbar. "

What do you want to do if you don't mark it red or bold

OK, let's do what he said. Double click the plug-in button below to produce the spring configuration metadata.json file

Reference documents:

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

Keywords: Spring Maven Java JSON

Added by TRB on Mon, 16 Dec 2019 16:51:48 +0200