Jenkins集成源碼靜態分析工具


1、static code analysis插件說明

Jenkins提供了插件”static code analysis“,該插件搜集不同的分析結果,並集合顯示出來。
實際上,我們可以認為static code analysi和FindBugs等插件組成了一個靜態分析代碼的套件。僅僅安裝static code analysis是不夠的,還需要安裝其他插件(如FindBugs),才能正確工作。

2、static code analysis支持哪些插件?

官方文檔:https://wiki.jenkins-ci.org/display/JENKINS/Static+Code+Analysis+Plug-ins#StaticCodeAnalysisPlug-ins-summary
Jenkins理解好幾種"代碼靜態分析工具"的結果文件(?什么格式的?),並以可視化的形勢顯示出來。
Jenkins支持對下面列出的靜態分析工具插件的”可視化“:
上述插件的共同特點(可能會有個別遺漏)
  • View column that shows the total number of warnings in a job
  • Build summary showing the new and fixed warnings of a build
  • Several trend reports showing the number of warnings per build
  • Several portlets for the Jenkins dashboard view
  • Overview of the found warnings per module, package, category, or type
  • Detail reports of the found warnings optionally filtered by severity (or new and fixed)
  • Colored HTML display of the corresponding source file and warning lines
  • Several failure thresholds to mark a build as unstable or failed
  • Configurable project health support
  • Highscore computation for builds without warnings and successful builds
  • Works with the freestyle and native m2 build option of Jenkins
  • Email Support to show aggregated results of the warnings in a project
  • Remote API to export the build quality and found warnings
  • Several tokens to simplify post processing of the analysis results

3、static code analysis依賴哪些插件?

從官方文檔了解到,static code analysis依賴:
This plugin adds reusable macro expansion capability for other plugins to use
Jenkins plugin for building Maven 2/3 jobs via a special project type.
This plugin contributes a new view implementation that provides a dashboard / portal-like view for your Jenkins instance.
This plugin adds Apache Ant support to Jenkins.

其中,Maven和Ant一般在Jenkins安裝完后,就跟着安裝上的,作用是作為Jenkins的構建工具。
本例先把Dashboard View和Token Macro安裝了,后續再體驗兩者的作用。

4、如何使用static code analysis進行代碼分析

1)安裝static code analysis插件
以管理員登錄到Jenkins。在”系統管理“/”管理插件“,選擇”可安裝插件“,在”過濾“欄輸入”static code analysis“,選擇”Static Code Analysis Plug-ins“插件。勾選直接安裝。

2)安裝其他插件
包括FindBugs、PMD、checkstyle等工具。具體也是通過搜索,在可選里面找到該插件,然后進行安裝。

3)在Jenkins里面配置job使用maven
示例1:使用maven進行構建,maven內部調用ant。
cpds項目已經有了一個ant腳本,能夠實現系統測試。因此使用maven的話,需要支持調用ant。不過也幸好maven支持調用ant。
所以maven構建包括兩部分:一是調用ant,ant完成編譯等工作;二是maven調用FindBugs等插件完成靜態代碼分析工作。

示例2:先執行maven,再執行ant。
類似示例1,不過maven和ant是平等關系,沒有包含調用的情況。

5、示例

5.1for java

5.1.1非maven工程

對於非maven的java工程,例如普通的Eclipse工程,建議使用ant進行編譯,然后使用maven進行代碼分析(?)
當然也可以將普通工程轉化為maven工程,但是可能比較麻煩。
方法:ant+findbugs分析代碼。
代碼清單: 

5.1.2 maven工程

1)jenkins中使用static code analysis + findbugs進行代碼分析

前提:按照前幾章的說明,需要在jenkins安裝static code analysis,findbugs插件。
一、編輯pom
首先,添加工程依賴和構建需要的插件,包括工程本身的依賴和構建需要的工具。
由於准備執行“mvn compile findbugs:findbugs site”命令,所以必須包含resources、compiler等maven插件。而如果需要打包(jar)、單元測試,則需要添加對jar、surefire、assembly等插件的依賴。
代碼清單1:pom.xml的部分內容
   
   
   
           
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-resources-plugin</artifactId>
  6. <version>2.6</version>
  7. </plugin>
  8. <plugin>
  9. <groupId>org.apache.maven.plugins</groupId>
  10. <artifactId>maven-compiler-plugin</artifactId>
  11. <configuration>
  12. <source>1.5</source>
  13. <target>1.5</target>
  14. </configuration>
  15. </plugin>
  16. <plugin>
  17. <groupId>org.apache.maven.plugins</groupId>
  18. <artifactId>maven-jar-plugin</artifactId>
  19. <configuration>
  20. <archive>
  21. <manifest>
  22. <mainClass>org.sonatype.mavenbook.weather.Main</mainClass>
  23. <addClasspath>true</addClasspath>
  24. </manifest>
  25. </archive>
  26. </configuration>
  27. </plugin>
  28. <plugin>
  29. <groupId>org.apache.maven.plugins</groupId>
  30. <artifactId>maven-surefire-plugin</artifactId>
  31. <configuration>
  32. <testFailureIgnore>true</testFailureIgnore>
  33. </configuration>
  34. </plugin>
  35. <plugin>
  36. <artifactId>maven-assembly-plugin</artifactId>
  37. <configuration>
  38. <archive>
  39. <manifest>
  40. <mainClass>org.sonatype.mavenbook.weather.Main</mainClass>
  41. </manifest>
  42. </archive>
  43. <descriptorRefs>
  44. <descriptorRef>jar-with-dependencies</descriptorRef>
  45. </descriptorRefs>
  46. </configuration>
  47. </plugin>
  48. </plugins>
  49. </build>

其次,需要在maven工程的pom.xml進行設置:在 reporting節點中添加findbugs-maven-plugin的說明
代碼清單2:中findbugs-maven-plugin的說明
   
   
   
           
  1. <reporting>
  2. <plugins>
  3. <!--FindBugs插件。maven goal:findbugs:findbugs -->
  4. <plugin>
  5. <groupId>org.codehaus.mojo</groupId>
  6. <artifactId>findbugs-maven-plugin</artifactId>
  7. <version>2.5.1</version>
  8. <configuration>
  9. <threshold>High</threshold>
  10. <effort>Default</effort>
  11. <findbugsXmlOutput>true</findbugsXmlOutput>
  12. <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
  13. <xmlOutput>true</xmlOutput>
  14. <formats><format>html</format></formats>
  15. </configuration>
  16. </plugin>
  17. </plugins>
  18. </reporting>

二、配置Jenkins job
根據經驗,在jenkins中使用maven風格的job構建maven工程時,可能會出現問題。所以,建議創建freestyle風格的工程。
Maven的目標(goals)設置為:compile findbugs:findbugs site。
由於findbugs基於.class進行分析,所以先需要對代碼進行編譯,即執行compile。執行 findbugs:findbugs表示使用findbugs對字節碼文件進行代碼分析。site則是生成相應的網頁內容。

三、執行構建
構建完成后,jenkins顯示結果如下:


2)jenkins中使用static code analysis + PMD進行代碼分析

與配置FindBugs插件類似,仍舊是在pom中添加PMD的maven插件( maven-pmd-plugin )。
代碼清單3:pom中配置PMD
   
   
   
           
  1. <!--PMD報告設置 -->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-pmd-plugin</artifactId>
  5. <version>3.2</version>
  6. <configuration>
  7. <rulesets>
  8. <!-- Two rule sets that come bundled with PMD -->
  9. <ruleset>/rulesets/java/braces.xml</ruleset>
  10. <ruleset>/rulesets/java/naming.xml</ruleset>
  11. <!-- Custom local file system rule set -->
  12. <!-- ruleset>d:\rulesets\strings.xml</ruleset-->
  13. <!-- Custom remote rule set accessed via a URL -->
  14. <!-- ruleset>http://localhost/design.xml</ruleset-->
  15. </rulesets>
  16. </configuration>
  17. </plugin>
完成構建后,pmd for jenkins插件在左側視圖自動添加按鈕,如下:


點擊bug鏈接,如“1 warning”可查看詳情:


相關問題參考:














免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM