干貨!Jenkins下配置findbugs、pmd及checkstyle實現代碼自動檢測


配置前提:

對於maven項目來說,需要在pom.xml文件的<build><plugins>添加配置</plugins></build>

網上有些地方說要放在<reporting></reporting>里面,但是經過驗證對於產出pmd.html、checstyle.html和findbugsXml.xml沒有影響。

但是如果需要產出findbugs.html可視化文件的話,需要把findbugs的配置放到<reporting>里面,在輸出findbugsXml.xml后敲擊

mvn compile site

命令生成html文件。

但是經過我本地測驗,這種方式需要消耗大量時間,不建議使用。可以采用下面的方式進行查看。

另外findbugs的報告xml形式不是很人性化,但是如果是在你本地的eclipse下的話可以輸入一下命令查看findbgus的gui界面

//生成findbugsXml.xml文件
clean compile findbugs:findbugs
//然后查看gui
findbugs:gui

pom.xml中對於三種代碼檢查方式的配置如下

<build>
<plugins>        
    <!-- 配置靜態代碼檢查包括checkStyle、findBugs、pmd -->
            <!-- 配置checkStyle代碼規范檢查 -->
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <configLocation>checkstyle.xml</configLocation>
                </configuration>
            </plugin>
            <!-- 配置findbugs對編譯文件中可能出現的問題在運行前進行檢查 -->
             <!-- <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <threshold>High</threshold>
                    <effort>Default</effort>
                    <findbugsXmlOutput>true</findbugsXmlOutput>
                    <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
                    <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
                </configuration>
              </plugin> -->
              <!-- 配置pmd對java源文件進行檢查,檢測出可以優化的代碼 -->
              <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-pmd-plugin</artifactId>
                    <version>3.5</version>
               </plugin>   
        </plugins>
    </build>
    <reporting>
    <plugins>
         <!-- 如果要生成findbugs.html的話需要放到這里面 -->
        <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <threshold>High</threshold>
                    <effort>Default</effort>
                    <findbugsXmlOutput>true</findbugsXmlOutput>
                    <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
                    <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
                </configuration>
              </plugin>
              </plugins>
    </reporting>
View Code  

一、Jenkins上添加新項目的基礎配置

首先Jenkins上要安裝相應的插件FindBugs Plug-in+PMD Plug-in+Checkstyle Plug-in+Static Analysis Collector Plug-in

這里需要特別注意雖然,我們只是對代碼進行檢查,並不進行打包發布操作,但是也是需要配置執行節點Node

二、配置每日定時觸發代碼檢查的日程表(PollScm)

PollScm一共有五個可配置參數 * * * * *

第一個參數代表的是分鍾 minute,取值 0~59;

第二個參數代表的是小時 hour,取值 0~23;

第三個參數代表的是天 day,取值 1~31;

第四個參數代表的是月 month,取值 1~12;

第五個參數代表的是星期 week,取值 0~7,0 和 7 都是表示星期天。

我設置的是每周一到周五凌晨兩點進行代碼檢查

H 2 * * 1-5 

三、maven項目對於findbugs、pmd、及checkstyle的構建配置

 在構建這一步的時候就需要填寫check命令了

findbugs的配置如下:

pmd的配置如下:

checkstyle的配置如下:

以上配置完成后,就會在項目的target/site目錄下產出相應的Html和xml文件

四、構建后以報表的形式顯示三個report

在構建后操作中選擇【Publish *** analysis results】,默認設置就好。

 通過這一步讓代碼檢測報告動可視化的顯示在Jenkins界面上

五、完成代碼檢查后發送項目檢查郵件給項目小組

 我是配置的發送模板,然后按照模板進行發送email

一、配置郵件發送模板

在【系統管理】中選擇Editable Email Notification Templates

---------

 ---------

 

------

 郵件內容模板:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次構建日志</title>
</head>

<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
    offset="0">
    <table width="95%" cellpadding="0" cellspacing="0"
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        <tr>
            <td><h2>
                    <font color="#0000FF">${PROJECT_NAME}構建結果 - ${BUILD_STATUS}</font>
                </h2></td>
        </tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">構建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>項目名稱&nbsp;&nbsp;${PROJECT_NAME}</li>
                    <li>構建編號&nbsp;&nbsp;第${BUILD_NUMBER}次構建</li>
                    <li>構建人&nbsp;&nbsp;${CAUSE}</li>
                    <li>構建節點&nbsp;&nbsp;${NODE_NAME}</li>
                    <li>變更集: ${JELLY_SCRIPT,template="html"}</li>
                    <li>構建日志:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>構建&nbsp;&nbsp;Url&nbsp;&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li>
                    <li>工作目錄&nbsp;&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                    <li>項目&nbsp;&nbsp;Url&nbsp;&nbsp;<a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">Changes Since Last
                        Successful Build:</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>歷史變更記錄 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%m</pre>",pathFormat="&nbsp;&nbsp;&nbsp;&nbsp;%p"}
            </td>
        </tr>
        <tr>
            <td><b>Failed Test Results</b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><pre
                    style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
                <br /></td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">構建日志 (最后 100行):</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <!-- <tr>
            <td>Test Logs (if test has ran): <a
                href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a>
                <br />
            <br />
            </td>
        </tr> -->
        <tr>
            <td><textarea cols="80" rows="30" readonly="readonly"
                    style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">構建結果及報告:</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
    </table>
</body>
</html>
View Code

 項目配置:

 


免責聲明!

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



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