本文章也是在参考了网上好多相关文章(主要参考文章见文末)后自学整理的,如有错误之处烦请留言指正。
环境准备
系统默认已安装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博客