1)创建数据库
CREATE DATABASE sonarCHARACTER SET utf8COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIEDBY 'sonar';
GRANT ALL ON sonar.*TO 'sonar'@'%' IDENTIFIEDBY 'sonar';
GRANT ALL ON sonar.*TO 'sonar'@'localhost' IDENTIFIEDBY 'sonar';
FLUSHPRIVILEGES; 2)配置sonarqube
解压后进入conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false 3)配置环境变量
vi /etc/profile
export SONAR_HOME=/data/sonar
export PATH=${SONAR_HOME}/bin:${PATH} 4)启动
cd bin/linux-x86-64
./sonar.sh start
./sonar.sh stop 访问 http://ip:9000 即可
二、安装配置sonar-scanner
1)安装及配置
wget https://repo1.maven.org/maven2/org/sonarsource/scanner/cli/sonar-scanner-cli/3.0.3.778/sonar-scanner-cli-3.0.3.778.zip
解压后进入目录
# more 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://192.168.12.109:9000
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
2)配置环境变量
#vi /etc/profile
export SONAR_RUNNER_HOME=/data/sonar-scanner
export PATH=$SONAR_RUNNER_HOME/bin:$PATH
#source /etc/profile
确诊生效
# sonar-scanner -h
INFO:
INFO: usage: sonar-scanner [options]
INFO:
INFO: Options:
INFO: -D,--define <arg> Define property
INFO: -h,--help Display help information
INFO: -v,--version Display version information
INFO: -X,--debug Produce execution debug output
三、jenkins集成sonar-scanner
1)配置sonar
jenkins 安装 SonarQube Scanner for Jenkins
安装后进入填写sonar信息,新版本是用token进入认证的
用admin进入sonar 用户-->安全, 生成token
2)sonar scaner环境变量配置
配置完以下,我们进入项目中集成
一下步要注意下,我们选择在构建前,还是构建后进行代码扫描,我这里项目是maven编译后再做代码扫描,所以是构建后,这里要注意下 Post Steps