可以定义检查器的自定义属性扩展定义。
Checkstyle 使用属性扩展机制来允许将特定值注入到预定义的 Checker 配置中。单独使用属性扩展不足以自定义 Checkstyle 执行的检查。
示例:checkstyle.xml - 仅检查正在使用的包名称以确保它符合所需的方案,但允许此检查器的用户指定他们自己的项目名称。
<?xml version="1.0" ?> <!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd"> <module name="Checker"> <module name="TreeWalker"> <module name="PackageName"> <property name="format" value="com\.example\.$\{projectname\}(\.[a-z][a-zA-Z0-9]+)*$"/> </module> </module> </module>
示例:pom.xml - 指定Checkstyle 在上述 Checker 配置中遇到此类变量时将使用的projectname属性扩展。
<project> ... <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>2.4</version> <configuration> <configLocation>checkstyle.xml</configLocation> <propertyExpansion>projectname=whizbang</propertyExpansion> </configuration> </plugin> </plugins> </reporting> ... </project>
属性扩展信息也可以来自使用propertiesLocation参数的位置,如下所示。propertiesLocation可以指向 URL、文件或构建类路径资源引用。
<project> ... <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>2.4</version> <configuration> <configLocation>checkstyle.xml</configLocation> <propertesLocation>${basedir}/checkstyle.properties</propertiesLocation> </configuration> </plugin> </plugins> </reporting> ... </project>