Swagger Codegen 选择性生成
您可能不想在项目中生成所有模型。同样,您可能只想编写一两个 API。如果是这种情况,您可以使用系统属性来控制输出
默认情况下,会生成特定库支持的所有内容。启用某个功能后,它会限制生成的内容
1# generate only models2java -Dmodels {opts}3
4# generate only apis5java -Dapis {opts}6
7# generate only supporting files8java -DsupportingFiles9
10# generate models and supporting files11java -Dmodels -DsupportingFiles
要控制生成的特定文件,您可以传递一个您想要的 CSV 列表
1# generate the User and Pet models only2-Dmodels=User,Pet3
4# generate the User model and the supportingFile `StringUtil.java`:5-Dmodels=User -DsupportingFiles=StringUtil.java
要控制 API 和模型的文档和测试的生成,请将 false 传递给该选项。对于 API,这些选项是 -DapiTests=false
和 -DapiDocs=false
。对于模型,是 -DmodelTests=false
和 -DmodelDocs=false
。这些选项默认为 true,并且不限制上面列出的功能选项的生成(例如 -Dapi
)
1# generate only models (with tests and documentation)2java -Dmodels {opts}3
4# generate only models (with tests but no documentation)5java -Dmodels -DmodelDocs=false {opts}6
7# generate only User and Pet models (no tests and no documentation)8java -Dmodels=User,Pet -DmodelTests=false {opts}9
10# generate only apis (without tests)11java -Dapis -DapiTests=false {opts}12
13# generate only apis (modelTests option is ignored)14java -Dapis -DmodelTests=false {opts}
使用选择性生成时,只会使用特定生成所需的模板。
忽略文件格式
Swagger Codegen 支持 .swagger-codegen-ignore
文件,类似于您可能已经熟悉的 .gitignore
或 .dockerignore
。
与 --skip-overwrite
标志相比,忽略文件可以更好地控制覆盖现有文件。使用忽略文件,您可以指定可以忽略的单个文件或目录。这可能很有用,例如,如果您只想要生成代码的子集。
示例
1# Swagger Codegen Ignore2# Lines beginning with a # are comments3
4# This should match build.sh located anywhere.5build.sh6
7# Matches build.sh in the root8/build.sh9
10# Exclude all recursively11docs/**12
13# Explicitly allow files excluded by other rules14!docs/UserApi.md15
16# Recursively exclude directories named Api17# You can't negate files below this directory.18src/**/Api/19
20# When this file is nested under /Api (excluded above),21# this rule is ignored because parent directory is excluded by previous rule.22!src/**/PetApiTests.cs23
24# Exclude a single, nested file explicitly25src/IO.Swagger.Test/Model/AnimalFarmTests.cs
.swagger-codegen-ignore
文件必须存在于输出目录的根目录中。
首次代码生成时,您还可以传递 CLI 选项 --ignore-file-override=/path/to/ignore_file
以更好地控制生成的输出。请注意,这是一个完全覆盖,并且在重新生成代码时将覆盖输出目录中的 .swagger-codegen-ignore
文件。
IntelliJ 中可通过 .ignore 插件获得对 .swagger-codegen-ignore
文件的编辑器支持。