如何配置OpenAPI Generator避免生成冲突的SpringBoot主类
使用Maven的OpenAPI Generator Plugin从OpenAPI文件生成Spring Boot服务端代码时,当interfaceOnly设为false,插件会自动生成带@SpringBootApplication注解的org.openapitools.OpenApiGeneratorApplication类,这和项目中自定义的DemoApplication.java(同样带有@SpringBootApplication注解)产生冲突,构建时出现多主类错误:
[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]
尝试过openapi-generator-ignore文件和openapiGeneratorIgnoreList属性,但该类仍会生成,希望在代码生成阶段忽略OpenApiGeneratorApplication.java。当前pom.xml配置如下:
<?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>
方法一:禁用默认主类生成
在OpenAPI Generator Plugin的configOptions中添加skipDefaultServer参数并设为true,直接阻止生成默认的主类及相关服务器配置:
<configOptions> <useSpringBoot3>true</useSpringBoot3> <delegatePattern>false</delegatePattern> <interfaceOnly>false</interfaceOnly> <skipDefaultServer>true</skipDefaultServer> <!-- 添加该行 --> </configOptions>
这是Spring生成器的专属配置,比文件忽略更直接有效,同时不会影响SpringDocConfiguration类的生成。
方法二:指定项目主类
若需保留生成的主类但明确项目使用自定义主类,可在Spring Boot Maven Plugin中配置mainClass属性:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.demo.DemoApplication</mainClass> <!-- 指定自定义主类 --> </configuration> </plugin>
该方法不会阻止OpenApiGeneratorApplication生成,但构建时会强制使用指定的主类,避免冲突错误。
方法三:排除指定生成文件
部分版本的OpenAPI Generator支持excludeFiles参数,可在插件配置中添加:
<configuration> <!-- 其他原有配置 --> <excludeFiles>src/main/java/org/openapitools/OpenApiGeneratorApplication.java</excludeFiles> </configuration>
需注意填写文件的完整相对生成路径,具体路径可根据插件生成代码的实际位置调整。
内容的提问来源于stack exchange,提问作者Edu

