sonar mysql 5.5_SonarQube代码质量管理工具安装与使用(sonarqube5.1.2 + sonar-runner-dist-2.4 + MySQL5.x)...

1. SonarQube安装(sonarqube5.1.2 + sonar-runner-dist-2.4)

1.1 前提条件

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

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

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:\sonar\sonarqube-5.1.2;

2) 修改配置文件D:\sonar\sonarqube-5.1.2\conf\sonar.properties,进行配置数据库设置

75a67fc525515db87387faec97ea0e57.png

1.3 启动Sonar

Server服务

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

ddaff4049bc0877d589801f251be3dba.png

1.4 访问Sonar

Server

其中,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+

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

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%\bin”,点确定。

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

52d517b4897669612b5f71fa4e669612.png

2.3 配置Sonar

Runner

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

91eedc68afeed6987d8777db0a3e3428.png

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

2.5 代码扫描

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

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

b2f9d781c8cb433ec591950c2d85cbf0.png

2.6 查看代码扫描结果

2a9d09f394a8d01990313c7a851656fa.png

2.7 补充说明

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

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

0575c8015210b80c9e2d6b1b98c24a9c.png

3. 案例分析

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

MyProject:

sonar-project.properties内容如下:

d3f988b2776be589d8f16031818a6542.png

Main Dashboard: 主要的信息展示

Duplications: 重复率比重

Complexity:复杂度

9143fc03d8b5174dd44c9ee42184aa83.png

Configure widgets:布局控件

SQALE Rating:Software Quality Assessment based on

Lifecycle Expectations Rating 基于生命周期期望的软件质量模型评价

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

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

d0f32781b657cd6ae09eab2c627595ca.png

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

ac7b4c0a80541f2a50293990ef2e2ea4.png

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

0f5ea0061e1e867cc6bddfcf5bfb100c.png

4. 参考文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值