原文:http://blog.csdn.net/abcdocker/article/details/53840582
Sonar介紹
Sonar 是一個用于代碼質量管理的開放平臺。通過插件機制,Sonar 可以集成不同的測試工具,代碼分析工具,以及持續集成工具。與持續集成工具(例如?Hudson/Jenkins 等)不同,Sonar 并不是簡單地把不同的代碼檢查工具結果(例如?FindBugs,PMD?等)直接顯示在 Web 頁面上,而是通過不同的插件對這些結果進行再加工處理,通過量化的方式度量代碼質量的變化,從而可以方便地對不同規模和種類的工程進行代碼質量管理。?
在對其他工具的支持方面,Sonar 不僅提供了對?IDE?的支持,可以在?Eclipse和?IntelliJ IDEA?這些工具里聯機查看結果;同時 Sonar 還對大量的持續集成工具提供了接口支持,可以很方便地在持續集成中使用 Sonar。?
此外,Sonar 的插件還可以對?Java?以外的其他編程語言提供支持,對國際化以及報告文檔化也有良好的支持。
Sonar的相關下載和文檔可以在下面的鏈接中找到:http://www.sonarqube.org/downloads/。需要注意最新版的Sonar需要至少JDK 1.8及以上版本。
上篇文章我們已經可以成功的使用git進行拉去,Sonar的功能就是來檢查代碼是否有BUG。除了檢查代碼是否有bug還有其他的功能,比如說:你的代碼注釋率是多少,代碼有一些建議,編寫語法的建議。所以我們叫質量管理
Sonar還可以給代碼打分,并且引用了技術宅的功能(告訴你有很多地方沒改)
Sonar部署
[root@linux-node1 ~]# yum install -y java-1.8.0
[root@linux-node1 ~]# cd /usr/local/src軟件包我們通過wget或者下載,rz上傳到服務器#軟件包下載:https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.6.zip
[root@linux-node1 src]# unzip sonarqube-5.6.zip
[root@linux-node1 src]# mv sonarqube-5.6 /usr/local/
[root@linux-node1 src]# ln -s /usr/local/sonarqube-5.6/ /usr/local/sonarqube
onar數據庫?
如果沒有數據庫請執行yum install -y mariadb mariadb-server
[root@linux-node1 ~]# systemctl start mariadb
[root@linux-node1 ~]# systemctl enable mariadbCreatedsymlinkfrom /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@linux-node1 ~]# mysql_secure_installation[root@linux-node1 ~]# mysql -uroot -p123456
特別提示:?
sonar好像不支持mysql 5.5,所以如果看日志出現以上error 請安裝mysql5.6?或者更高版本?
http://blog.csdn.net/onothing12345/article/details/49910087
執行sql語句
mysql>CREATE DATABASE sonar CHARACTERSETutf8 COLLATE utf8_general_ci;
mysql>GRANTALLONsonar.*TO'sonar'@'localhost'IDENTIFIEDBY'sonar@pw';
mysql>GRANTALLONsonar.*TO'sonar'@'%'IDENTIFIEDBY'sonar@pw';mysql>FLUSH PRIVILEGES;
配置Sonar
[root@linux-node1 ~]#? cd /usr/local/sonarqube/conf/
[root@linux-node1 conf]# lssonar.properties? wrapper.conf
編寫配置文件,修改數據庫配置
[root@linux-node1 conf]# vim sonar.properties#我們只需要去配置文件里面修改數據庫的認證即可14sonar.jdbc.username=sonar#數據庫用戶15sonar.jdbc.password=sonar@pw#數據庫密碼23sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&character? ? Encoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
配置Java訪問數據庫驅動(可選)?
默認情況Sonar有自帶的嵌入的數據庫,那么你如果使用類是Oracle數據庫,必須手動復制驅動類到${SONAR_HOME}/extensions/jdbc-driver/oracle/目錄下,其它支持的數據庫默認提供了驅動。其它數據庫的配置可以參考官方文檔:?
http://docs.sonarqube.org/display/HOME/SonarQube+Platform
啟動Sonar?
你可以在Sonar的配置文件來配置Sonar Web監聽的IP地址和端口,默認是9000端口。
[root@linux-node1 conf]# vim sonar.properties99#sonar.web.host=0.0.0.0106#sonar.web.port=9000
啟動命令如下:
[root@linux-node1 ~]# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh startStartingSonarQube...StartedSonarQube.
如果有什么問題可以看一下日志[/usr/local/sonarqube/logs/sonar.log]
檢查是否有相應的端口
[root@linux-node1 ~]# netstat -lntupActiveInternetconnections (only servers)ProtoRecv-QSend-QLocalAddressForeignAddressStatePID/Programnametcp00127.0.0.1:80800.0.0.0:*LISTEN2239/unicorn mastertcp000.0.0.0:800.0.0.0:*LISTEN505/nginx:master ptcp000.0.0.0:220.0.0.0:*LISTEN569/sshdtcp00127.0.0.1:250.0.0.0:*LISTEN971/mastertcp00127.0.0.1:431630.0.0.0:*LISTEN5205/javatcp000.0.0.0:80600.0.0.0:*LISTEN505/nginx:master ptcp00127.0.0.1:320000.0.0.0:*LISTEN4925/javatcp000.0.0.0:430440.0.0.0:*LISTEN4952/javatcp000.0.0.0:333500.0.0.0:*LISTEN5205/javatcp000.0.0.0:90000.0.0.0:*LISTEN5011/javatcp000.0.0.0:333850.0.0.0:*LISTEN5011/javatcp00127.0.0.1:90010.0.0.0:*LISTEN4952/javatcp600:::3306:::*LISTEN4658/mysqldtcp600:::34993:::*LISTEN2348/javatcp600:::8081:::*LISTEN2348/javatcp600:::22:::*LISTEN569/sshdtcp600::1:25:::*LISTEN971/masterudp600:::33848:::*2348/javaudp600:::5353:::*2348/java
#端口是9000哦!
Web登陸:IP:9000?
提示:?
sonar跟jenkins類似,也是以插件為主?
sonar安裝插件有2種方式:第一種將插件下載完存放在sonar的插件目錄,第二種使用web界面來使用安裝?
存放插件路徑[/usr/local/sonarqube/extensions/plugins/]
安裝中文插件?
登陸:用戶名:admin?密碼:admin?
需要重啟才會生效
生效后如下圖:?
我們在安裝一個php語言?
溫馨提示:如果下載不下來我們直接去github進行下載,因為我們這個插件都是使用wget進行下載的?
我們現在只能使用java的jar包和php,因為我們只安裝了java和php的語言插件。如果想使用Python的程序,就需要安裝Python的語言插件
Sonar 插件--->語言插件 (分析什么語言,你就需要安裝什么語言的插件)
Sonar通過SonarQube Scanner(掃描器)來對代碼進行分析?
官方文檔:http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
下載掃描器插件
[root@linux-node1 ~]# wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zip
[root@linux-node1 ~]# unzip sonar-scanner-2.8.zip
[root@linux-node1 ~]# mv sonar-scanner-2.8 /usr/local/
[root@linux-node1 ~]# ln -s /usr/local/sonar-scanner-2.8/ /usr/local/sonar-scanner
我們要將掃描器和sonar關聯起來
[root@linux-node1 ~]# cd /usr/local/sonar-scanner
[root@linux-node1 sonar-scanner]# lsbin? conf? lib
[root@linux-node1 sonar-scanner]# cd conf/
[root@linux-node1 conf]# lssonar-scanner.properties
[root@linux-node1 conf]# vim sonar-scanner.propertiessonar.host.url=http://localhost:9000#sonar地址
sonar.sourceEncoding=UTF-8#字符集
sonar.jdbc.username=sonar#數據庫賬號
sonar.jdbc.password=sonar@pw#數據庫密碼
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8? ? #數據庫連接地址#打開注釋即可
我們現在需要找一個代碼進行分析。
sonar插件提供了一個代碼的庫?
github:https://github.com/SonarSource/sonar-examples?
我們下載軟件包:https://github.com/SonarSource/sonar-examples/archive/master.zip
解壓
[root@linux-node1 src]# unzip sonar-examples-master.zip
[root@linux-node1 php]# cd sonar-examples-master/projects/languages/php
[root@linux-node1 php]# cd php-sonar-runner-unit-tests/
[root@linux-node1 php-sonar-runner-unit-tests]# lltotal8-rw-r--r--1root root647Dec1409:57README.mddrwxr-xr-x2root root51Dec1409:57reports-rw-r--r--1root root346Dec1409:57sonar-project.propertiesdrwxr-xr-x3root root31Dec1409:57srcdrwxr-xr-x2root root25Dec1409:57tests#這里就是PHP的目錄
配置文件解釋:?
如果你想讓我掃描,就需要在代碼路徑下放一個配置文件
[root@linux-node1 php-sonar-runner-unit-tests]# cat sonar-project.propertiessonar.projectKey=org.sonarqube:php-ut-sq-scanner#Keysonar.projectName=PHP :: PHPUnit :: SonarQube Scanner#這里的名稱會顯示在一會的web界面上sonar.projectVersion=1.0#版本,這里的版本一會也會顯示在web界面上sonar.sources=src#軟件包存放路徑sonar.tests=testssonar.language=php#語言sonar.sourceEncoding=UTF-8#字體# Reusing PHPUnit reportssonar.php.coverage.reportPath=reports/phpunit.coverage.xmlsonar.php.tests.reportPath=reports/phpunit.xml
#也就是說在項目里面必須有這個配置文件才可以進行掃描
掃描?
#提示:需要在項目文件里面進行執行
[root@linux-node1 php-sonar-runner-unit-tests]# /usr/local/sonar-scanner/bin/sonar-scannerINFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.propertiesINFO: Project root configuration file: /usr/local/src/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests/sonar-project.propertiesINFO: SonarQube Scanner 2.8INFO: Java 1.8.0_111 Oracle Corporation (64-bit)INFO: Linux 3.10.0-514.2.2.el7.x86_64 amd64INFO: User cache: /root/.sonar/cacheINFO:LoadglobalrepositoriesINFO:Loadglobalrepositories (done) |time=211msWARN: Property'sonar.jdbc.url'isnotsupportedanymore. It will be ignored. ThereisnolongeranyDBconnectiontothe SQdatabase.WARN: Property'sonar.jdbc.username'isnotsupportedanymore. It will be ignored. ThereisnolongeranyDBconnectiontothe SQdatabase.WARN: Property'sonar.jdbc.password'isnotsupportedanymore. It will be ignored. ThereisnolongeranyDBconnectiontothe SQdatabase.INFO:Usercache: /root/.sonar/cacheINFO:Loadplugins indexINFO:Loadplugins index (done) |time=3msINFO: Download sonar-csharp-plugin-5.0.jarINFO: Download sonar-java-plugin-3.13.1.jarINFO: Download sonar-l10n-zh-plugin-1.11.jarINFO: Plugin [l10nzh] defines'l10nen'asbase plugin. This metadata can be removedfrommanifestofl10n plugins since version5.2.INFO: Download sonar-scm-git-plugin-1.2.jarINFO: Download sonar-php-plugin-2.9.1.1705.jarINFO: Download sonar-scm-svn-plugin-1.3.jarINFO: Download sonar-javascript-plugin-2.11.jarINFO: SonarQube server5.6INFO:Defaultlocale:"en_US", source code encoding:"UTF-8"INFO: Process project propertiesINFO:Loadproject repositories..................................................................................................
提示:我們什么都不指定就會在當面目錄下掃描sonar-project.properties文件,根據配置文件進行掃描工作。掃描之后我們在web界面上就可以看到代碼的掃描結果
這里的名字,版本 都是在sonar-project.properties文件中定義的?
質量閾幫我們設定好一個閾值,超過相應的閾值就算有bug?
為了讓jenkins可以在構建項目的時候執行sonar,所以我們需要在jenkins上安裝插件?
現在就可以進行配置,讓jenkins和sonar結合在一起。這樣我們構建項目的時候就會進行代碼檢測
點擊保存
配置?
編輯我們的項目,選擇最下放。找到構建?
對PHP文件進行復制
[root@linux-node1 php-sonar-runner-unit-tests]# cat /usr/local/src/sonar-examples-master/projects/languages/php/php-sonar-runner-unit-tests/sonar-project.propertiessonar.projectKey=org.sonarqube:php-ut-sq-scannersonar.projectName=PHP :: PHPUnit :: SonarQube Scannersonar.projectVersion=1.0sonar.sources=srcsonar.tests=testssonar.language=phpsonar.sourceEncoding=UTF-8# Reusing PHPUnit reportssonar.php.coverage.reportPath=reports/phpunit.coverage.xmlsonar.php.tests.reportPath=reports/phpunit.xml
Analysis properties 分析的參數
填寫完畢后,我們點擊保存?
我們選擇立即構建?
提示:此時的SonarQube是無法點擊的
點擊Console Output可以查看構建輸出的內容?
#提示:只要沒有error就可以
構建完成后,我們發現這里的SonarQube可以點擊,我們點擊SonarQube就會鏈接到192.168.56.11:9000 就是代碼查看器的地址?
現在我們已經做到了可以在git上進行拉取代碼。并進行檢測
我們還可以配置一個構建失敗發送郵箱:?
在我們項目里面設置構建后操作,選擇E-mail Notification?
溫馨提示:使用163郵箱發送的通知被163服務器退回了,因此我將設置在jenkins的郵箱改成了QQ郵箱
QQ:郵箱需要設置如下:?
**1、需要開啟POPE3/SMTP服務?
2、在jenkins上配置的密碼我們需要點擊生成授權碼進行使用**
QQ郵件默認會收到如下提示:?
當再次構件成功時,郵件內容如下:?