Centos 安装 SonarQube

一、 SonarQube说明

1. SonarQube介绍

官网安装地址

参考安装说明

Sonar (SonarQube)是一个开源平台,用于管理源代码的质量.

Sonar 不只是一个质量数据报告工具,更是代码质量管理平台。

支持Java, C#, C/C++, PL/SQL, Cobol, JavaScrip, Groovy 等等二十几种编程语言的代码质量管理与检测。
Sonar可以从以下七个维度检测代码质量,而作为开发人员至少需要处理前5种代码质量问题。

1. 不遵循代码标准
        sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具规范代码编写。
2. 潜在的缺陷
        sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具检 测出潜在的缺陷。
3. 糟糕的复杂度分布
        文件、类、方法等,如果复杂度过高将难以改变,这会使得开发人员 难以理解它们, 且如果没有自动化的单元测试,对于程序中的任何组件的改变都将可能导致需要全面的回归测试。
4. 重复
        显然程序中包含大量复制粘贴的代码是质量低下的,sonar可以展示 源码中重复严重的地方。
5. 注释不足或者过多
        没有注释将使代码可读性变差,特别是当不可避免地出现人员变动 时,程序的可读性将大幅下降 而过多的注释又会使得开发人员将精力过多地花费在阅读注释上,亦违背初衷。
6. 缺乏单元测试
        sonar可以很方便地统计并展示单元测试覆盖率。
7. 糟糕的设计
        通过sonar可以找出循环,展示包与包、类与类之间的相互依赖关系,可以检测自定义的架构规则 通过sonar可以管理第三方的jar包,可以利用LCOM4检测单个任务规则的应用情况, 检测藕合。

通过以下介绍SonarQube的安装、使用说明。

2. 为什么要选择sonarQube?

1. 更加优秀的图形化界面

基本上通过界面就可以对自己项目的代码状况一目了然

2. 可以查询出其它软件难以定位到的问题

比如 : 可能导致空指针异常的问题 (对象在进行使用前没有加空的判断)
可能导致内存泄漏的问题, 在try catch 块里面,直接使用e.printStackTrace()将堆栈信息打印到内存的
可能导致的漏洞 : 成员变量使用public定义的
还有诸如 : 流等未关闭或者是非正常关闭都能够检测出来!
功能非常强大!!

二、 安装

2.1 安装SonarQube web server

安装先决条件:

  1. 首先确保安装JDK
  2. 下载SonarQube,下载解压之后找到对应的启动文件即可启动

2.2 安装Mysql

数据库要求 MySQL >=5.6 && < 8.0

1. 创建数据库

create database sonar;

2. 创建用户并授权

CREATE USER 'sonar'@'localhost' IDENTIFIED BY 'Sonar@123';

3. 为用户设置执行权限

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

4. 修改SonarQube配置文件

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=sonar
sonar.jdbc.password=Sonar@123

默认情况下已经SonarQube已经携带了初Oracle之外的其他数据库驱动

2.2 配置 sonar.properties

1. 配置Elasticsearch存储路径

默认情况下,Elasticsearch数据存储在 <install_directory>/data 中,但不建议用于生产实例。

编辑 <install_directory>/conf/sonar.properties 以配置以下设置:

sonar.path.data=/var/sonarqube/data
sonar.path.temp=/var/sonarqube/temp

用于启动SonarQube的用户必须具有对这些目录的读写权限

2. 启动Web服务器

默认端口为“9000”,上下文路径为“/”。可以在<install_directory>/conf/sonar.properties中更改这些值:

sonar.web.host=192.0.0.1
sonar.web.port=80
sonar.web.context=/sonar

执行以下脚本以启动服务器:

  • 在Linux/Mac OS上:bin/<您的操作系统>/sonar.sh启动
  • 在Windows上: bin/windows-x86-XX/StartSonar.bat

您现在可以在http://localhost:9000上浏览SonarQube (默认系统管理员凭据为admin / admin)。

注意:不要使用Root用户启动SonarQube

# 添加用户
adduser sonar
# 设置密码
passwd  xxxx
# 为文件夹设置用户
chown -R  用户名 文件夹名

2.3 Linux配置为系统服务

新建文件/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 $*

执行下面命令自动启动

# 创建超链接
sudo ln -s $SONAR_HOME/bin/linux-x86-64/sonar.sh /usr/bin/sonar

#修改脚本权限
sudo chmod 755 /etc/init.d/sonar

# 添加自启动
sudo chkconfig --add sonar

三、 使用

3.1 安装必要的插件(汉化包)


安装完成之后重启

3.2 分析项目源代码

使用Maven方式分析项目
其他方式参考:https://docs.sonarqube.org/display/SCAN

  1. settings.xml 配置如下:
<settings>
    <pluginGroups>
        <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
    </pluginGroups>
    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Optional URL to server. Default value is http://localhost:9000 -->
                <sonar.host.url>
                  http://myserver:9000
                </sonar.host.url>
            </properties>
        </profile>
     </profiles>
</settings>

分析Maven项目包括运行Maven目标:sonar:sonar在pom.xml文件所在的目录中。


mvn clean verify sonar:sonar
  
# In some situation you may want to run sonar:sonar goal as a dedicated step. Be sure to use install as first step for multi-module projects
mvn clean install
mvn sonar:sonar
 
# Specify the version of sonar-maven-plugin instead of using the latest. See also 'How to Fix Version of Maven Plugin' below.
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.5.0.1254:sonar

Eclipse项目运行方式:

猜你喜欢

转载自blog.csdn.net/afgasdg/article/details/83186569