Additional-spring-configuration-metadata.json 仅与第二次构建时生成的 configurationProperties 元数据合并

Additional-spring-configuration-metadata.json only merges with the configurationProperties generated metadata on second build

我有一个 kotlin spring 引导自动配置项目。它有一个 class 注释 @ConfigurationProperties。这会按预期生成 属性 元数据文件。我想添加一个并非来自 @ConfigurationProperties class 的 属性。如文档所述,为此我应该创建自己的 additional-spring-configuration-metadata.json 文件。

我发现我第一次 运行 mvn clean package 时,生成的元数据不包括来自 additional-spring-configuration-metadata.json 的信息。我第二次 运行 mvn package(假设我没有做干净)元数据包括来自 additional-spring-configuration-metadata.json

的信息

这是为什么?如何修复它每次都第一次生成数据?

我的pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.redacted.entity.autoconfigure</groupId>
    <artifactId>mongo-autoconfiguration</artifactId>
    <version>1.0.0</version>
    <name>mongo-autoconfiguration</name>
    <description>AutoConfiguration for mongo</description>

    <properties>
        <java.version>1.8</java.version>
        <kotlin.version>1.3.50</kotlin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <executions>
                    <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-autoconfigure-processor</artifactId>
                                    <version>2.1.7.RELEASE</version>
                                </annotationProcessorPath>
                                <annotationProcessorPath>
                                    <groupId>org.springframework.boot</groupId>
                                    <artifactId>spring-boot-configuration-processor</artifactId>
                                    <version>2.1.7.RELEASE</version>
                                </annotationProcessorPath>
                            </annotationProcessorPaths>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

更新:我在下面添加了一个解决方案,但我仍然有悬赏。如果有人找到更简单的解决问题的方法,我会奖励它

您可能 运行 遇到错误,认为 kapt 目标不是 运行 - https://youtrack.jetbrains.com/issue/KT-18022。尽管这被标记为已修复,但其他报告的最近评论也有同样的问题。

我通过添加 mvn 复制资源插件来修复它以在 process-sources 阶段复制 additional-spring-configuration-metadata.json 文件,以便在 kapt 运行时可以使用它。

附加-spring-配置-metadata.json

          <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes/META-INF</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/META-INF</directory>
                                    <includes>
                                        <include>
                                            additional-spring-configuration-metadata.json
                                        </include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>