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支持對下面列出的靜態分析工具插件的”可視化“:
- Checkstyle Plug-in
- DRY Plug-in
- FindBugs Plug-in
- PMD Plug-in
- Compiler Warnings Plug-in
- Task Scanner Plug-in
- CCM Plug-in
- Android Lint Plug-in
- OWASP Dependency-Check Plugin
上述插件的共同特點(可能會有個別遺漏):
- 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.
其中,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的部分內容
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.sonatype.mavenbook.weather.Main</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.sonatype.mavenbook.weather.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
其次,需要在maven工程的pom.xml進行設置:在
reporting節點中添加findbugs-maven-plugin的說明
。
代碼清單2:中findbugs-maven-plugin的說明
<reporting>
<plugins>
<!--FindBugs插件。maven goal:findbugs:findbugs -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<threshold>High</threshold>
<effort>Default</effort>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
<xmlOutput>true</xmlOutput>
<formats><format>html</format></formats>
</configuration>
</plugin>
</plugins>
</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
<!--PMD報告設置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.2</version>
<configuration>
<rulesets>
<!-- Two rule sets that come bundled with PMD -->
<ruleset>/rulesets/java/braces.xml</ruleset>
<ruleset>/rulesets/java/naming.xml</ruleset>
<!-- Custom local file system rule set -->
<!-- ruleset>d:\rulesets\strings.xml</ruleset-->
<!-- Custom remote rule set accessed via a URL -->
<!-- ruleset>http://localhost/design.xml</ruleset-->
</rulesets>
</configuration>
</plugin>
完成構建后,pmd for jenkins插件在左側視圖自動添加按鈕,如下:

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

相關問題參考:
Jenkins-FQA:
http://www.cnblogs.com/topplay/p/3873456.html