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

如何用Maven插件仅格式化target下OpenAPI生成的Java代码?

仅格式化Target目录下生成代码的Maven配置方案

方案一:调整maven-formatter-plugin配置

如果之前使用的是formatter-maven-plugin,可以通过精准配置includes和excludes限制仅处理target下的生成代码,避免格式化src目录的源码。以下是正确的pom.xml配置示例:

<plugin>
    <groupId>net.revelc.code.formatter</groupId>
    <artifactId>formatter-maven-plugin</artifactId>
    <version>2.22.0</version>
    <executions>
        <execution>
            <id>format-generated-code</id>
            <!-- 确保在代码生成之后执行,比如process-classes阶段 -->
            <phase>process-classes</phase>
            <goals>
                <goal>format</goal>
            </goals>
            <configuration>
                <!-- 引入Google Java风格配置文件 -->
                <configFile>${project.basedir}/google-java-format.xml</configFile>
                <!-- 仅包含target下的OpenAPI生成代码,根据实际生成路径调整 -->
                <includes>
                    <include>target/generated-sources/openapi/**/*.java</include>
                </includes>
                <!-- 排除src目录下的所有源码 -->
                <excludes>
                    <exclude>src/main/java/**/*.java</exclude>
                    <exclude>src/test/java/**/*.java</exclude>
                </excludes>
                <!-- 禁用默认排除规则,确保仅按配置处理 -->
                <useDefaultExcludes>false</useDefaultExcludes>
            </configuration>
        </execution>
    </executions>
</plugin>

注意:

  • 确认target/generated-sources/openapi是否与你的OpenAPI插件实际生成路径一致,若生成到其他子目录,需修改include路径。
  • google-java-format.xml可使用官方标准配置,或自行调整规则。

方案二:使用spotless-maven-plugin

spotless-maven-plugin是更灵活的代码格式化工具,原生支持Google Java风格,无需额外配置文件,且能精准控制处理路径。配置示例如下:

<plugin>
    <groupId>com.diffplug.spotless</groupId>
    <artifactId>spotless-maven-plugin</artifactId>
    <version>2.37.0</version>
    <executions>
        <execution>
            <id>format-generated-code</id>
            <phase>process-classes</phase>
            <goals>
                <goal>apply</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <java>
            <!-- 指定Google Java格式化版本 -->
            <googleJavaFormat>1.17.0</googleJavaFormat>
            <!-- 仅处理target下的生成代码 -->
            <includes>
                <include>target/generated-sources/openapi/**/*.java</include>
            </includes>
            <!-- 排除src目录源码 -->
            <excludes>
                <exclude>src/main/java/**/*.java</exclude>
                <exclude>src/test/java/**/*.java</exclude>
            </excludes>
        </java>
    </configuration>
</plugin>

优势:

  • 无需额外维护格式配置文件,直接指定版本即可应用标准Google风格。
  • 支持增量格式化,仅处理变更文件,执行速度更快。

通用验证步骤

配置完成后,可通过以下命令测试格式化效果:

  • 对于formatter-maven-plugin:mvn formatter:format
  • 对于spotless-maven-plugin:mvn spotless:apply

查看控制台输出,确认仅处理target目录下的生成代码,未涉及src目录文件即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 23:25:24