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

使用SonarQube掃描儀分析Maven

安裝jenkins

安裝Maven

安裝SonarQube

配置Jenkins + Maven + SonarQube

  • SonarQube 登陸web

access-sonar

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

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

get-tokens

右鍵複製 獲取的tokens,而後去jenkins裏面配置 sonar服務器

  • jenkins登陸 -> configure system -> SonarQube serversmaven

config-sonar

注:筆者安裝的服務統一使用域名的方式訪問。若是有須要,記得本地設置hoststhis

  • 配置mavenurl

編輯位於$ 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 http://localhost:9000 -->
                <sonar.host.url>
                  http://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得出緣由。

相關文章
相關標籤/搜索