使用SonarQube掃描儀分析Maven
安裝jenkins
安裝Maven
安裝SonarQube
配置Jenkins + Maven + SonarQube
- SonarQube 登錄
access-sonar
用戶密碼為安裝sonar設(shè)置的用戶名和密碼
- 登錄到sonar平臺,設(shè)置 administration -security -user -administrator (右鍵袄秩,重新獲取一個(gè)tokens塑荒,名字自定義)
get-tokens
右鍵復(fù)制 獲取的tokens赖瞒,然后去jenkins里面配置 sonar
- jenkins登錄 -> configure system -> SonarQube servers
config-sonar
注:筆者安裝的富足統(tǒng)一使用域名的方式訪問愕贡。如果有需要穆律,記得本地設(shè)置hosts
- 配置maven
編輯位于$ MAVEN_HOME / conf或?/ .m2中的settings.xml文件读慎,設(shè)置插件前綴和可選的SonarQube服務(wù)器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服務(wù)器地址
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
- 分析一個(gè)Maven項(xiàng)目
移動(dòng)到一個(gè)maven項(xiàng)目目錄內(nèi)怕品,執(zhí)行下面命令
mvn clean verify sonar:sonar
# 此段命令也可以在jenkins配置野舶,如下:
這里寫圖片描述
mvn 分析完成之后易迹,登錄sonar平臺查看分析結(jié)果
這里寫圖片描述
從圖中可以很明顯的看出此項(xiàng)目存在347個(gè)BUG,然后給開發(fā)創(chuàng)建sonar賬號平道,讓他們幫忙修復(fù)睹欲。。巢掺。
相關(guān)報(bào)錯(cuò)解決
- Sonargraph Integration: Skipping project aniu-api-product [tv.aniu:aniu-api-product], since no Sonargraph rules are activated in current SonarQube quality profile [SonarQube]
此報(bào)錯(cuò)暫時(shí)不影響maven 集成到 sonar上
- 413 Request Entity Too Large
原因是nginx默認(rèn)上傳文件的大小是1M句伶,可nginx的設(shè)置中修改
解決方法如下:
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參數(shù)楚堤,設(shè)置 max_allowed_packet = 64M ,然后重啟mysql
[mysqld]
max_allowed_packet=32M
https://dev.mysql.com/doc/refman/5.7/en/packet-too-large.html
注:這里報(bào)錯(cuò)可以通過查看sonar的web.log得出原因疫蔓。