Check Style 是一個相當好用的程式碼檢測工具,主要用來 code review 是否有不符合code standard 的部份。
但它並沒有產生整體報告的功能,因此當面對大量檔案需要 code review,還是必須花上不少時間來整理。
Apache Maven Checkstyle Plugin(amcp) 剛好可以補足其缺少報告的部份。
以下介紹如何安裝以及產生報告
首先安裝 Maven (由於 amcp 的要求,Maven 版本必須是第3版,參考這裡)

sudo apt-get install maven

接著下載該套件

wget http://apache.stu.edu.tw/maven/plugins/maven-checkstyle-plugin-2.17-source-release.zip

解壓縮

7z x maven-checkstyle-plugin-2.17-source-release.zip

移動到plugin目錄下並安裝,安裝過程中會自動下載相關套件。

cd maven-checkstyle-plugin-2.17
mvn install

完成後就可以開始使用了。
首先建立測試資料夾

mkdir test

移動到測試資料夾中並建立src資料夾,src資料夾存放要檢查的source code

cd test
mkdir src

在 src 中建立一個簡單的 SimpleObject.java

package com.foxx.char6_7;
public class SimpleObject
{
    private int mId;
    private String mName;
    public SimpleObject(int id, String name)
    {
        mId = id;
        mName = name;
    }
    public int getId()
    {
        return mId;
    }
    public void setId(int id)
    {
        mId = id;
    }
    public String getName()
    {
        return mName;
    }
    public void setName(String name)
    {
        mName = name;
    }
}

接著移動到test資料夾並建立pom.xml

cd ../
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>foxx.test</groupId>
  <artifactId>example_for_generate_checkstyle_report</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- custome checkstyle rules location
    <checkstyle.config.location>/full/path/of/your/checkstyle/rule/file</checkstyle.config.location> -->
  </properties>
<build>
  <sourceDirectory>src</sourceDirectory>
</build>
<reporting>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-checkstyle-plugin</artifactId>
				<version>2.17</version>
			</plugin>
		</plugins>
	</reporting>
</project>

pom.xml 有幾點需要注意。
第10~11行指定 checkstyle 的 rule file , 如果沒有指定預設為使用sun checkstyle rule。
第16行指定檢查的資料夾,該資料夾下所有的.java都會被檢查。
第19~27行指定產生報告用的plugin , 即為amcp。
建立完 pom.xml 之後,就可以執行amcp。
移動 test 資料夾中並輸入

mvn clean checkstyle:check

clean 為清除之前建立的檔案(主要是target資料夾),而 checkstyle:check 為執行checkstyle的檢查並產生報告
完成後在 test 資料夾中會產生 target 資料夾,而報告則是 checkstyle-result.xml 檔案, e.g.

<checkstyle version="6.11.2"><file name="/home/foxxtseng/temp/maven_and_checkstyle/test/src/SimpleObject.java"><error line="0" severity="error" message="Missing package-info.java file." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck"/><error line="3" severity="error" message="Missing a Javadoc comment." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTypeCheck"/><error line="4" column="1" severity="error" message="'{' at column 1 should be on the previous line." source="com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck"/><error line="5" column="5" severity="error" message="Missing a Javadoc comment." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck"/><error line="6" column="5" severity="error" message="Missing a Javadoc comment." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck"/><error line="7" severity="error" message="Line has trailing spaces." source="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck"/><error line="8" column="5" severity="error" message="Missing a Javadoc comment." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck"/><error line="8" column="25" severity="error" message="Parameter id should be final." source="com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck"/><error line="8" column="33" severity="error" message="Parameter name should be final." source="com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck"/><error line="9" column="5" severity="error" message="'{' at column 5 should be on the previous line." source="com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck"/><error line="14" column="5" severity="error" message="Method 'getId' is not designed for extension - needs to be abstract, final or empty." source="com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck"/><error line="14" column="5" severity="error" message="Missing a Javadoc comment." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck"/><error line="15" column="5" severity="error" message="'{' at column 5 should be on the previous line." source="com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck"/><error line="19" column="5" severity="error" message="Method 'setId' is not designed for extension - needs to be abstract, final or empty." source="com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck"/><error line="19" column="5" severity="error" message="Missing a Javadoc comment." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck"/><error line="19" column="23" severity="error" message="Parameter id should be final." source="com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck"/><error line="20" column="5" severity="error" message="'{' at column 5 should be on the previous line." source="com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck"/><error line="24" column="5" severity="error" message="Method 'getName' is not designed for extension - needs to be abstract, final or empty." source="com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck"/><error line="24" column="5" severity="error" message="Missing a Javadoc comment." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck"/><error line="25" column="5" severity="error" message="'{' at column 5 should be on the previous line." source="com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck"/><error line="29" column="5" severity="error" message="Method 'setName' is not designed for extension - needs to be abstract, final or empty." source="com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck"/><error line="29" column="5" severity="error" message="Missing a Javadoc comment." source="com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck"/><error line="29" column="25" severity="error" message="Parameter name should be final." source="com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck"/><error line="30" column="5" severity="error" message="'{' at column 5 should be on the previous line." source="com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck"/><error line="33" severity="error" message="Line has trailing spaces." source="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck"/><error line="34" severity="error" message="Line has trailing spaces." source="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck"/></file></checkstyle>

預設報告的格式並不容易查看,換個指令。

mvn clean site

site 指令會另外產生一個html格式的報告,報告位於 target/site/checkstyle.html  。
html格式的報告相當容易查看。
螢幕擷圖存為 2016-03-16 10:07:41
使用 amcp 最大的好處在於需要 code review 的時候,先把需要check的檔案checkout下來,再 run amcp ,
會自動檢查所有的檔案並產生報告,不用一個一個抓violation, 抓到天昏地暗了。