CloudStack在4.4版本中,加入了自动代码审查功能。对于不满足code-style的代码,在使用maven编译的过程中,会报错导致无法继续编译。
详细研究了一下代码审查功能的实现机制,总结至此。
./tools/checkstyle/src/main/resources/cloud-style.xml

<?xml version="1.0"?>
<!-- Licensed to the Apache Software Foundation (ASF) ...... License. -->
<!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="FileTabCharacter">
    <property name="eachLine" value="true" />
  </module>

  <module name="TreeWalker">
    <module name="LineLength">
      <property name="max" value="1024" />
    </module>

    <module name="RedundantImport" />
    <module name="UnusedImports" />
    <module name="MemberName">
      <property name="format" value="^_?[a-zA-Z0-9]*$" />
    </module>

	<module name="LocalFinalVariableName">
	  <property name="format" value="^[a-zA-Z][a-zA-Z0-9_]*$" />
	</module>

	<module name="StaticVariableName">
	  <property name="format" value="^(s_)?[a-z][a-zA-Z0-9]*$"/>
	</module>

	<module name="ConstantName">
	  <property name="format" value="^[a-zA-Z][a-zA-Z0-9_]*$"/>
	</module>

    <module name="PackageName" />
    <module name="ParameterName" />
    <module name="TypeName" />
    <module name="AvoidStarImport" />
  </module>

  <module name="RegexpSingleline">
    <!-- \s matches whitespace character, $ matches end of line. -->
    <property name="format" value="\s+$" />
    <property name="message" value="Line has trailing spaces." />
  </module>
  <!-- some modules that we should soon add <module name="MagicNumber"/> -->
  <!-- some modules that we should soon add -->
</module>

该文件定义了code-style检查模块。内容是一个嵌套的module集合。每个module中,可以嵌套其它module或propertiy,property是module属性,用于标记检查的格式和错误提示。

顶层module

定义代码检查module。 # 第二层module 嵌套了3个module 1、 2、 3、 后续会添加和其他的module # 第三层module ## FileTabCharacter ```xml ``` ## TreeWalker 1、 ```xml ``` 单行最大不超过1024个字符 2、 检查是否有重复的import 3、 检查是否有未使用的import 4、 ```xml ``` 检查成员变量命名是否遵循命名规则:以下划线开始,名字只包含英文大小写和阿拉伯数字 5、 ```xml ``` 检查final变量命名是否遵循命名规则:以英文大小写字符开始,名字只包含英文大小写和阿拉伯数字 6、 ```xml ``` 检查static变量命名是否遵循命名规则:以s\_开始,名字只包含英文大小写和阿拉伯数字 7、 ```xml ``` 检查常量命名是否遵循命名规则:名字只包含英文大小写和阿拉伯数字 8、 9、 10、 11、 ## RegexpSingleline ```xml ``` format:空行是否有空格,或者每行末尾是否有空格 message:如果检查空行有空格或者每行末尾有空格,则返回错误值:Line has trailing spaces.