SonarQube代码质量管理平台安装与配置

1.安装说明

操作用户:root

软件下载目录:/root/opt

无root权限时,放到自定义目录即可

================

2.预置条件

1.需要JDK1.6+支持;

2.需要Mysql数据库支持;(虽然SonarQube自带了H2数据库,但是为了方便管理数据推荐使用Mysql数据库)

================

3.安装

在安装之前,介绍一下SonarQube和SonarQube Runner之间的关系。

SonarQube是服务器端,它主要有两个功能:1.分析源代码;2.因为它内嵌了Apache模块,所以提供Web端的界面访问。

SonarQube Runner是一个利用SonarQube服务端分析代码的命令行工具,可以把它简单理解为客户端。

下载地址:http://www.sonarqube.org/downloads/

所以,为了安装和调试方便,建议SonarQube和SonarQube Runner都下载。

3.1安装SonarQube

第一步:将下载的http://downloads.sonarsource.com/sonarqube/sonarqube-5.2.zip解压后放到/usr/local目录下。具体步骤如下:

[root@code-test opt]# wget -c http://downloads.sonarsource.com/sonarqube/sonarqube-5.2.zip

[root@code-test opt]# unzip -n sonarqube-5.2.zip -d /usr/local

 

第二步:配置环境变量

[root@code-test ~]# vim /etc/profile

添加

export SONAR_HOME=/usr/local/sonarqube-5.2

无root权限时,  vim ~/.bash_profile

保存退出并使配置生效

[root@code-test ~]# source /etc/profile

 

第三步:配置数据库

mysql> CREATE DATABASE sonar;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'sonar'@'%' IDENTIFIED BY 'sonar';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL ON *.*TO 'sonar'@'%';
Query OK, 0 rows affected (0.00 sec)


第四步:配置sonar.properties

[root@code-test ~]# vim /usr/local/sonarqube-5.2/conf/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

sonar.web.host=0.0.0.0

sonar.web.context=

sonar.web.port=9000

PS:SonarQube默认监听9000端口。

 

版本6.7配置:

第五步:启动服务

[root@code-test ~]# cd /usr/local/sonarqube-5.2/bin/linux-x86-64

[root@code-test ~]# ./sonar.sh start

另外,启动/停止/重启命令如下: 
#./sonar.sh start   启动服务 
#./sonar.sh stop    停止服务 
#./sonar.sh restart 重启服务

第六步:访问SonarQube Web管理界面, http://localhost:9000。如果能够看到这个界面证明SonarQube安装成功啦。

 

3.2安装SonarQube Runner

第一步:将下载的sonar-runner-dist-2.4.zip解压后放到/usr/local目录下。具体步骤如下:

[root@code-test opt]# wget -c http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

[root@code-test opt]# unzip -n sonar-runner-dist-2.4.zip -d /usr/local

 

第二步:配置环境变量

[root@code-test ~]# vim  /etc/profile

添加

export SONAR_RUNNER_HOME=/usr/local/sonar-runner-2.4

export PATH=$PATH:$SONAR_HOME/bin:$SONAR_RUNNER_HOME/bin

保存并退出

[root@code-test ~]# source /etc/profile

 

第三步:配置sonar-runner.properties

[root@code-test conf]# vi /usr/local/sonar-runner-2.4/conf/sonar-runner.properties

找到

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

sonar.login=admin

sonar.password=admin

将前面的#去掉

PS:刚才我们已经看到SonarQube已经可以访问了,所以就将sonar.host.url改成了实际的访问地址。

 

第四步:运行sonar-runner分析源代码

1.打开要进行代码分析的项目根目录,新建sonar-project.properties文件

2.配置sonar-project.properties文件

[root@i-vzdytl5t spring-boot-demo]# cat sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=spring-boot-demo
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=spring-boot-demo
sonar.projectVersion=master

# Path is relative to the sonar-project.properties file. Replace "" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=./
sonar.language=java

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
# Additional parameters
sonar.my.property=value
sonar.java.binaries=/opt/spring-boot-demo/target/classes #编译的完成的,参考https://docs.sonarqube.org/display/PLUG/Java+Plugin+and+Bytecode
3.执行分析sonar-runner

[root@i-vzdytl5t spring-boot-demo]# sonar-runner

4.打开SonarQube的Web界面,就可以看到分析的结果啦。

 

第五步:SonarQube默认是没有安装中文语言包的。进入SonarQube插件目录,下载语言包即可。步骤如下:

[root@code-test ~]# cd /usr/local/sonarqube-5.1.1/extensions/plugins

[root@code-test plugins]# wget -c https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.25/sonar-l10n-zh-plugin-1.25.jar

汉化包放在下面这个目录

/usr/local/sonarqube-5.2/extensions/plugins

然后重启sonar才能生效

cd /usr/local/sonarqube-5.2/bin/linux-x86-64

./sonar.sh  start

================

4.SonarQube可以分析的语言

SonarQube可以分析当下最常用、流行的语言。如:Ruby,Python,Php,Css,Javascript,Java,Go,Web,C#。支持20多种语言。SonarQube安装成功后默认只能分析Java,如果想让它分析其他语言需要安装插件。

支持分析编程语言插件地址:

http://docs.sonarqube.org/display/PLUG/Plugin+Library

如果你是做Web开发的,可以选择安装Php,Css,Javascript,Web这四款就行啦。直接进入到SonarQube的Plugin目录下载,成功后重启SonarQube即可。

例如:安装Php分析插件

[root@code-test ~]# cd /usr/local/sonarqube-5.1.1/extensions/plugins

[root@code-test plugins]# wget -c http://downloads.sonarsource.com/plugins/org/codehaus/sonar-plugins/php/sonar-php-plugin/2.6/sonar-php-plugin-2.6.jar

================

6.参考链接

http://blog.csdn.net/hunterno4/article/details/11687269

http://wenku.baidu.com/view/a5c2a3357375a417876f8f09.html

http://my.oschina.net/zj0303/blog/301474

http://www.cnblogs.com/gao241/p/3190701.html

原文地址:https://www.cnblogs.com/fanshudada/p/10260127.html