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

Jac*_*STL 5 maven kotlin spring-boot kapt

我有一个Kotlin Spring Boot自动配置项目。它有一个带有注释的类@ConfigurationProperties。这将按预期生成属性元数据文件。我想添加一个不是来自@ConfigurationProperties该类的属性。如文档所述,为此,我应该创建自己的additional-spring-configuration-metadata.json文件。

我发现我第一次运行mvn clean package时,会生成元数据,而不会包含来自的信息additional-spring-configuration-metadata.json。第二次运行mvn包(假设我没有清理干净),元数据包含来自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>
Run Code Online (Sandbox Code Playgroud)

更新:我在下面添加了一个解决方案,但是我仍然有赏金机会。如果有人发现解决问题的方法不那么麻烦,我将予以奖励

Jac*_*STL 3

我通过添加 mvn 复制资源插件来修复该问题,以additional-spring-configuration-metadata.json在进程源阶段复制文件,以便在 kapt 运行时可用。

附加-spring-configuration-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>
Run Code Online (Sandbox Code Playgroud)