如何在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
相关产品推荐
相关产品推荐

