SonarQube安装配置

1Sonar介绍

Sonar是一个用于代码质量管理的开源平台,用于管理Java源代码的质量。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具,比如pmd-cpd、checkstyle、findbugs、Jenkins。通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。

同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar

此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。

2、配置数据库

Apache Derby 是Sonar自带并且默认安装使用的数据库,此外Sonar对如下数据库提供支持:MySQL 5.x, Oracle 10g XE, Postgresql, MS SqlServer等,本文以mysql为例介绍如何配置数据库: 

1)创建数据库

在mysql中执行如下脚本创建数据库及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;

2)编辑${SONAR_HOME}/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
# Optional properties
sonar.jdbc.driverClassName:                com.mysql.jdbc.Driver

3)配置DB驱动包

如果使用Oracle数据库,必须手动复制驱动类到${SONAR_HOME}/extensions/jdbc-driver/oracle/目录下。其它支持的数据库默认提供了驱动,http://docs.codehaus.org/display/SONAR/Analysis+Parameters 列举了一些常用的配置及默认值.

3、安装、配置Sonar

Sonar的运行需要 JDK 1.7+ 

SonarQube 安装包下载地址:https://sonarsource.bintray.com/Distribution/sonarqube/ 挑选适合自己的安装包,本文中使用的安装包是sonarqube-5.4.zip.md5

创建运行sonar的Linux账户sonar,并设置账户密码:

# useradd sonar
# passwd sonar

使用sonar账户登录Linux。

Sonar默认集成了jetty容器,可以直接启动提供服务。

1)直接启动

编辑.bash_profile,添加环境变量SONAR_HOME

$ vi $HOME/.bash_profile

修改成如下内容:

PATH=$PATH:$HOME/bin
SONAR_HOME=$HOME/sonar
export PATH SONAR_HOME

使环境变量生效

Source $HOME/.bash_profile

运行如下命令启动sonar,其它操作系统sonar均提供了启动脚本

$ ${SONAR_HOME}/bin/linux-x86-64/sonar.sh start

在浏览器中访问: http://localhost:9000/ ,运行界面如下:

Sonar默认的端口是”9000”、默认的上下文路径是”/”、默认的网络接口是”0.0.0.0”,默认的管理员帐号和密码为:admin/admin,这些参数都可以在配置文件中修改:

$ vi ${SONAR_HOME}/conf/sonar.properties

4、配置为自启动服务

使用root账户或者开启sudo权限操作。

创建自启动脚本文件/etc/init.d/sonar

# vi /etc/init.d/sonar

添加如下内容

#!/bin/sh
#
# rc file for SonarQube
#
# chkconfig: 345 96 10
# description: SonarQube system (www.sonarsource.org)
#
### BEGIN INIT INFO
# Provides: sonar
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: SonarQube system (www.sonarsource.org)
# Description: SonarQube system (www.sonarsource.org)
### END INIT INFO
/usr/bin/sonar $*

添加启动服务

# ln -s $SONAR_HOME/bin/linux-x86-64/sonar.sh /usr/bin/sonar
# chmod 755 /etc/init.d/sonar
# chkconfig --add sonar

5、配置插件

a)插件介绍

Sonar支持多种插件,插件的下载地址为:http://docs.codehaus.org/display/SONAR/Plugin+Library

将下载后的插件上传到${SONAR_HOME}extensions\plugins目录下,重新启动sonar。

sonar默认集成了Java Ecosystem插件,该插件是一组插件的合集

·        Java[sonar-java-plugin]:java源代码解析,计算指标等

·        Squid[sonar-squid-java-plugin]:检查违反Sonar定义规则的代码

·        Checkstyle[sonar-checkstyle-plugin]:使用CheckStyle检查违反统一代码编写风格的代码

·        FindBugs[sonar-findbugs-plugin]:使用FindBugs检查违反规则的缺陷代码

·        PMD[sonar-pmd-plugin]:使用pmd检查违反规则的代码

·        Surefire[sonar-surefire-plugin]:使用Surefire执行单元测试

·        Cobertura[sonar-cobertura-plugin]:使用Cobertura获取代码覆盖率

·        JaCoCo[sonar-jacoco-plugin]:使用JaCOCO获取代码覆盖率

下面列出了一些常用的插件:

·        JavaScript代码检查:http://docs.codehaus.org/display/SONAR/JavaScript+Plugin

·        python代码检查:http://docs.codehaus.org/display/SONAR/Python+Plugin

·        Web页面检查(HTML、JSP、JSF、Ruby、PHP等):http://docs.codehaus.org/display/SONAR/Web+Plugin

·        xml文件检查:http://docs.codehaus.org/display/SONAR/XML+Plugin

·        scm源码库统计分析:http://docs.codehaus.org/display/SONAR/SCM+Stats+Plugin

·        文件度量:http://docs.codehaus.org/display/SONAR/Tab+Metrics+Plugin

·        中文语言包:http://docs.codehaus.org/display/SONAR/Chinese+Pack

·        时间表显示度量结果:http://docs.codehaus.org/display/SONAR/Timeline+Plugin

·        度量结果演进图:http://docs.codehaus.org/display/SONAR/Motion+Chart+Plugin

猜你喜欢

转载自blog.csdn.net/weixin_42343424/article/details/80567029
今日推荐