我正在尝试从swagger codegen maven插件(3.0.8版本(和(组Id:io.swagger.codegen.v3(自动生成类,如下所示。代码生成非常有效,但我想将Generated ApiClient的名称更改为类似PREFIX+ApiClient的名称(例如:customApiClient,其中custom是前缀(。
<build>
<finalName>cdm-customer-servicing-api-client</finalName>
<plugins>
<plugin>
<!-- This 2019 version is required for OpenAPI 3 -->
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.8</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/spec.json</inputSpec>
<language>java</language>
<apiPackage>*****.client.api</apiPackage>
<modelPackage>*******. client.model</modelPackage>
<configOptions>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<artifactVersion>${project.version}</artifactVersion>
<library>resttemplate</library>
<java8>true</java8>
<dateLibrary>java8</dateLibrary>
<licenseName>Apache 2.0</licenseName>
<licenseUrl>https://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
</configOptions>
<additionalProperties>
<property></property>
</additionalProperties>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelDocumentation>false</generateModelDocumentation>
</configuration>
</execution>
</executions>
</plugin>
上面的定义生成了模型,在YML规范中指定了api,没有任何问题。它还生成ApiClient.java,它在所有API类中都是自动连接的。我想更改ApiClient.java的名称,添加一些前缀或后缀。
原因是:我在服务中确实有2个规范,我想确保来自一个服务的ApiClient不会覆盖另一个ApiClient。
如果有人运气好,请告诉我。
可以实现扩展JavaClientCodegen.java
的自定义生成器。
-
使用此结构创建项目(名称由您决定(。骨架可以用
./gradlew clean openApiMeta
创建(找不到等效的mvn(
src/
└── main
├── java
│ └── org
│ └── openapitools
│ └── codegen
│ └── CustomJavaGenerator.java
└── resources
├── customjava
│ └── CustomApiClient.mustache
└── META-INF
└── services
└── org.openapitools.codegen.CodegenConfig
- 自定义JavaGenerator.java
public class CustomJavaGenerator extends JavaClientCodegen implements CodegenConfig {
private final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
private static final String CUSTOM_API_CLIENT_PREFIX = "customApiClientPrefix";
@Override
public void processOpts() {
super.processOpts();
final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
String customPrefix = "DefPref";
if (additionalProperties.containsKey(CUSTOM_API_CLIENT_PREFIX)) {
customPrefix = additionalProperties.get(CUSTOM_API_CLIENT_PREFIX).toString();
LOGGER.warn("client custom name: " + customPrefix + "ApiClient.java" );
}
supportingFiles.removeIf(e -> "ApiClient.java".equals(e.getDestinationFilename()));
supportingFiles.add(new SupportingFile("CustomApiClient.mustache", invokerFolder, customPrefix + "ApiClient.java"));
}
@Override
public String getName() {
return "javaCustom";
}
}
- resources/META-INF/services/org.openapitools.codegen.CodegenConfig
org.openapitools.codegen.CustomJavaGenerator
- 将此文件下载到
resources/customjava/CustomApiClient.mustache
并替换ApiClient
的每个实例(区分大小写(,如图所示
public class {{customApiClientPrefix}}ApiClient { ...
打包并安装项目,使其在mavenLocal repo上显示为例如
customjava-openapi-generator-1.0.0.jar
。生成代码,在这种情况下,使用gradle。
将自定义jar添加到类路径,将openApiGenerate
配置添加到gradle.build。对于maven应该类似。
buildscript {
repositories {
mavenLocal()
jcenter()
maven { url "https://repo1.maven.org/maven2" }
}
dependencies {
// other dependencies
// custom generator
classpath "org.openapitools:customjava-openapi-generator:1.0.0"
classpath "org.openapitools:openapi-generator-gradle-plugin:5.1.0"
}
}
repositories {
jcenter()
}
apply plugin: 'org.openapi.generator'
openApiGenerate {
//verbose = true
generatorName = "javaCustom"
inputSpec = "$rootDir/generate/proj-swa01-1.0.0-resolved.yaml".toString()
outputDir = "$buildDir/generated".toString()
apiPackage = "org.openapi.example.api"
invokerPackage = "org.openapi.example.invoker"
modelPackage = "org.openapi.example.model"
templateDir = "customjava"
configOptions = [
dateLibrary: "java8"
]
additionalProperties = [
customApiClientPrefix: "Swa"
]
}
- Gradle命令:
./gradlew clean openApiGenerate