如何在Swagger CodeGen多API端点生成时排除Client文件夹?
Great question—dealing with duplicate Client folder boilerplate (like Configuration, ApiClient, ApiException) when generating multiple API clients is a super common pain point. Manual deletion and namespace mapping gets old fast, so here are several cleaner, scalable alternatives:
1. Centralize Common Code in a Shared Library
This is the most straightforward sustainable fix. Configure Swagger CodeGen to generate all shared boilerplate into a single reusable library, then have each individual API client depend on this shared module instead of generating its own copies.
Here’s how to implement it (using Java as an example):
- First, generate the shared core library with your common boilerplate:
swagger-codegen generate -i first-api-spec.yaml -l java -o ./shared-client \ --invoker-package com.yourcompany.shared.client \ --api-package com.yourcompany.firstapi \ --model-package com.yourcompany.firstapi.models - For subsequent API clients, reuse the shared package and skip overwriting existing core files:
swagger-codegen generate -i second-api-spec.yaml -l java -o ./second-api-client \ --invoker-package com.yourcompany.shared.client \ --api-package com.yourcompany.secondapi \ --model-package com.yourcompany.secondapi.models \ --skip-overwrite
The second API client will now reference the shared Configuration and ApiClient classes instead of generating duplicates.
2. Customize CodeGen Templates
Swagger CodeGen uses Mustache templates to generate code. You can modify these templates to force all API clients to reference a single shared instance of the boilerplate classes instead of generating their own.
- Locate the template files for your target language (e.g.,
src/main/resources/Javafor Java generators). - Edit templates like
ApiClient.mustacheorConfiguration.mustacheto replace inline class definitions with imports from your shared package. - Use the
--template-dirflag to point CodeGen to your customized templates when generating clients:swagger-codegen generate -i api-spec.yaml -l java -o ./api-client --template-dir ./custom-templates
This ensures every generated client uses the same shared boilerplate, eliminating duplicates entirely.
3. Merge API Specs (If Feasible)
If your APIs are part of the same ecosystem, consider merging their OpenAPI specs into a single file before generation. This way, CodeGen will produce one unified client with only one set of boilerplate files.
You can manually combine specs or use tools like swagger-cli to merge them programmatically:
swagger-cli merge api1.yaml api2.yaml -o combined-api.yaml
Then generate a single client from the merged spec—no more duplicate boilerplate to worry about.
4. Automate Cleanup with Post-Generation Scripts
If the above options don’t fit your workflow, write a simple script (Bash, Python, etc.) to automate the cleanup of duplicate boilerplate after generation. For example:
- Check if the shared boilerplate files already exist in a central location.
- Delete duplicate files from the newly generated API client directory.
- Update import statements in the generated API classes to point to the shared files.
This removes the manual effort while keeping your workflow flexible.
内容的提问来源于stack exchange,提问作者jonmeyer

