天天看點

Centos 7環境安裝SonarQube和SonarQube Runner

SonarQube簡介

Sonar 是一個用于代碼品質管理的開放平台。通過插件機制,Sonar 可以內建不同的測試工具,代碼分析工具,以及持續內建工具。比如pmd-cpd、checkstyle、findbugs、Jenkins。通過不同的插件對這些結果進行再加工處理,通過量化的方式度量代碼品質的變化,進而可以友善地對不同規模和種類的工程進行代碼品質管理。同時 Sonar 還對大量的持續內建工具提供了接口支援,可以很友善地在持續內建中使用 Sonar。

此外,Sonar 的插件還可以對 Java 以外的其他程式設計語言(支援的語言包括:Java、PHP、C#、C、Cobol、PL/SQL、Flex等)提供支援,對國際化以及報告文檔化也有良好的支援。可以說Sonar是目前最強大的代碼品質管理工具之一。

下面就來揭開Sonar的神秘面紗,看看如何在Centos環境上安裝Sonar。

安裝環境

系統環境:centos7.0 -x86_64(最小化安裝)

前置條件:jdk1.8 , mysql-5.6

軟體下載下傳目錄:/usr/local/

軟體安裝目錄:/usr/local/

軟體版本:sonarqube-6.7,sonar-runner-dist-2.4 sonar-scanner-3.0.3.778

sonarqube下載下傳位址:http://www.sonarqube.org/downloads/

sonar-runner下載下傳位址:https://link.jianshu.com/?t=http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

sonar-scanner下載下傳掃描器位址:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.3.778-linux.zip

預置條件

1, 需要JDK1.6+支援;

Centos 7環境安裝SonarQube和SonarQube Runner

添加環境變量

[root@localhost java]# vim /etc/profile

Centos 7環境安裝SonarQube和SonarQube Runner

JDK

export JAVA_HOME=/usr/java/jdk1.8.0_101

export JRE_HOME=${JAVA_HOME}/jre

export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib

export PATH=${JAVA_HOME}/bin:$PATH

export TIME_STYLE='+%Y/%m/%d %H:%M:%S'

[root@localhost local]# source /etc/profile

2, 需要Mysql資料庫支援;雖然SonarQube自帶了H2資料庫,但是為了友善管理資料推薦使用Mysql資料庫

安裝Mysql資料庫

安裝MySQL的yum源

yum -y install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

安裝MySQL

yum -y install mysql mysql-devel mysql-server mysql-utilities

啟動MySQL

systemctl start mysqld.service

3,資料庫配置

進入資料庫指令

mysql -u root -p

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;

mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';

mysql> GRANT ALL ON sonar. TO 'sonar'@'%' IDENTIFIED BY 'sonar';

mysql> GRANT ALL ON sonar. TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';

mysql> FLUSH PRIVILEGES;

4,安裝

在安裝之前,介紹一下SonarQube和SonarQube Runner之間的關系。

SonarQube是伺服器端,它主要有兩個功能:1.分析源代碼;2.因為它内嵌了Apache子產品,是以提供Web端的界面通路。

SonarQube Runner是一個利用SonarQube服務端分析代碼的指令行工具,可以把它簡單了解為用戶端。

是以,為了安裝和調試友善,建議SonarQube和SonarQube Runner都下載下傳。

5, 安裝SonarQube

第一步:将下載下傳的sonarqube-6.7.zip解壓後移動到/usr/local目錄下。具體步驟如下:

Centos 7環境安裝SonarQube和SonarQube Runner

将檔案解壓到/usr/local/

[root@localhost local]# unzip sonarqube-6.7

第二步:配置環境變量

[root@localhost local]# vim /etc/profile

export SONAR_HOME=/usr/local/sonarqube-6.7

export SONAR_RUNNER_HOME=/usr/local/sonar-runner-2.4

PATH=$PATH:$SONAR_HOME/bin:$SONAR_RUNNER_HOME/bin

Centos 7環境安裝SonarQube和SonarQube Runner

第三步:配置sonar.properties

Centos 7環境安裝SonarQube和SonarQube Runner
Centos 7環境安裝SonarQube和SonarQube Runner
Centos 7環境安裝SonarQube和SonarQube Runner

配置SonarQube Runner

[root@localhost local]# # unzip sonar-runner-dist-2.4.zip

Centos 7環境安裝SonarQube和SonarQube Runner
Centos 7環境安裝SonarQube和SonarQube Runner

[root@localhost sonar-runner-2.4]# vim sonar-project.properties

#Required metadata

sonar.projectKey=my:project

sonar.projectName=yaok

sonar.projectVersion=1.0

#Paths to source directories.

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

#Do not put the "sonar-project.properties" file in the same directory with the source code.

#(i.e. never set the "sonar.sources" property to ".")

sonar.sources=/root/java

#The value of the property must be the key of the language.

sonar.language=java

#Encoding of the source code

sonar.sourceEncoding=UTF-8

#Additional parameters

sonar.my.property=value

Centos 7環境安裝SonarQube和SonarQube Runner

[root@localhost sonar-runner-2.4]# ./bin/sonar-runner -e -X

Centos 7環境安裝SonarQube和SonarQube Runner

如果能夠看到下面的輸出資訊,證明你的SonarQube Runner安裝并配置正确啦。

6,安裝後啟動SonarQube

用root無法啟動lSonarQube,需要另外建立普通使用者來啟動

[root@localhost ]# useradd esadmin

[root@localhost local]# chown esadmin.esadmin sonarqube-6.7

[root@localhost sonarqube-6.7]# vim elasticsearch/config/elasticsearch.yml

開啟以下兩行

Centos 7環境安裝SonarQube和SonarQube Runner

[root@localhost sonarqube-6.7]# su esadmin

[esadmin@localhost sonarqube-6.7]$ ./bin/linux-x86-64/sonar.sh start

啟動的時候會報錯

1,錯誤:max file descriptors [4096] for elasticsearch process is too low, increase to at least

解決方法:

[root@localhost ]#vim /etc/security/limits.conf

esadmin hard nofile 65536

esadmin soft nofile 65536

在檔案最後添加以上兩行

檢視檔案限制指令

[esadmin@localhost sonarqube-6.7]$ ulimit -Hn

Centos 7環境安裝SonarQube和SonarQube Runner

2,錯誤,max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

切換到root使用者修改配置sysctl.conf

[root@localhost]#vim /etc/sysctl.conf

添加下面配置:

vm.max_map_count=655360

并執行指令:

[root@localhost java]# sysctl -p

Centos 7環境安裝SonarQube和SonarQube Runner

解決好以上錯誤後就可以重新開機SonarQube

[root@localhost sonarqube-6.7]# ps -aux |grep sonar

Centos 7環境安裝SonarQube和SonarQube Runner

使用浏覽器就可以直接登陸

http://192.168.0.39:9000/projects

Centos 7環境安裝SonarQube和SonarQube Runner

7,安裝中文語言包

Centos 7環境安裝SonarQube和SonarQube Runner

選擇語言包,點選安裝,安裝好後重新開機,重新進入就是漢化版了

下篇講解如何利用Jenkins和SonarQube內建對代碼進行持續檢測

注:有些文字來自網際網路

繼續閱讀