1、下载SonarQube Scanner安装包
在下载地址中挑选合适的安装包:https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/,本文中使用的是sonar-scanner-2.8.zip
2、、安装及配置SonarQube Scanner
1)将下载的安装包解压到指定目录/export/data/tomcatRoot/scanner.jd.com($SCANNER_HOME)
2)修改$SCANNER_HOME\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://localhost:9000
注:如需修改JDK配置,修改$SCANNER_HOME\bin\sonar-runner文件,如下添加红字部分并将路径替换成实际路径
#!/bin/sh
#
# SonarQube Runner Startup Script for Unix
#
# Optional ENV vars:
# SONAR_RUNNER_HOME - location of runner's installed home dir
# SONAR_RUNNER_OPTS - parameters passed to the Java VM when running Sonar
……
#
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of Jon Disnard.
JAVA_HOME=/export/servers/jdk1.7.0_79
3)设置环境变量,将bin文件加入到PATH中,如下红色字
cd
vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
SCANNER_HOME=/export/data/tomcatRoot/scanner.jd.com
PATH=$PATH:$HOME/bin:$SCANNER_HOME/bin:.
export PATH
4)校验配置,执行 sonar-scanner -h 命令,配置成功则展示如下:
[root@localhost ~]# sonar-scanner -h
INFO:
INFO: usage: sonar-runner [options]
INFO:
INFO: Options:
INFO: -D,--define <arg> Define property
INFO: -e,--errors Produce execution error messages
INFO: -h,--help Display help information
INFO: -v,--version Display version information
INFO: -X,--debug Produce execution debug output
INFO: -i,--interactive Run interactively
3、配置被测项目,编辑sonar-project.properties,简单示例
sonar.projectKey=org.sonarqube:example-it-jacoco-sonar-scanner
#在SonarQube中展示的项目名称
sonar.projectName=Java :: IT Coverage with JaCoCo :: SonarQube Scanner
#在SonarQube中展示的版本号
sonar.projectVersion=1.0
#设置源代码的路径
sonar.sources=/export/data/jenkins_home/workspace/sonarTest/it-jacoco-sonar-runner/src
#设置字节码文件的路径
sonar.binaries=/export/data/jenkins_home/workspace/sonarTest/it-jacoco-sonar-runner/classes
sonar.language=java
sonar.sourceEncoding=UTF-8
#设置字报告存放的路径
sonar.jacoco.itReportPath=/export/data/jenkins_home/workspace/sonarTest/it-jacoco-sonar-runner/reports/jacoco.exec