以下示例描述了 Checkstyle 插件的基本用法。
要生成 Checkstyle 报告作为项目报告的一部分,请在pom.xml的<reporting>部分添加 Checkstyle 插件。
<project>
...
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<reportSets>
<reportSet>
<reports>
<report>checkstyle</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
...
</project>
然后,执行站点阶段以生成报告。
mvn site
您还可以通过从命令行显式执行checkstyle:checkstyle目标来生成 Checkstyle 报告。除非您想使用特定配置,否则您不需要在pom.xml中指定 Checkstyle 插件。
mvn checkstyle:checkstyle
如果您想向控制台报告或构建失败,您必须将checkstyle::check的执行添加到<build>元素并配置您需要的任何选项。
(请注意,对于 Maven 3,根据Maven 3 兼容性说明,在 Maven 3 中,您在<reporting>元素中设置的选项对<build>元素中的执行没有任何影响。)
例如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>