sonarqube搭建超級完整版

本文重點(diǎn)說明sonarqube的使用和搭建,以及集成到Jenkins,從因到果進(jìn)行詳細(xì)的說明。gitLab+Jenkins的安裝不在范圍內(nèi)。

從如下幾個維度給大家介紹sonarqube

0、前言

? ??CodeReview能帶來什么?

? ? CodeReview能解決那些問題?

1、sonarqube是什么?

2、sonarqube 的架構(gòu)

3、sonarqube的集成

4、windows如何安裝

5、linux如何安裝?

6、sonarqube集成到Jenkins中

7、IDEA中sonarLint如何使用?

前言

今天你 code review了嗎?

[白眼黑臉]表情回答:工作那么多,今天做代碼審查,是不可能做代碼審查的......

為什么不做代碼審查呢?

[白眼黑臉憤怒]表情回答:有多少代碼要寫,心里沒點(diǎn)B數(shù)嗎?

哈哈哈。。。估計說出了我廣大中國IT人民的內(nèi)心話,接下來就讓我們解放生產(chǎn)力,輕松擼代碼,輕松回個家。

CodeReview能帶來什么?

1、讓個人變的優(yōu)秀 :程序猿成長的階梯,促進(jìn)工程師日常代碼交流和快速穩(wěn)步成長。

2、讓團(tuán)隊變的強(qiáng)大:讓新人密集型團(tuán)隊、創(chuàng)新型團(tuán)隊凝聚一股敦實(shí)的技術(shù)力量,有益于團(tuán)隊知識分享與合作,增加團(tuán)隊?wèi)?zhàn)斗力。

3、讓公司IT技術(shù)穩(wěn)?。?/b>為未來公司長遠(yuǎn)戰(zhàn)略目標(biāo)打下夯實(shí)的技術(shù)基石。

CodeReview能解決那些問題?

1、生產(chǎn)服務(wù)不穩(wěn)定,火災(zāi)頻繁。

2、代碼債務(wù)重,后續(xù)入職人員將錯就錯,項目不受控。

3、bug依賴測試團(tuán)隊發(fā)現(xiàn),質(zhì)量和效率低下。

4、團(tuán)隊成員年輕,水平參差不齊。項目急任務(wù)重沒有時間。


1、sonarqube是什么?

SonarQube 官網(wǎng):https://www.sonarqube.org/

Sonar (SonarQube)是一個開源平臺,用于管理源代碼的質(zhì)量。 Sonar 不只是一個質(zhì)量數(shù)據(jù)報告工具,更是代碼質(zhì)量管理平臺。 支持java, JavaScrip, Scala 等等二十幾種編程語言的代碼質(zhì)量管理與檢測。?SonarQube?是一種自動代碼審查工具,用于檢測代碼中的錯誤,漏洞和代碼異味。它可以與您現(xiàn)有的工作流程集成,以便在項目分支和拉取請求之間進(jìn)行連續(xù)的代碼檢查。


2、sonarqube 的架構(gòu)

1、SonarQube Server 包含3個主要服務(wù)進(jìn)程①WebServer②SearchServer使用elastic search 做為搜索的工具ui ③ComputeEngineServer 分析并且保存到sonar數(shù)據(jù)庫中

2、SonarQube Database ① 配置實(shí)例項、插件安裝等 ② 項目質(zhì)量快照視圖

3、SonarQube Plugins 各種插件:如 語言插件、認(rèn)證插件、編程語言檢查插件等

4、SonarQube Scanners 分析項目的工具

3、sonarqube 的集成

1、在IDE中使用sonarlint插件運(yùn)行本地分析

2、開發(fā)者將代碼提交到scm工具中

3、持續(xù)集成工具自動觸發(fā)檢查代碼執(zhí)行SonarQube Scanner 運(yùn)行sonarqube分析

4、分析報告?zhèn)鬟f給sonarqube server進(jìn)行加工處理

5、進(jìn)行處理分析、將數(shù)據(jù)保存到數(shù)據(jù)庫中、將效果通過ui展示出來

6、開發(fā)者通過sonarqube ui查看自己的bug和技術(shù)債等

7、項目經(jīng)理、運(yùn)維經(jīng)理、測試經(jīng)理、開發(fā)人員通過代碼報表進(jìn)行查看管理

4、windows如何安裝

步驟一:準(zhǔn)備工作

1、jdk1.8、maven-3.6.0

2、sonarqube下載地址:

https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.6.zip

3、Sonar Scanner掃描下載

https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492-windows.zip

4、mysql數(shù)據(jù)庫(5.7)

步驟二:sonarqube安裝和配置篇

1、下載好sonarqube后,解壓打開bin目錄,啟動相應(yīng)OS目錄下的StartSonar。如本文演示使用的是win的64位系統(tǒng),則打開E:\sonar\sonarqube-7.6\bin\windows-x86-64\StartSonar.bat

2.啟動瀏覽器,訪問http://localhost:9000,如出現(xiàn)下圖則表示安裝成功。

3、修改sonar.properties文件,添加mysql連接信息配置。E:\sonar\sonarqube-7.6\conf\sonar.properties【url是數(shù)據(jù)庫連接地址,username是數(shù)據(jù)庫用戶名,jdbc.password是數(shù)據(jù)庫密碼,login是sonarqube的登錄名,sonar.password是sonarqube的密碼】

//數(shù)據(jù)庫地址

sonar.jdbc.url=jdbc:mysql://10.10.8.31:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

sonar.jdbc.username=sonar

sonar.jdbc.password=sonar

sonar.sorceEncoding=UTF-8

//sonar登錄用戶

sonar.login=admin

sonar.password=admin

4、重啟sonarqube服務(wù),再次訪問http://localhost:9000,會稍微有點(diǎn)慢,因?yàn)橐跏蓟瘮?shù)據(jù)庫信息。windows重啟方法:在資源管理器中關(guān)閉所有的java.exe進(jìn)程,然后重新運(yùn)行StartSonar.bat文件;訪問地址進(jìn)行登錄,初始的賬號密碼是:admin/admin,數(shù)據(jù)庫有初始化的表信息。

5、插件安裝chinese Pack,安裝中文語言包

6、安裝好插件重啟sonar,登錄后如下


步驟三:sonar-scanner安裝和配置篇

1、解壓sonar-scanner,配置環(huán)境變量

A:新建環(huán)境變量:SONAR_SCANNER_HOME、值:E:\sonar\sonar-scanner-3.3.0.1492

B:打開path,添加%SONAR_SCANNER_HOME%\bin;

C:cmd進(jìn)入,輸入sonar-scanner -v? 出現(xiàn)如下為設(shè)置成功

2、修改settings.xml文件,E:\CFS\outTool\apache-maven-3.6.0\conf\settings.xml

<pluginGroups>

? ? ? ? <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>

? </pluginGroups>

</profiles>

? ? <profile>

? ? ? ? ? ? <id>sonar</id>

? ? ? ? ? ? <activation>

? ? ? ? ? ? ? ? <activeByDefault>true</activeByDefault>

? ? ? ? ? ? </activation>

? ? ? ? ? ? <properties>

? ? ? ? ? ? ? <sonar.jdbc.url>jdbc:mysql://10.10.8.31:3306/sonar</sonar.jdbc.url>

? ? ? ? ? ? ? ? <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>

? ? ? ? ? ? ? ? <sonar.jdbc.username>sonar</sonar.jdbc.username>

? ? ? ? ? ? ? ? <sonar.jdbc.password>sonar</sonar.jdbc.password>

? ? ? ? ? ? ? ? <sonar.host.url>http://10.10.7.67:9000/</sonar.host.url>

? ? ? ? ? ? </properties>

? ? ? ? </profile>

? </profiles>

3、項目maven Reimport后,必須編譯項目后才能分析target目錄下class文件。執(zhí)行clean install sonar:sonar,進(jìn)行代碼掃描分析

A:idea中配置快捷鍵


B:項目控制臺出現(xiàn)如下為分析執(zhí)行完畢


4、刷新 http://localhost:9000? ,點(diǎn)擊項目模塊出現(xiàn)如下分析結(jié)果


前端代碼檢查

項目根目錄新增配置文件:sonar-project.properties

配置詳細(xì):

# must be unique in a given SonarQube instance

sonar.projectKey=Artemis-front

# this is the name displayed in the SonarQube UI

sonar.projectName=Artemis-front

sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.

# Since SonarQube 4.2, this property is optional if sonar.modules is set.

# If not set, SonarQube starts looking for source code from the directory containing

# the sonar-project.properties file.

sonar.sources=.

在根目錄運(yùn)行命令:sonar-scanner

5、linux如何安裝?

準(zhǔn)備工作

1、下載 SonarQube和Sonar scanner

SonarQube:?http://www.sonarqube.org/downloads/

Sonarscanner:?https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner(與jenkins在一臺服務(wù)器)

FTP上傳,然后解壓 unzip? XXX

2、安裝mysql數(shù)據(jù)庫

配置環(huán)境變量

進(jìn)入配置文件:vi /etc/profile
配置文件生效:source /etc/profile

SONAR_HOME=/home/sonar/sonarqube-7.7

export SONAR_HOME

SONAR_RUNNER_HOME=/home/sonar/sonar-scanner-3.3.0.1492-linux

PATH=$SONAR_RUNNER_HOME/bin:$PATH

export SONAR_RUNNER_HOME

export JAVA_HOME=/usr/local/jdk1.8

PATH=$JAVA_HOME/bin:$PATH:$HOME/.local/bin:$HOME/bin

#/etc/security/limits.conf 添加如下配置:

* soft nofile 65536

* hard nofile 131072

* soft memlock unlimited

* hard memlock unlimited

#/etc/sysctl.conf? 添加如下配置:

vm.max_map_count=655360

#配置數(shù)據(jù)庫 vi sonarqube-7.7/conf/sonar.properties

sonar.jdbc.username=sonar

sonar.jdbc.password=sonar

sonar.jdbc.url=jdbc:mysql://10.10.8.11:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

開啟防火墻,開放9000端口


啟動服務(wù)

./sonar.sh start

重啟服務(wù):./sonar.sh restart

停止服務(wù):./sonar.sh stop

訪問http://10.10.8.12:9000/


查看數(shù)據(jù)庫


插件安裝chinese Pack,安裝中文語言包


安裝好插件重啟sonar,登錄后如下


6、jenkins集成sonarQube

1.在jenkins中安裝SonarQube Scanner 插件?

系統(tǒng)管理--》插件管理--》


2.在配置頁對SonarQube servers進(jìn)行配置:?

系統(tǒng)配置–系統(tǒng)設(shè)置–SonarQube servers


訪問sonarqube進(jìn)行token生成

配置--》權(quán)限


賬號--》我的賬號




token一定保存:c5bc5fe7b18600d8a8efb5af5ace7383c7ca00f0,在jenkins中會配置用到

系統(tǒng)管理--》全局工具配置 sonarqube scanner配置


maven項目配置

在maven的/home/tt/apache-maven-3.5.3/conf/setting配置文件添加如下配置

<pluginGroups>

? ? <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>

? </pluginGroups>

</profiles>

? <profile>

? ? ? ? ? ? <id>sonar</id>

? ? ? ? ? ? <activation>

? ? ? ? ? ? ? ? <activeByDefault>true</activeByDefault>

? ? ? ? ? ? </activation>

? ? ? ? ? ? <properties>

? ? ? ? ? ? ? <sonar.jdbc.url>jdbc:mysql://10.10.8.11:3306/sonar</sonar.jdbc.url>

? ? ? ? ? ? ? ? <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>

? ? ? ? ? ? ? ? <sonar.jdbc.username>sonar</sonar.jdbc.username>

? ? ? ? ? ? ? ? <sonar.jdbc.password>sonar</sonar.jdbc.password>

? ? ? ? ? ? ? ? <sonar.host.url>http://10.10.8.12:9000/</sonar.host.url> <!-- sonarqube地址

? ? ? ? ? ? </properties>

? ? ? ? </profile>

? </profiles>

在項目的pom文件中加入sonar-maven-plugin

<dependency>

? ? <groupId>org.sonarsource.scanner.maven</groupId>

? ? <artifactId>sonar-maven-plugin</artifactId>

? ? <version>3.6.0.1398</version>

</dependency>

jenkins項目配置的Build中加入啟動參數(shù)

直接把sonar.java.binaries指向項目自動生成的target/sonar目錄。

mvn sonar:sonar?-Dsonar.java.binaries=target/sonar -Dmaven.test.skip=true


jenkins中立即構(gòu)建項目



啊哈哈哈。。。。終于完事兒了。。??纯闯晒?/p>


jenkins前端項目分析


問題

SonarQube scanner exited with non-zero code: 1


7、IDEA中sonarLint如何使用?


一、IDEA如何安裝SonarLint插件

1.打開 Idea

2.點(diǎn)擊【File】

3.點(diǎn)擊【Settings】

4.點(diǎn)擊【Plugins】

5.在搜索欄中輸入“sonarlint”關(guān)鍵字

6.點(diǎn)擊【Install】進(jìn)行安裝

7.重啟Idea


二、IDEA如何連接Sonar服務(wù)器

1.打開 Idea

2.點(diǎn)擊【File】→【Settings】

3.選擇【SonarLint General Settings】

4.點(diǎn)擊【+】

5.ConfigurationName:請輸入連接名,如SonarQube

6.Choosea Connection Type:sonarqube

7.SonarQubeURL:http://xxxx

8.AuthenticationType:Login/Password

?Login:請輸入sonar系統(tǒng)登錄用戶名

?Password:請輸入sonar系統(tǒng)登錄密碼

9.點(diǎn)擊【Next】,提示連接成功



三、IDEA中SonarLint如何進(jìn)行代碼掃描

u掃描整個工程

1.打開 IDEA

2.右擊項目名稱,如jar-kdc-api

3.點(diǎn)擊【SonarLint】

4.點(diǎn)擊【AnalyzeAll Files with SonarLint】

5.彈出確認(rèn)窗口,點(diǎn)擊【Proceed】繼續(xù)

6.【SonarLint Analysis】窗口會顯示掃描進(jìn)度

u掃描單個文件

打開單個文件,sonarlint會自動進(jìn)行sonar掃描


IDEA中SonarLint如何關(guān)聯(lián)sonar已有項目

1.打開Idea

2.依次點(diǎn)擊【File】→【Settings】→【OtherSettings】→【SonarLint Project Settings】

3.Bindto server:選擇之前的sonar連接名,如SonarQube

4.點(diǎn)擊【Updateproject list】,下載Sonar所有項目列表

5.在【SonarQubeproject】中選擇sonar對應(yīng)的項目名,如jar-kdc-api,點(diǎn)擊【OK】完成關(guān)聯(lián)

點(diǎn)擊SonarLint Project Settings,勾選Enable binding to remote SonarQube server,并選擇Bind to server,選擇你剛添加的sonarqube server。點(diǎn)擊OK,關(guān)閉窗口即可。



彩蛋連接:

sonar表關(guān)系:

http://www.cnblogs.com/zishi/p/7510072.html

sonar rule:

https://www.cnblogs.com/tongxuping/p/7838702.html

https://blog.csdn.net/zhuangchuanao/article/details/82893762

sonar質(zhì)量閥

http://www.itdecent.cn/p/113f6ef9bb0a

郵箱設(shè)置

https://www.cnblogs.com/LearningLog/p/9018371.html

https://blog.csdn.net/liunianxiluo/article/details/88124981

https://my.oschina.net/u/2254503/blog/639034

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容