SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x) 1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4) 2. Sonar Runner 3. 案例分析 4. 参考文档

1.1 前提条件

1) 已安装Java环境(version:1.7+)

2) 已安装MySQL数据库(version:5.x)

  下载SonarQube: http://www.sonarqube.org/downloads/

  下载Sonar-Runner: http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

  中文补丁包下载:https://github.com/SonarCommunity/sonar-l10n-zh

3) MySQL数据库配置

执行数据库脚本,创建数据库及用户:

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; 

CREATE USER 'sonar' IDENTIFIED BY 'sonar';

GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';

GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';

FLUSH PRIVILEGES;

1.2 安装Sonar Server

1) 将下载的sonarqube-5.1.2.zip包解压至D:sonarsonarqube-5.1.2;

2) 修改配置文件D:sonarsonarqube-5.1.2confsonar.properties,进行配置数据库设置

SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档

1.3 启动Sonar Server服务

Sonar目前支持Linux/Macosx/Solaris/Windows等操作系统。以Windows 32位操作系统为例,目录切换至D:sonarsonarqube-5.1.2inwindows-x86-32目录(32对应jdk7_32bit 而非windows_32bit),运行StartSonar.bat文件启动服务。

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档

1.4 访问Sonar Server

访问地址:http://172.20.28.35:9000/ 或者 http://localhost:9000/

其中,172.20.28.35为服务器的IP 

1.5 Installing SonarQube in Eclipse(安装插件[非必须项],可以先忽略1.1-1.4步骤)

Plugin

3.4

3.5

SonarQube

3.6+

4.2+

Eclipse

3.7.x, 3.8.x, 4.2.x, 4.3.x, 4.4.x, 4.5.x
(Indigo, Juno, Kepler, Luna, Mars)

4.2.x, 4.3.x, 4.4.x, 4.5.x
(Juno, Kepler, Luna, Mars)

Supported Plugins / Languages

Java
C/C++ / only C++ is supported
Python 

See Features details

Installation: If a previous version of Eclipse SonarQube is already installed, you can update it. Go to Help > Check for Updates.

To install this plugin in your Eclipse IDE:

Go to Help > Eclipse Marketplace... and search for "SonarQube"
Detail link: http://docs.sonarqube.org/display/SONAR/Installing+SonarQube+in+Eclipse 

2. Sonar Runner

2.1 安装Sonar Runner

解压sonar-runner-dist-2.4.zip到任意目录,为了方便,将安装包解压到D:sonar-runner-2.4下。

2.2 配置环境变量

1) 打开“系统属性”对话框,点击“环境变量”,进入环境变量对话框。

2) 在“系统变量(S)”下点击“新建(W)...”,在编辑系统变量对话框中添加SONAR_RUNNER_HOME变量。

3) 在“系统变量(S)”下找到“Path”,点击“编辑(I)...”,在编辑系统变量对话框中“变量值(V):”输入框内容未尾添加、“;%SONAR_RUNNER_HOME%in”,点确定。

4) 按下图所示命令进行验证环境变量是否配置成功,如果展示结果如下图所示,则表示配置成功。

SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档 

2.3 配置Sonar Runner

编辑D:sonar-runner-2.4confsonar-runner.properties,配置指定的Sonar Server地址、数据库URL、数据库用户名及密码、Sonar Server用户名及密码

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档

2.4 配置sonar-project.properties

在项目源码的根目录下创建sonar-project.properties配置文件

Multi-module Project

There are two ways to define a multi-module structure in SonarQube:

 

Using the given file structure...

... with the given 'properties' files

Way #1

Set all the configuration in the properties file in the root folder

 

"MyProject/sonar-project.properties" file content

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# Root project information

sonar.projectKey=org.mycompany.myproject

sonar.projectName=My Project

sonar.projectVersion=1.0

 

# Some properties that will be inherited by the modules

sonar.sources=src

 

# List of the module identifiers

sonar.modules=module1,module2

 

# Properties can obviously be overriden for

# each module - just prefix them with the module ID

module1.sonar.projectName=Module 1

module2.sonar.projectName=Module 2

Way #2

Set the configuration in multiple properties files

 

"MyProject/sonar-project.properties" file content

1

2

3

4

5

6

7

8

9

10

# Root project information

sonar.projectKey=org.mycompany.myproject

sonar.projectName=My Project

sonar.projectVersion=1.0

 

# Some properties that will be inherited by the modules

sonar.sources=src

 

# List of the module identifiers

sonar.modules=module1,module2

"MyProject/module1/sonar-project.properties" file content

1

2

3

# Redefine properties

# Note that you do not need to prefix the property here

sonar.projectName=Module 1

"MyProject/module2/sonar-project.properties" file content

1

2

3

# Redefine properties

# Note that you do not need to prefix the property here

sonar.projectName=Module 2

Detail link: http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Runner 

2.5 代码扫描

切换到项目源码根目录,执行以下命令:sonar-runner

扫描结果中出现“EXCUTION SUCCESS”字样则表示代码扫描成功。

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档 

2.6 查看代码扫描结果

http://localhost:9000/

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档

2.7 补充说明

在实际应用过程中,可能会遇到报JVM空间不够或内存溢出的情况,为了解决此问题,可以按下文所示方法通过修改批处理脚本D:sonar-runner-2.4insonar-runner.bat文件即可。

根据实际情况并参照下图所示修改sonar-runner.bat文件中第77行。

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档 

3. 案例分析

默认只有java  plugin。添加javascript,jsp(web)检测,需下载相关插件

link:http://docs.sonarqube.org/display/PLUG/Web+Plugin

MyProject:

sonar-project.properties内容如下:

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档

Main Dashboard: 主要的信息展示

Duplications: 重复率比重

Complexity:复杂度

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档

Configure widgets:布局控件

SQALE Rating:Software Quality Assessment based on Lifecycle Expectations Rating 基于生命周期期望的软件质量模型评价

Debt:技术债务(修复所有issues所用的时间代价)

Issues: Blocker/Critical/Major/Minor/Info 问题的级别,依次从高级到低级。

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档  

如下图:点击issues 选择Critical级别,右侧会有一个列表,点击文件进入。

SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档 

在红色箭头处点击展开,会有相关信息提示。

 SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)
1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)
2.
Sonar Runner
3. 案例分析
4. 参考文档  

4. 参考文档

1.如何搭建Sonar代码质量管理平台
2.如何使用Sonar Runner V2.3进行代码扫描

3.SonarQube代码质量管理平台安装与使用

4.http://deejay.iteye.com/blog/1901950