CentOS-6.8系列-sonar安裝指南

本文章也是在參考了網上好多相關文章(主要參考文章見文末)後自學整理的,如有錯誤之處煩請留言指正。

環境準備

系統默認已安裝jdk8與mysql,在centos6.8中安裝jdk與mysql可參考 CentOS-6.8系列-JDK與Mysql安裝指南

系統參數需要滿足下述要求

  • sonarqube一定不能在root帳戶下運行
  • vm.max_map_count 大於或等於 262144
  • fs.file-max 大於或等於 65536
  • 運行SonarQube的用戶至少可以打開 65536個 文件描述符
  • 運行SonarQube的用戶可以打開至少2048個線程
  • seccomp已被編譯 進內核

    根據需要執行下述命令以使內核參數符合sonar安裝需求

#可以使用以下命令查看這些值:
sysctl vm.max_map_count
sysctl fs.file-max
ulimit -n

#可以通過以root身份運行以下命令來爲當前會話動態設置它們:
sysctl -w vm.max_map_count=262144
sysctl -w fs.file-max=65536
ulimit -n 65536

#爲了更永久設置這些值,則必須修改/etc/sysctl.d/99-sonarqube.conf(或/etc/sysctl.conf文件)
#文件末尾添加下述兩行
vi /etc/sysctl.conf
    vm.max_map_count=262144
    fs.file-max=65536
#在/etc/profile文件末尾添加ulimit -n 65536
vi /etc/profile
    ulimit -n 65536
#保存後運行#source /etc/profile 使其生效
source /etc/profile

mysql數據庫創建庫及用戶腳本

一次執行下述命令

mysql -uroot -p123456
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar123';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar123';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar123';
FLUSH PRIVILEGES;
#mysql設置:一旦所有的SonarQube表都使用了InnoDB引擎,首先要做的就是使用innodb_buffer_pool_size參數爲你的MySQL實例分配最大數量的RAM,並給參數至少15Mb query_cache_size
#檢查mysql引擎,確保爲InnoDB
show engines;
#查看緩存開啓狀況
SHOW STATUS LIKE 'qcache%';
show variables like '%query_cache%';
#設置緩存
vi /etc/my.cnf
    [mysqld]
    character_set_server=utf8
    query_cache_type=1
    query_cache_size=32M
#重啓
service mysqld restart

sonar安裝及配置

依次執行下述命令

#創建sonar文件夾
mkdir /usr/sonarqube
#上傳sonarqube-6.7.zip至該文件夾並解壓
unzip sonarqube-6.7.zip
cd sonarqube-6.7.zip
#修改sonar配置文件
vim conf/sonar.properties
    #取消mysql模塊的註釋
    sonar.jdbc.username=sonar
    sonar.jdbc.password=sonar123

    #----- Embedded Database (default)
    # H2 embedded database server listening port, defaults to 9092
    #sonar.embeddedDatabase.port=9092

    #----- MySQL 5.6 or greater
    # Only InnoDB storage engine is supported (not myISAM).
    # Only the bundled driver is supported. It can not be changed.
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
#開啓9000端口
vi /etc/sysconfig/iptables
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 9000 -j ACCEPT
service iptables restart

#創建用戶(elasticsearch不能以root用戶啓動)
useradd sonar
passwd snoar
#賦予權限
chown -R sonar /usr/sonarqube/
#切換至sonar用戶下啓動sonar
su sonar

#啓動 SonarQube
cd /usr/sonarqube/sonarqube-6.7/bin/linux-x86-64/
./sonar.sh start

#訪問http://10.2.111.54:9000       默認用戶名密碼admin/admin
wp: 23ec44af4147ded513789e10cc95e100b9e39c07

#添加漢化插件
#從https://docs.sonarqube.org/display/PLUG/Plugin+Library下載漢化插件移動至指定目錄即可
cp /usr/local/src/sonar-l10n-zh-plugin-1.19.jar /usr/sonarqube/sonarqube-6.7/extensions/plugins
#重啓sonar
cd /usr/sonarqube/sonarqube-6.7/bin/linux-x86-64/
./sonar.sh restart

sonarqube的簡單使用

本部分的代碼走查使用Analyzing with SonarQube Scanner(點開連接查看官方使用文檔)
本人實驗步驟記錄如下:

windows客戶端下
下載sonar-scanner-cli-3.0.3.778-windows.zip並解壓至指定目錄
配置<install_directory>/conf/sonar-scanner.properties:
    #Configure here general information about the environment, such as SonarQube DB details for example
    #No information about specific project should appear here

    #----- Default SonarQube server
    sonar.host.url=http://10.2.111.54:9000

    #----- Default source code encoding
    #sonar.sourceEncoding=UTF-8
項目根路徑下新建sonar-scanner.properties配置文件
    # must be unique in a given SonarQube instance
    sonar.projectKey=EHL:IDMS
    # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
    sonar.projectName=EHL_IDMS
    sonar.projectVersion=1.0
    sonar.language=java
    # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
    # This property is optional if sonar.modules is set. 
    sonar.sources=src/main/java
    sonar.java.binaries=target/classes

    # Encoding of the source code. Default is default system encoding
    sonar.sourceEncoding=UTF-8
將D:\workide\sonarscanner\sonar-scanner-3.0.3.778-windows\bin加入path後直接在項目根目錄下執行sonar-scanner
然後在http://10.2.111.54:9000上查看代碼分析報告

另外可以在ide(eclipse、IDEA)中直接安裝sonarlint,可在寫代碼時實時查看代碼是否符合規則

主要參考文章:
SonarQube文檔
Scanners
在linux上安裝與啓動Elasticsearch
CENTOS安裝ElasticSearch - 紫鷹王
SonarQube 5.6.6安裝和配置(雲環境) - 簡書
初識Sonar - 簡書
Sonar及其eclipse插件的安裝 - ImportNew
SonarQube代碼質量管理平臺安裝與使用 - CSDN博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章