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

如何在maven-checkstyle-plugin扫描中排除target目录

解决Maven Checkstyle插件排除target目录的问题

我来帮你搞定这个问题——要把target目录从Checkstyle的扫描范围里排除,只需要在<excludes>标签里填入**/target/**就可以了。

为什么这个写法有效?

Checkstyle的路径匹配是基于项目根目录的,**/target/**这个通配符模式会匹配:

  • 项目根目录下的target文件夹
  • target文件夹里的所有子目录和文件
  • 任何嵌套在其他目录下的target(虽然Maven项目里一般不会有,但这个写法能覆盖所有情况)

完整的插件配置示例

把你原来的pom片段替换成下面的配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <executions>
        <execution>
            <id>checkstyle-check</id>
            <phase>test</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <configLocation>checkstyles.xml</configLocation>
        <failsOnError>true</failsOnError>
        <failOnViolation>true</failOnViolation>
        <consoleOutput>true</consoleOutput>
        <includes>**/*.java,**/*.groovy</includes>
        <excludes>**/target/**</excludes>
    </configuration>
</plugin>

额外注意事项

  • 如果你后续发现还是有target里的文件被扫描,检查一下你的checkstyles.xml配置,有没有在里面定义了额外的包含/排除规则,有时候配置文件里的规则会覆盖pom里的设置。
  • 另外,其实更严谨的写法是只扫描源码目录(比如src/main/**/*.java,src/test/**/*.java),这样从根源上就不会扫到target目录,也能避免意外扫描到其他无关的java文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:54:17