閱讀274 返回首頁    go 技術社區[雲棲]


Jenkins+Maven+SonarQube構建代碼質量檢測平台

使用SonarQube掃描儀分析Maven

安裝jenkins

安裝Maven

安裝SonarQube

配置Jenkins + Maven + SonarQube

  • SonarQube 登錄

access-sonar

用戶密碼為安裝sonar設置的用戶名和密碼

  • 登錄到sonar平台,設置 administration -security -user -administrator (右鍵,重新獲取一個tokens,名字自定義)

get-tokens

右鍵複製 獲取的tokens,然後去jenkins裏麵配置 sonar

  • jenkins登錄 -> configure system -> SonarQube servers

config-sonar

注:筆者安裝的富足統一使用域名的方式訪問。如果有需要,記得本地設置hosts

  • 配置maven

編輯位於$ MAVEN_HOME / conf或〜/ .m2中的settings.xml文件,設置插件前綴和可選的SonarQube服務器URL

<settings>
    <pluginGroups>
        <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
    </pluginGroups>
    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Optional URL to server. Default value is https://localhost:9000 -->
                <sonar.host.url>
                  https://sonar.aniu.so  # 填寫自己的sonar服務器地址
                </sonar.host.url>
            </properties>
        </profile>
     </profiles>
</settings>
  • 分析一個Maven項目

移動到一個maven項目目錄內,執行下麵命令

mvn clean verify sonar:sonar 
# 此段命令也可以在jenkins配置,如下:

這裏寫圖片描述

mvn 分析完成之後,登錄sonar平台查看分析結果

這裏寫圖片描述

從圖中可以很明顯的看出此項目存在347個BUG,然後給開發創建sonar賬號,讓他們幫忙修複。。。

相關報錯解決

  • Sonargraph Integration: Skipping project aniu-api-product [tv.aniu:aniu-api-product], since no Sonargraph rules are activated in current SonarQube quality profile [SonarQube]

此報錯暫時不影響maven 集成到 sonar上

  • 413 Request Entity Too Large

原因是nginx默認上傳文件的大小是1M,可nginx的設置中修改

解決方法如下:
1.打開nginx配置文件 nginx.conf, 路徑一般是:/etc/nginx/nginx.conf。
2.在http{}段中加入 client_max_body_size 20m; 20m為允許最大上傳的大小(大小可自定義)。
3.保存後重啟nginx,問題解決。
  • sonar Failed to upload report - 500: An error has occurred

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (22790518 > 16777216). You can change this value on the server by setting the max_allowed_packet' variable.

show variables like '%max_allowed_packet%';
更改mysql 的max_allowed_packet參數,設置 max_allowed_packet = 64M ,然後重啟mysql
[mysqld]
max_allowed_packet=32M
https://dev.mysql.com/doc/refman/5.7/en/packet-too-large.html

注:這裏報錯可以通過查看sonar的web.log得出原因。

最後更新:2017-08-21 20:32:16

  上一篇:go  IDC雲安全評估: 阿裏雲為最重視安全建設的雲服務提供商
  下一篇:go  記一次阿裏雲ECS實例預約遷移