You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下openapi-generator-maven-plugin无法识别Mustache模板

解决OpenAPI Generator Maven插件忽略自定义模板的问题

针对你在Windows环境下使用openapi-generator-maven-plugin 6.6.0时遇到的自定义模板被忽略的问题,按以下步骤排查修复:

1. 确保模板目录结构符合生成器要求

openapi-generator会根据你指定的generatorName(jaxrs-jersey或jaxrs-spec)去对应子目录查找模板,而非直接读取templates根目录下的文件。你需要按如下结构放置模板:

  • 若生成jaxrs-jersey代码:
    src/main/resources/templates/
    └── jaxrs-jersey/
        └── api.mustache
    
  • 若生成jaxrs-spec代码:
    src/main/resources/templates/
    └── jaxrs-spec/
        └── api.mustache
    

2. 修正pom.xml中的插件配置

6.6.0版本的插件仅需配置templateDirectory参数(templateResourcePath用于从类路径加载模板,不适用本地文件系统路径)。正确的配置示例:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.6.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                <generatorName>jaxrs-jersey</generatorName> <!-- 替换为jaxrs-spec按需选择 -->
                <templateDirectory>${project.basedir}/src/main/resources/templates</templateDirectory>
                <!-- 补充你的其他配置:如packageName、apiPackage等 -->
            </configuration>
        </execution>
    </executions>
</plugin>

3. 验证模板加载路径

执行Maven命令时加上-X参数开启调试,查看日志中是否存在类似如下的模板加载记录:

Loading template from: file:/C:/workspace/path/project/submodule/src/main/resources/templates/jaxrs-jersey/api.mustache

若未出现该记录,说明目录结构或配置仍有问题,需重新检查路径和子目录名称。

4. 清理缓存并重新生成

执行mvn clean generate-sources命令,清理之前生成的代码和Maven缓存,确保新模板被正确加载。

内容的提问来源于stack exchange,提问作者Wieland Gmeiner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 15:10:11