文章目录
  1. 1. sonar环境搭建简明手册
    1. 1.1. 1.sonar简介
    2. 1.2. 2.安装包
    3. 1.3. 3.环境要求
    4. 1.4. 4.安装步骤
    5. 1.5. 5.修改配置
    6. 1.6. 6.汉化
    7. 1.7. 7.安装配置sonar-runner

sonar环境搭建简明手册

1.sonar简介

SONAR是一个开源代码质量检查平台,根据官方的说明

sonar用于提高软件质量,提高你的开发团队效率,采用持续检查的方法……
支持c/c++,java,php,ruby,javascript,python,object c…等众多语言

2.安装包

获取安装包:sonarqube-4.3.3.zipsonar-runner-dist-2.4.zip

3.环境要求

1
jdk1.6+

运行时内存最少512M,分析内存2g以上(建议)。吃内存厉害!

4.安装步骤

  • jdk环境1.6+

  • 解压sonar安装包

注意路径中不能含有中文

执行
{sonar_home}/bin/windows-x86-64/StartSonar.bat

如果发现启动成功字样

访问下面的地址:http://localhost:9000

5.修改配置

首先,sonar默认的数据库是derby,在生产环境我们肯定要选择自己的数据库。还好sonar支持的数据库很多,这里我们选择mysql数据库。

  • 创建数据库
  • 创建用户并赋予权限
    启动mysql服务,命令行登陆,执行下面的脚本:

    1
    2
    3
    4
    5
    6
    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;
  • 修改下面的文件

`{sonar_home}/conf/sonar.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
#--------------------------------------------------------------------------------------------------
# WEB SERVER

# Binding IP address. For servers with more than one IP address, this property specifies which
# address will be used for listening on the specified ports.
# By default, ports will be used on all IP addresses associated with the server.
sonar.web.host=localhost

# Web context. When set, it must start with forward slash (for example /sonarqube).
# The default value is root context (empty value).
sonar.web.context=/sonar

# TCP port for incoming HTTP connections. Disabled when value is -1.
sonar.web.port=9000

说明:
参数1:sonar数据库的用户名密码
参数2:sonar数据库的连接字符串
参数3:配置web访问的ip地址或域名
参数4:默认上下文路径 /
第五个参数是启用的端口

重启sonar,发现数据库中已经自动创建了表,证明mysql数据库配置成功。

6.汉化

插件目录
{sonar_home}/extensions/plugins
sonar-l10n-zh-plugin-1.8.jar放到上面的路径下,重启sonar服务
打开页面,汉化成功。

7.安装配置sonar-runner

sonar-runner是一个提供对项目进行分析的命令行工具,安装和配置非常简单。

解压缩sonar-runner-dist-2.4.zip

配置环境变量
SONAR_RUNNER_HOME=解压路径
Path:%SONAR_RUNNER_HOME%\bin;

同样不能含有中文

然后在你需要进行代码检查的项目根目录下建立sonar-project.properties,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Required metadata
sonar.projectKey=my:project
sonar.projectName=kfhx
sonar.projectVersion=1.0

# Paths to source directories.
# Paths are relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Do not put the "sonar-project.properties" file in the same directory with the source code.
# (i.e. never set the "sonar.sources" property to ".")
sonar.sources=f:/bz/kfht

# The value of the property must be the key of the language.
sonar.language=c++

# Encoding of the source code
sonar.sourceEncoding=UTF-8

# Additional parameters
sonar.my.property=value

sonar.projectKey= 是一个标识,不能重复,否则结果会覆盖
sonar.sources=需要检查的源码路径
sonar.language=语言的种类
sonar.sourceEncoding= 项目的编码方式

命令行下执行:
sonar-runner

然后就是漫长的等待,分析结束后访问:http://localhost:9000
就可以看到相关的分析结果。

文章目录
  1. 1. sonar环境搭建简明手册
    1. 1.1. 1.sonar简介
    2. 1.2. 2.安装包
    3. 1.3. 3.环境要求
    4. 1.4. 4.安装步骤
    5. 1.5. 5.修改配置
    6. 1.6. 6.汉化
    7. 1.7. 7.安装配置sonar-runner