跳到內容

Swagger Codegen 選擇性產生

您可能不希望在專案中產生所有模型。同樣地,您可能只想撰寫一兩個 API。如果這樣,您可以使用系統屬性來控制輸出。

預設值是產生特定程式庫支援的所有內容。一旦您啟用某項功能,它將限制產生的內容。

終端機視窗
1
# generate only models
2
java -Dmodels {opts}
3
4
# generate only apis
5
java -Dapis {opts}
6
7
# generate only supporting files
8
java -DsupportingFiles
9
10
# generate models and supporting files
11
java -Dmodels -DsupportingFiles

若要控制產生的特定檔案,您可以傳遞您想要的 CSV 清單。

終端機視窗
1
# generate the User and Pet models only
2
-Dmodels=User,Pet
3
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)
2
java -Dmodels {opts}
3
4
# generate only models (with tests but no documentation)
5
java -Dmodels -DmodelDocs=false {opts}
6
7
# generate only User and Pet models (no tests and no documentation)
8
java -Dmodels=User,Pet -DmodelTests=false {opts}
9
10
# generate only apis (without tests)
11
java -Dapis -DapiTests=false {opts}
12
13
# generate only apis (modelTests option is ignored)
14
java -Dapis -DmodelTests=false {opts}

使用選擇性產生時,只會使用特定產生所需的範本。

忽略檔案格式

Swagger Codegen 支援 .swagger-codegen-ignore 檔案,類似於您可能已熟悉的 .gitignore.dockerignore

忽略檔案允許比 --skip-overwrite 旗標更好地控制覆寫現有檔案。使用忽略檔案,您可以指定可以忽略的個別檔案或目錄。如果您只想要產生程式碼的子集,這會很有用。

範例

終端機視窗
1
# Swagger Codegen Ignore
2
# Lines beginning with a # are comments
3
4
# This should match build.sh located anywhere.
5
build.sh
6
7
# Matches build.sh in the root
8
/build.sh
9
10
# Exclude all recursively
11
docs/**
12
13
# Explicitly allow files excluded by other rules
14
!docs/UserApi.md
15
16
# Recursively exclude directories named Api
17
# You can't negate files below this directory.
18
src/**/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.cs
23
24
# Exclude a single, nested file explicitly
25
src/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 檔案的編輯器支援。