从 2.12 版开始,可以直接在pom.xml中插件本身的配置部分定义内联检查器配置。
当每次编译都应该执行 Checkstyle 检查并且您不喜欢为您的 Checkstyle 规则创建自己的项目并且不喜欢拥有单独的父 POM 结构时,这尤其有用。
要在pom.xml中定义自定义 Checkstyle 检查器配置,请使用checkstyleRules参数。
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.1.2</version> <executions> <execution> <id>verify-style</id> <phase>process-classes</phase> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <logViolationsToConsole>true</logViolationsToConsole> <checkstyleRules> <module name="Checker"> <!-- Checks for Size Violations. --> <!-- See http://checkstyle.sf.net/config_sizes.html --> <module name="FileLength"> <property name="max" value="3500" /> <property name="fileExtensions" value="java"/> </module> <!-- Checks for whitespace --> <!-- See http://checkstyle.sf.net/config_whitespace.html --> <module name="FileTabCharacter"/> <module name="TreeWalker"> <module name="StaticVariableName"/> <module name="TypeName"> <property name="format" value="^_?[A-Z][a-zA-Z0-9]*$"/> </module> </module> </module> </checkstyleRules> </configuration> </plugin> </plugins> </build> ... </project>
内联配置通过将配置写入文件并将该文件传递给 checkstyle 来工作。此配置文件必须包含特定的标头。
我们默认包含一个与包含的 checkstyle 版本兼容的版本。
当您使用与包含的版本不同的 checkstyle 版本时,您可能需要指定不同的标题:
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.1.2</version> <executions> <execution> <id>verify-style</id> <phase>process-classes</phase> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <logViolationsToConsole>true</logViolationsToConsole> <checkstyleRules> <module name="Checker"> <!-- Checks for Size Violations. --> <!-- See http://checkstyle.sf.net/config_sizes.html --> <module name="FileLength"> <property name="max" value="3500" /> <property name="fileExtensions" value="java"/> </module> <!-- Checks for whitespace --> <!-- See http://checkstyle.sf.net/config_whitespace.html --> <module name="FileTabCharacter"/> <module name="TreeWalker"> <module name="StaticVariableName"/> <module name="TypeName"> <property name="format" value="^_?[A-Z][a-zA-Z0-9]*$"/> </module> </module> </module> </checkstyleRules> <checkstyleRulesHeader> <![CDATA[ <!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd"> ]]> </checkstyleRulesHeader> </configuration> <dependencies> <dependency> <groupId>com.puppycrawl.tools</groupId> <artifactId>checkstyle</artifactId> <version>8.15</version> </dependency> </dependencies> </plugin> </plugins> </build> ... </project>