spring-bootmavenopenapi-generator

How to configure OpenApi Generator with Swagger UI


If one wants to generate the code from an openapi file, OpenApi Generator Plugin for Maven is quite handy.

I am using the OpenAPI Generator Plugin for Maven to generate server code for a demo Spring Boot application created with Spring Initializr. When the interfaceOnly flag is set to false, the generator creates a SpringDocConfiguration class that includes a bean for io.swagger.v3.oas.models.OpenAPI, which the Swagger UI uses. However, it also generates an org.openapitools.OpenApiGeneratorApplication class with a @SpringBootApplication annotation. This class conflicts with my DemoApplication.java, which has its own @SpringBootApplication annotation.

when I try to build the project, I encounter the following error due to multiple main classes:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.2.5:repackage (repackage) on project demo: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:3.2.5:repackage failed: Unable to find a single main class from the following candidates [org.openapitools.OpenApiGeneratorApplication, com.example.demo.DemoApplication] -> [Help 1]

How can I ignore the OpenApiGeneratorApplication.java file during generation? I would like to check out the java class annotated with @SpringBootApplication to have better control.

My pom.xml is configured as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>22</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.2.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-models</artifactId>
            <version>2.2.21</version>
        </dependency>
        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.5.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <configHelp>false</configHelp>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/petstore.yaml
                            </inputSpec>
                            <openapiGeneratorIgnoreList>OpenApiGeneratorApplication.java</openapiGeneratorIgnoreList>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.example.demo.api</apiPackage>
                            <modelPackage>com.example.demo.api.model</modelPackage>
                            <configOptions>
                                <useSpringBoot3>true</useSpringBoot3>
                                <delegatePattern>false</delegatePattern>
                                <interfaceOnly>false</interfaceOnly>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>           
        </plugins>
    </build>

</project>

I tried to use the openapi-generator-ignore file but together with the property openapiGeneratorIgnoreList, but it still generates the class.

For easy testing I will temporarily leave this project to checkout: [git@github.com:legolaz8451/openapi-generator-spring-demo.git](git@github.com:legolaz8451/openapi-generator-spring-demo.git)


Solution

  • To avoid the generation of the @SpringBootApplication class from Spring, one has to add the class to the openapiGeneratorIgnoreList inside the configuration element like this:

    <openapiGeneratorIgnoreList>
      src/main/java/org/openapitools/OpenApiGeneratorApplication.java
    </openapiGeneratorIgnoreList>
    

    source