下载安装包
https://www.sonarqube.org/downloads/
配置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
sonar.web.javaOpts=-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError
sonar.web.host=0.0.0.0
sonar.web.context=
sonar.web.port=9000
配置wrapper.conf
wrapper.java.command=C:\Program Files\Java\jdk1.8.0_171\bin\java
创建MySQL数据库 sonar
创建用户及授权
CREATE USER 'sonar'@'localhost' IDENTIFIED BY 'sonar';
GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'localhost'
选择bin目录下对应操作系统的StartSonar.bat启动SonarQube服务器
项目代码分析
下载SonarQube扫描仪
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
配置sonar-scanner.properties文件
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://localhost:9000
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
#----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
#----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
#----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
修改mysql中max_allowed_packet参数(最大允许数据包值)
SET GLOBAL max_allowed_packet = 100* 1024 * 1024;
添加SonarQube扫描仪到系统Path变量
D:\sonar-scanner-3.2.0.1227-windows\bin;
在需要扫描分析的项目根目录建sonar-project.properties文件
#must be unique in a given SonarQube instance
sonar.projectKey=my:project 项目名
#this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=项目名
sonar.projectVersion=1.0
#Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
#Since SonarQube 4.2, this property is optional if sonar.modules is set.
#If not set, SonarQube starts looking for source code from the directory containing
#the sonar-project.properties file.
sonar.sources=.
sonar.java.binaries= 项目classes路径
#Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8
到项目路径打开命令行用sonar-scanner命令扫描即可,扫描完成后打开sonar.jdbc.url查看分析报告