4

Is there a way to programmatically set the build version from the POM of my Spring Boot application into the OpenApi YAML generated by the springdoc-openapi-maven-plugin?

How can I achieve it?

Currently I have integrated the springdoc-openapi-maven-plugin this way:

    <plugin>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
            <execution>
                <id>integration-test</id>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <apiDocsUrl>http://localhost:9090/v3/api-docs.yaml</apiDocsUrl>
            <outputFileName>my_open_api_specification.yml</outputFileName>
            <outputDir>${project.build.directory}/my_open_api_specification/</outputDir>
            <skip>false</skip>
        </configuration>
    </plugin>

I have an interface to annotate methods with OpenApi annotations:

@Tag(name = "API Operationen")
@RequestMapping
public interface RestApi {
...

    @Bean
    default OpenAPI myOpenApi() {
    
        return new OpenAPI()
            .info(new Info()
                .title("The title")
                .version("1.0")           <-- I want to dynamically set this version with the value coming from  BuildProperties (POM version).
                .description("The description"));
    }
}

I found some information using spring-boot-maven-plugin but I didn't succeed:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <!-- <version>2.5.4</version> -->
    <executions>
        <execution>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
        </executions>
</plugin>

Update


In the meantime I moved the bean definition from the interface into a @Configuration file/class but when I start the application I get a NullPointerException during the bean creation of myOpenApi:

@Configuration
@ConfigurationPropertiesScan("xxx")
@RequiredArgsConstructor
public class OpenApiConfig {

    @Autowired    // <-- was missing
    BuildProperties buildProperties;

    @Bean
    OpenAPI myOpenApi() {

        return new OpenAPI()
            .info(new Info()
                .title("FooBar")
                .version(buildProperties.getVersion())
                .description("blablabla"));
    }
}

As far as I understood the BuildProperties gets created by the Maven plugin and is available when the myOpenApi is going to be created, but it seems as if this is not the case. (At least with my configuration.)

du-it
  • 2,561
  • 8
  • 42
  • 80
  • Not sure how you create that bean in an interface, but you may follow [Cannot get maven project.version property in a Spring application with @Value](https://stackoverflow.com/questions/38983934/cannot-get-maven-project-version-property-in-a-spring-application-with-value) to inject the build version by `@Value` and then set to `.version()` – samabcde Jan 21 '22 at 13:00
  • I didn't have any success with this attempt. – du-it Jan 21 '22 at 20:58
  • 1
    You need to add @Autowired to `BuildProperties buildProperties`. – samabcde Jan 22 '22 at 02:22
  • OK, that did the trick. Thumb up! I thought by using @RequiredArgsConstructor this would be implicitly done as it works with controllers, ...? Post it as an answer, if you want, that I can rate it adequately. :-) – du-it Jan 22 '22 at 14:25
  • 1
    If you want to make use of @RequiredArgsConstructor, you should make `BuildProperties buildProperties` final. – samabcde Jan 22 '22 at 15:05

0 Answers0