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
檔案的編輯器支援。