前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SonarQube的安装与使用

SonarQube的安装与使用

作者头像
Ant丶
发布2022-03-01 20:12:40
1.2K0
发布2022-03-01 20:12:40
举报
文章被收录于专栏:cayzlhcayzlh

SonarQube的安装与使用

前言

随着代码量的越来越多,同时对代码质量的要求也越来越高,对于代码review的需求越来越多。因此,引入SonarQube这个工具对Java代码进行质量管控。

SonarQube(曾用名Sonar(声纳))是一个开源代码质量管理系统。

安装

前置条件

  • 系统环境:Centos 7
  • Java环境:1.8
  • SonarQube版本:6.7.7

由于最新版的SonarQube7.9要求Java环境必须是Java11以上,我们目前开发使用的是1.8,所以选用较低版本的6.7.7

创建sonar用户

由于sonar用到了es,不允许直接使用root用户运行,因此,需要在linux下,创建sonar用户,专门用来运行sonar程序。

假设当前使用的是root用户登录:

代码语言:javascript
复制
useradd sonar
passwd sonar
su sonar

安装mysql数据库,创建sonar库

1、mysql的安装步骤:记录Linux安装Mysql全过程

2、创建sonar库

​ 创建sonar数据库,用于保存soanrqube的扫描数据

安装sonarqube

1、将sonar6.7.7安装包拉到/opt/SonarQube目录

2、解压

代码语言:javascript
复制
unzip sonarqube-6.7.7.zip

3、修改配置

代码语言:javascript
复制
vi ./sonarqube-6.7.7/conf/sonar.properties

添加mysql配置:

代码语言:javascript
复制
#--------------------------------------------------------------------------------------------------
# DATABASE

# User credentials.
# 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=*******

#----- MySQL 5.6 or greater
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

省略了不需要展示的部分

创建软连接

代码语言:javascript
复制
ln -s /opt/SonarQube/sonarqube-6.7.7/bin/linux-x86-64/sonar.sh /usr/bin/sonar

运行sonar

代码语言:javascript
复制
sonar start

重启 sonar restart

停止 sonar stop

查看状态 sonar status

可能遇到的问题

没有目录权限:

将目录授权给sonar用户:

代码语言:javascript
复制
su root
chown -R sonar /opt/SonarQube

没有操作权限

代码语言:javascript
复制
chmod a+x /opt/SonarQube/sonarqube-6.7.7/bin/linux-x86-64/sonar.sh
chmod a+x /opt/SonarQube/sonarqube-6.7.7/bin/linux-x86-64/wrapper
chmod a+x /opt/SonarQube/sonarqube-6.7.7/elasticsearch/bin/elasticsearch

数据库问题

录数据库后执行:

代码语言:javascript
复制
SET GLOBAL max_allowed_packet = 4*1024*1024*16

使用

Java

扫描Java的maven项目,首先要在pom.xml中添加配置:

代码语言:javascript
复制
<plugin>
    <groupId>org.sonarsource.scanner.maven</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <version>3.3.0.603</version>
</plugin>
使用logintoken扫描
代码语言:javascript
复制
mvn sonar:sonar \
  -Dsonar.host.url=http://10.0.2.91:9000 \
  -Dsonar.login=youtoken

其中youtoken可以在登录sonar后台后找到:我的账号 - 安全

token
token
设置settings.xml扫描

修改本地maven的settings.xml文件,添加配置:

代码语言:javascript
复制
<pluginGroups>
    <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
代码语言:javascript
复制
<profiles>
    <profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- 配置 Sonar Host地址,默认:http://localhost:9000 -->
            <sonar.host.url>
                http://10.0.2.91:9000
            </sonar.host.url>
        </properties>
    </profile>
</profiles>

然后执行:

代码语言:javascript
复制
mvn clean verify sonar:sonar

代码语言:javascript
复制
mvn clean install sonar:sonar

代码语言:javascript
复制
mvn clean -Dmaven.test.skip=true verify sonar:sonar

或在IDEA中执行maven插件:

idea sonar plugin
idea sonar plugin
分析

扫描完成后,登录sonar后台,将可以看到本次扫描的项目,和相应的分析:

sonar
sonar

参考

分享计划

博客内容将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/

许可协议

本文采用 署名-非商业性使用-相同方式共享 4.0 国际 许可协议,转载请注明出处。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SonarQube的安装与使用
    • 前言
      • 安装
        • 前置条件
        • 创建sonar用户
        • 安装mysql数据库,创建sonar库
        • 安装sonarqube
        • 创建软连接
        • 运行sonar
        • 可能遇到的问题
      • 使用
        • Java
      • 参考
      相关产品与服务
      云数据库 SQL Server
      腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档