How can I integrate POM build version into YAML generation using springdoc-openapi-maven-plugin?

1.3k Views Asked by At

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.)

0

There are 0 best solutions below