Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a kotlin spring boot auto configuration project. It has a class Annotated with @ConfigurationProperties. This generates the property metadata file as expected. I want to add a property that does not come from the @ConfigurationProperties class. As documented, to do that I should create my own additional-spring-configuration-metadata.json file.

I found that the first time I run mvn clean package, the metadata is generated without including the info from additional-spring-configuration-metadata.json. The second time I run mvn package (assuming I didn't do a clean) the metadata includes the info from additional-spring-configuration-metadata.json

Why is this? How do I fix it to generate the data first time everytime?

My pom is below:

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

UPDATE: I added a solution below but I still have a bounty open. I will award it if someone finds a less cumbersome way of solving the problem

like image 977
Jacob Botuck Avatar asked Oct 16 '22 11:10

Jacob Botuck


1 Answers

I fixed it by adding in the mvn copy resources plugin to copy the additional-spring-configuration-metadata.json file in the process-sources phase so it can be available when kapt runs.

additional-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>
like image 146
Jacob Botuck Avatar answered Oct 29 '22 17:10

Jacob Botuck