ps:
SonarQube是管理代碼質量一個開放平臺,可以快速的定位代碼中潛在的或者明顯的錯誤.
1、準備環境
Centos7.5, jdk8,mysql 5.6-5.7, sonarqube-7.8
請先確保linux已經安裝了jdk8,並且已經配置好了環境變量;
以下是我的虛擬機環境:(ps:jdk、mysql已裝好)
[root@localhost ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using EditLine wrapper
[root@localhost ~]# java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
數據庫安裝完畢後需要建立一個數據庫,sonarQube ,sonarQube 在使用過程中用到這個數據庫:
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> create database sonar charset=utf8;
Query OK, 1 row affected (0.00 sec)
2、安裝sonarQube
2.1解壓
2.2配置:
[root@localhost local]# cd sonarqube-7.8/
[root@localhost sonarqube-7.8]# ll
total 12
drwxr-xr-x. 6 root root 94 Jun 17 2019 bin
drwxr-xr-x. 2 root root 50 Jun 17 2019 conf
-rw-r--r--. 1 root root 7651 Jun 17 2019 COPYING
drwxr-xr-x. 2 root root 24 Jun 17 2019 data
drwxr-xr-x. 7 root root 131 Jun 17 2019 elasticsearch
drwxr-xr-x. 4 root root 40 Jun 17 2019 extensions
drwxr-xr-x. 6 root root 123 Jun 17 2019 lib
drwxr-xr-x. 2 root root 24 Jun 17 2019 logs
drwxr-xr-x. 2 root root 24 Jun 17 2019 temp
drwxr-xr-x. 7 root root 4096 Jun 17 2019 web
[root@localhost sonarqube-7.8]# cd conf/
[root@localhost conf]# ll
total 24
-rw-r--r--. 1 root root 20422 Jun 17 2019 sonar.properties
-rw-r--r--. 1 root root 3217 Jun 17 2019 wrapper.conf
[root@localhost conf]# vim sonar.properties
[root@localhost conf]# cd ..
配置 sonar.properties:
# Mysql數據用戶名
sonar.jdbc.username=root
sonar.jdbc.password=123456
# 配置mysql數據庫
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?
useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=
maxPerformance&useSSL=false
# 設置sonar服務
sonar.web.host=0.0.0.0
sonar.web.context=/sonarqube
sonar.web.port=9000
注意:不能用root用戶啓動,原因是ElasticSearch的安全問題,所以需要新建一個用戶,爲該用戶賦予sonarQube的文件訪問權限,再用該用戶登錄.
[root@localhost sonarqube-7.8]# su esuser
[esuser@localhost sonarqube-7.8]$ su root
Password:
[root@localhost sonarqube-7.8]# chown -R esuser.esuser /usr/local/sonarqube-7.8/
[root@localhost sonarqube-7.8]# ll
total 12
drwxr-xr-x. 6 esuser esuser 94 Jun 17 2019 bin
drwxr-xr-x. 2 esuser esuser 50 Mar 11 22:48 conf
-rw-r--r--. 1 esuser esuser 7651 Jun 17 2019 COPYING
drwxr-xr-x. 2 esuser esuser 24 Jun 17 2019 data
drwxr-xr-x. 7 esuser esuser 131 Jun 17 2019 elasticsearch
drwxr-xr-x. 4 esuser esuser 40 Jun 17 2019 extensions
drwxr-xr-x. 6 esuser esuser 123 Jun 17 2019 lib
drwxr-xr-x. 2 esuser esuser 24 Jun 17 2019 logs
drwxr-xr-x. 2 esuser esuser 24 Jun 17 2019 temp
drwxr-xr-x. 7 esuser esuser 4096 Jun 17 2019 web
[root@localhost sonarqube-7.8]# chmod 777 -R /usr/local/sonarqube-7.8/
[root@localhost sonarqube-7.8]# vim elasticsearch/config/elasticsearch.yml
[root@localhost sonarqube-7.8]# su esuser
[esuser@localhost sonarqube-7.8]$ cd bin
[esuser@localhost bin]$ ls
jsw-license linux-x86-64 macosx-universal-64 windows-x86-64
[esuser@localhost bin]$ cd linux-x86-64/
[esuser@localhost linux-x86-64]$ ls
lib sonar.sh wrapper
[esuser@localhost linux-x86-64]$ ./sonar.sh start
Starting SonarQube...
Started SonarQube.
mysql數據庫sonar這個庫,會出現大量的表。
訪問瀏覽器http://192.168.160.130:9000/sonarqube,如圖:
ps:
1.sonar.properties配置文件中sonar.web.context=/sonarqube配置的就是http://192.168.160.130:9000/sonarqube後的
sonarqube。
2.使用admin/admin登錄,頁面會多出這個Administration菜單。使用無賬號無密碼登錄,則沒有。
附:sonar-l10n-zh
因此,下載sonar-l10n-zh-plugin-1.28。
[esuser@localhost plugins]$ pwd
/usr/local/sonarqube-7.8/extensions/plugins
[esuser@localhost plugins]$ wget https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.28/sonar-l10n-zh-plugin-1.28.jar
下載好,然後重啓sonar。
[esuser@localhost linux-x86-64]$ ./sonar.sh restart
Gracefully stopping SonarQube...
Stopped SonarQube.
Starting SonarQube...
Started SonarQube.
重新訪問:http://192.168.160.130:9000/sonarqube,頁面已經是中文版本了。
3.使用SonarQube Maven插件分析代碼
在maven的安裝目錄:如:D:\Program Files (x86)\apache-maven-3.3.9\conf中的settings.xml,加入如下內容:
ps:添加到 <profiles></profiles>標籤中。
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://192.168.160.130:9000/sonarqube</sonar.host.url>
</properties>
</profile>
在項目路徑下,使用cmd,執行mvn sonar:sonar
等待執行完畢。。。。
重新打開瀏覽器訪問:http://192.168.160.130:9000/sonarqube/projects: