環境
系統:
jdk:
mysql:
下載安裝sonar
因爲系統安裝的jdk是1.8,所以選擇一個支持1.8的sonar版本。
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.2.zip
下載後解壓到指定文件夾。
unzip sonarqube-7.2.zip -d sonar
準備啓動sonar
查看系統是32位還是64位:
root@node1:/home/shine/sonarqube/sonarqube-7.2# uname -a
Linux node1 4.15.0-99-generic #100~16.04.1-Ubuntu SMP Wed Apr 22 23:56:30 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
切換到指定版本目錄控制檯啓動sonar:
root@node1:/home/shine/sonarqube/sonarqube-7.2/bin# cd linux-x86-64/
root@node1:/home/shine/sonarqube/sonarqube-7.2/bin/linux-x86-64# ls
lib SonarQube.pid sonar.sh wrapper
root@node1:/home/shine/sonarqube/sonarqube-7.2/bin/linux-x86-64# ./sonar.sh console
使用./sonar.sh console在控制檯可以看到啓動報錯,不然就得到/logs目錄查看日誌。
使用root用戶啓動會報錯,所以需要給sonar創建一個專門的用戶:
sudo useradd sonar
sudo passwd sonar
給sonar目錄添加權限:
root@node1:/home/shine/sonarqube# chown -R sonar:sonar sonarqube-7.2/
root@node1:/home/shine/sonarqube# ll
total 12
drwxr-xr-x 3 root root 4096 5月 9 14:46 ./
drwxr-xr-x 8 root root 4096 5月 9 14:28 ../
drwxr-xr-x 11 sonar sonar 4096 6月 18 2018 sonarqube-7.2/
接下來切換到sonar用戶啓動還是會報錯:
Process exited with exit value [es]: 143
如果通過./sonar console啓動,那麼在控制檯就可以看到,如果通過./sonar start啓動,可以在logs/es.log裏看到,並且會有詳細的提示。
參照提示修改。切換到root用戶,在/etc/sysctl.conf文件中加入以下內容:
vm.max_map_count=262144
fs.file-max=65536
然後使用root用戶執行sysctl -p使配置生效。
在 /etc/security/limits.conf後加入以下內容:
ulimit -n 65536
ulimit -u 2048
然後切換sonar用戶下執行ulimit -Hn
現在可以用sonar用戶啓動sonar了。
訪問http://localhost:9000可以看到about頁面。
如果localhost:9000不可訪問,且啓動服務的時候es.log和sonar.log都沒有看到報錯,可以看看web.log有沒有報錯。
配置數據庫
首先登陸mysql,創建數據庫sonar。
然後打開安裝目錄下sonarqube-7.2/conf/sonar.properties文件,添加數據庫配置參數。
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=root
sonar.jdbc.password=cloud1688
sonar.login=admin
sonar.password=admin
重啓sonar服務,tail logs/web.log可以看到數據庫表創建過程日誌,耐心等待數據庫初始化完成。
數據庫初始化成功後,再次訪問localhost:9000,以admin/admin賬戶登陸,
按照提示創建token:
根據提示選擇常用語言和構建礦建
中文插件
我沒有安裝中文插件,如果需要安裝參照圖示步驟。
配置本機maven
在IDEA裏查看自己maven的配置文件路徑:
打開文件添加以下配置:
<profiles>
<profile>
<id>sonar</id>
<properties>
<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>root</sonar.jdbc.password>
<sonar.host.url>http://localhost:9000</sonar.host.url> <!-- Sonar服務器訪問地址 -->
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sonar</activeProfile>
</activeProfiles>
到自己項目下執行mvn sonar:sonar,對代碼進行分析。
構建完成後,可以在localhost:9000看到相關分析結果: