maven與checkstyle集成

鏈接:http://blog.csdn.net/kongxx/article/details/7750015

最近在整理maven工程,打算加入一次對代碼檢查和測試覆蓋率檢查的功能,因此想到了maven集成一些常用檢查工具的插件,下面就來介紹幾個常用插件的用法。

首先是介紹Checkstyle插件的集成,要添加Checkstyle插件,需要修改工程的pom.xml文件,添加以下插件配置

[html] view plaincopy
  1. <project>  
  2.     ...  
  3.     <properties>  
  4.         <checkstyle.config.location>config/maven_checks.xml</checkstyle.config.location>  
  5.     </properties>  
  6.     ...  
  7.     <reporting>  
  8.         <plugins>  
  9.             <plugin>  
  10.                 <groupId>org.apache.maven.plugins</groupId>  
  11.                 <artifactId>maven-checkstyle-plugin</artifactId>  
  12.                 <version>2.9.1</version>  
  13.             </plugin>  
  14.   
  15.             <plugin>  
  16.                 <groupId>org.apache.maven.plugins</groupId>  
  17.                 <artifactId>maven-jxr-plugin</artifactId>  
  18.                 <version>2.3</version>  
  19.             </plugin>  
  20.         </plugins>  
  21.     </reporting>  
  22.     ...  
  23. </project>  
1. 其中可以修改使用的檢查規則文件路徑,插件默認提供了四個規則文件可以直接使用,不要手動下載,它們分別是:

[plain] view plaincopy
  1. * config/sun_checks.xml - Sun Microsystems Definition (default).  
  2. * config/maven_checks.xml - Maven Development Definitions.  
  3. * config/turbine_checks.xml - Turbine Development Definitions.  
  4. * config/avalon_checks.xml - Avalon Development Definitions.  
2. 也可以使用自定義的規則文件,比如自定義一個文件名爲my_checks.xml,並放在工程根目錄下,然後修改配置爲如下:

[html] view plaincopy
  1. <properties>  
  2.     <checkstyle.config.location>my_checks.xml</checkstyle.config.location>  
  3. </properties>  
3. 另外,這裏也添加了jxr插件,用來在生成的結果中可以通過link找到代碼對應的行。

4. checkstyle插件的可執行任務如下:

[plain] view plaincopy
  1. mvn checkstyle:help           查看checkstyle-plugin的幫助:   
  2. mvn checkstyle:check          檢查工程是否滿足checkstyle的檢查,如果沒有滿足,檢查會失敗,可以通過target/site/checkstyle.html查看。  
  3. mvn checkstyle:checkstyle     檢查工程是否滿足checkstyle的檢查,如果沒有滿足,檢查不會失敗,可以通過target/site/checkstyle.html查看。  
  4. mvn checkstyle:checkstyle-aggregate     檢查工程是否滿足checkstyle的檢查,如果沒有滿足,檢查不會失敗,可以通過target/site/checkstyle.html查看。  
5. 在運行完“mvn checkstyle:checkstyle”命令後,可以運行"mvn jxr:jxr"來使checkstyle的結果可以直接跳轉到代碼行位置
發佈了26 篇原創文章 · 獲贊 6 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章