我正在使用swagger 2.0规范和swagger-codegen-maven-plugin v.2.2.3
在我的项目中,我使用小型JSON文件来标识模型类,例如
abstract.json
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "A part of model"
},
"paths": {},
"definitions": {
"A": {
"title": "A",
"type": "object",
"properties": {
"Aproperty": {
"type": "string",
"description": "Property which can be used in other classes"
}
}
}
}
}
real1.json
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "B part of model"
},
"paths": {},
"definitions": {
"B": {
"title": "B",
"type": "object",
"properties": {
"Bproperty": {
"$ref": "./abstract.json#/definitions/A"
}
}
}
}
}
real2.json
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "C part of model"
},
"paths": {},
"definitions": {
"C": {
"title": "C",
"type": "object",
"properties": {
"Cproperty": {
"$ref": "./abstract.json#/definitions/A"
}
}
}
}
}
通过这种方式,其他组件可以将相同的模型用作其他组件中对象的一部分。它工作正常并且受swagger规范2.0支持。 但是问题开始于尝试使用带有不同包的swagger-codegen-maven-plugin生成Java类 例如,我使用类似
的配置<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-A-model</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger/abstract.json
</inputSpec>
<language>io.swagger.codegen.languages.JavaClientCodegen</language>
<library>feign</library>
<environmentVariables>
<models></models>
<modelDocs>false</modelDocs>
<supportingFiles>false</supportingFiles>
<apis>false</apis>
<apiTests>false</apiTests>
</environmentVariables>
<modelPackage>com.project.model.external.abstract</modelPackage>
<configOptions>
<sourceFolder>.</sourceFolder>
</configOptions>
<output>${project.build.directory}/generated-sources/swagger</output>
<addCompileSourceRoot>true</addCompileSourceRoot>
</configuration>
</execution>
<execution>
<id>generate-B-model</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger/real1.json
</inputSpec>
<language>io.swagger.codegen.languages.JavaClientCodegen</language>
<library>feign</library>
<environmentVariables>
<models></models>
<modelDocs>false</modelDocs>
<supportingFiles>false</supportingFiles>
<apis>false</apis>
<apiTests>false</apiTests>
</environmentVariables>
<modelPackage>com.project.model.external.real1</modelPackage>
<configOptions>
<sourceFolder>.</sourceFolder>
</configOptions>
<output>${project.build.directory}/generated-sources/swagger</output>
<addCompileSourceRoot>true</addCompileSourceRoot>
</configuration>
</execution>
<execution>
<id>generate-C-model</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger/real2.json
</inputSpec>
<language>io.swagger.codegen.languages.JavaClientCodegen</language>
<library>feign</library>
<environmentVariables>
<models></models>
<modelDocs>false</modelDocs>
<supportingFiles>false</supportingFiles>
<apis>false</apis>
<apiTests>false</apiTests>
</environmentVariables>
<modelPackage>com.project.model.external.real2</modelPackage>
<configOptions>
<sourceFolder>.</sourceFolder>
</configOptions>
<output>${project.build.directory}/generated-sources/swagger</output>
<addCompileSourceRoot>true</addCompileSourceRoot>
</configuration>
</execution>
在三个软件包中,我将拥有三个相同的类A.java
有可能在抽象包中仅包含一个A类,而不是使用swagger-codegen-maven-plugin获得树形重复文件吗?