1、安装SonarQube Web Server
1)、下载SonarQube项目(sonarqube-4.3.2.zip)
http://www.sonarqube.org/downloads/
2)、解压sonarqube-4.3.2.zip
3)、创建Mysql数据库,配置数据库访问权限
以create_database.sql为例:
a)、创建数据库sonar,utf-8编码。
b)、创建用户sonar,密码也为sonar
C)、给sonar用户授权
在这儿我们直接执行脚本create_database.sql就行。
4)、编辑<install_directory>/conf/sonar.properties文件,配置数据库设置。
取消sonar.properties模板中我们需要配置项的注释。
以Mysql数据库为例:
a)、在sonar.properties中,取消相关项注释,配置数据库相关配置
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
b、如果我们用的是Oracle数据库,在这儿需要添加Oracle数据库驱动
(因为我们用的Mysql数据库,所以不需要添加驱动)
c、在sonar.properties中配置启动Web Server所需配置的选项
sonar.web.host=127.0.0.1
sonar.web.port=9000
到目前为止,我们已经做了以下事情:
创建了数据库sonar,并将它授权给新建用户sonar
配置sonar.properties文件,进行数据库、web Server相关配置设置
5)、启动<install_directory>/bin/windows-x86-64/ StartSonar.bat
6)、在浏览器中访问http://localhost:9000/
可以看到SonarQube相关网页界面。
2、安装分析器Sonar-runner
分析器有多种,我们在这儿使用Sonar-runner
安装分析器前提条件:SonarQube已经安装成功。
1)、下载sonar-runner
下面以<install_directory>代替sonar-runner-2.4文件夹名称
2)、编辑<install_directory>/conf/sonar-runner.properties, 配置全局设置(数据库连接、server URL)
sonar.host.url=http://localhost:9000
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
3)、将<install_directory>/bin设置到环境变量中
Add the <install_directory>/bin directory to your paths
4)、运行sonar-runner,你能得到以下信息
3、利用Sonar-runner分析项目
在项目的根目录下创建sonar-project.properties文件,添加相关配置选项
# Required metadata
sonar.projectKey=my:project
sonar.projectName=My project
sonar.projectVersion=1.0
# Path to the parent source code directory.
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional. If not set, SonarQube starts looking for source code
# from the directory containing the sonar-project.properties file.
sonar.sources=src
# Encoding of the source code
sonar.sourceEncoding=UTF-8
# Additional parameters
sonar.my.property=value
项目文件分布图如下所示:
4、在dos窗口中,进入项目所在目录,执行sonar-runner命令(也可以执行sonar-runner –e命令,-e选项会详细显示sonar-runner运行时日志信息)
5、sonar-runner执行成功后,打开浏览器访问http://localhost:9000/可看到我们的分析结果如图所示
到此,SonarRunner命令分析JAVA项目成功。