我使用 OpenApi 3.0 和 maven 插件 openapi-generator-maven-plugin 来生成我的 api + 对象。
这是我的 Maven 配置:
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/BookingService.yaml</inputSpec>
<generatorName>spring</generatorName>
<modelPackage>${clientPackage}.model</modelPackage>
<invokerPackage>${clientPackage}.invoker</invokerPackage>
<apiPackage>${clientPackage}.api</apiPackage>
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<configOptions>
<delegatePattern>true</delegatePattern>
</configOptions>
</configuration>
</execution>
它按预期工作,但它也生成我不想要的测试。正如你在我的配置中看到的,我禁用了 Api 测试 + 模型测试的测试..
这些测试的编译失败,因为构建目标文件夹中出现“无法解析符号‘SpringBootTest’”...
这些测试没有任何意义,我怎样才能禁用它们?
请注意,这是一种解决方法。最好获得一个不生成此测试用例的属性,但目前这似乎有效......
将此 Maven 插件添加到您的 pom.xml 中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- remove the unwanted generated testcases by the spring generator of openapi -->
<delete dir="${project.build.directory}/generated-sources/openapi/src/test" />
</target>
</configuration>
</execution>
</executions>
</plugin>
在流程源阶段生成源后,它将删除整个测试包
我们可以通过启用
<interfaceOnly>true</interfaceOnly>
来跳过生成,但delegatePattern
不会考虑。
这是来自 openapi-generator 的源代码... 源代码
if (!interfaceOnly) {
if (SPRING_BOOT.equals(library)) {
if (useSwaggerUI && selectedDocumentationProviderRequiresSwaggerUiBootstrap()) {
supportingFiles.add(new SupportingFile("swagger-ui.mustache", "src/main/resources/static", "swagger-ui.html"));
}
// rename template to SpringBootApplication.mustache
supportingFiles.add(new SupportingFile("openapi2SpringBoot.mustache",
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
"OpenApiGeneratorApplication.java"));
supportingFiles.add(new SupportingFile("SpringBootTest.mustache",
(testFolder + File.separator + basePackage).replace(".", java.io.File.separator),
"OpenApiGeneratorApplicationTests.java"));
supportingFiles.add(new SupportingFile("RFC3339DateFormat.mustache",
(sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
"RFC3339DateFormat.java"));
}
....
您始终可以在 Intellij IDE 中排除生成的测试包以解决本地编译问题。
详细回复请参见这里。
但是,简单的例子是:
<?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>
<groupId>com.kinlhp</groupId>
<artifactId>moname-build</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>Project to money me with no name [build].</description>
<url>https://moname.kinlhp.com</url>
<properties>
<main.basedir>${basedir}</main.basedir>
<maven.compiler.release>${java.version}</maven.compiler.release>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<openapi-generator.base-package>com.kinlhp.moname.commons.api.oas</openapi-generator.base-package>
<openapi-generator.input-spec>${project.basedir}/src/main/resources/openapi/specification.yaml</openapi-generator.input-spec>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>${project.build.sourceEncoding}</project.reporting.outputEncoding>
<!-- dependency versions -->
<revision>1.0.0.BUILD-SNAPSHOT</revision>
<!-- # third party # -->
<java.version>17</java.version>
<openapi-generator-maven-plugin.version>6.6.0</openapi-generator-maven-plugin.version>
</properties>
<build>
<pluginManagement>
<plugins>
<!-- # third party # -->
<!-- openapitools -->
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<id>openapi-generator</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- README#openapi-generator-spring-metadata -->
<generatorName>spring</generatorName>
<!-- README#openapi-generator-spring-config-options -->
<apiPackage>${openapi-generator.base-package}.endpoint</apiPackage>
<artifactId>moname-commons-api-oas</artifactId>
<artifactVersion>${revision}</artifactVersion>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<groupId>${project.groupId}</groupId>
<ignoreFileOverride>.openapi-generator-ignore</ignoreFileOverride>
<inputSpec>${openapi-generator.input-spec}</inputSpec>
<invokerPackage>${openapi-generator.base-package}.invoker</invokerPackage>
<modelNameSuffix>DTO</modelNameSuffix>
<modelPackage>${openapi-generator.base-package}.payload</modelPackage>
<configOptions>
<!-- README#openapi-generator-spring-metadata -->
<generatorLanguage>Java</generatorLanguage>
<generatorType>SERVER</generatorType>
<!-- README#openapi-generator-spring-config-options -->
<artifactDescription>Project to money me with no name [commons-api-oas].</artifactDescription>
<artifactUrl>${project.url}</artifactUrl>
<basePackage>${openapi-generator.base-package}</basePackage>
<bigDecimalAsString>true</bigDecimalAsString>
<booleanGetterPrefix>is</booleanGetterPrefix>
<configPackage>${openapi-generator.base-package}.configuration</configPackage>
<delegatePattern>true</delegatePattern>
<developerEmail>[email protected]</developerEmail>
<developerName>Luis Henrique Pereira</developerName>
<developerOrganization>${project.organization.name}</developerOrganization>
<developerOrganizationUrl>${project.organization.url}</developerOrganizationUrl>
<licenseName>MIT License</licenseName>
<licenseUrl>https://www.opensource.org/licenses/mit-license.php</licenseUrl>
<parentArtifactId>moname-commons</parentArtifactId>
<parentGroupId>${project.parent.groupId}</parentGroupId>
<parentVersion>${revision}</parentVersion>
<performBeanValidation>true</performBeanValidation>
<scmConnection>${project.scm.connection}</scmConnection>
<scmDeveloperConnection>${project.scm.developerConnection}</scmDeveloperConnection>
<scmUrl>${project.scm.url}</scmUrl>
<serializableModel>true</serializableModel>
<title>Project to money me with no name [commons-api-oas].</title>
<unhandledException>true</unhandledException>
<useSpringBoot3>true</useSpringBoot3>
<useSpringController>true</useSpringController>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>