Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting dynamic version for swagger API version

I'm trying to make version dynamic in below annotation.

@SpringBootApplication
@OpenAPIDefinition(info = @Info(title = "Test APIs", version = "${project.version}", description = "Testing APIs"))
public class DemoApplication {

}

Here I want to make version dynamic so it will take value from pom.xml file.

Do I need to have any configuration for that? TIA.

like image 538
Balkrishna Kardam Avatar asked Sep 17 '25 17:09

Balkrishna Kardam


1 Answers

You can define the OpenAPIDefinition programatically and use the BuildProperties to set OpenAPIDefinition.info data like this:

@SpringBootApplication
public class ApiApplication {

    @Autowired
    private BuildProperties buildProperties;

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }

    @Bean
    OpenAPI customOpenAPI() {
        return new OpenAPI()
            .components(new Components())
            .info(new Info()
                .title(buildProperties.getArtifact() + " API")
                .version(buildProperties.getVersion())
                .description(buildProperties.getArtifact() + " - API Swagger documentation")
                .license(new License().name("Apache 2.0").url("http://springdoc.org")));
    }
}

You'll need add the 'build-info' goal in the 'spring-boot-maven-plugin' into pom.xml file in order to use de BuildProperties component in your code:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>build-info</id>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
like image 77
Leonardo Ruggeri Avatar answered Sep 19 '25 08:09

Leonardo Ruggeri