sonarqube静态扫描代码环境搭建及使用(本地环境)

一、背景

目标:在macbook机器本地环境搭建sonarqube服务,并对本地maven项目代码进行静态扫描,将扫描结果提交到sonarqube服务中,并在web页面中展示出来。
静态代码扫描可以发现的问题:
a、数组越界:例如,数组长度为10,arr.length=10,但出现arr[10]这种越界
b、大量重复代码块
c、死循环
d、定义的变量未使用
e、过高的代码复杂度
f、不可达的僵尸代码
g、空指针引用
h、缓冲区溢出
i、部分内存泄露问题
j、变量类型不匹配
k、使用未初始化的变量

二、搭建步骤

1、下载sonarqube软件

下载地址:https://binaries.sonarsource.com/Distribution/sonarqube/
下载历史版本sonarqube6.7.5的zip压缩包,因为高版本不稳定。解压后,在/bin/macosx-universal-64文件夹下执行sonar.sh console即可运行,访问http://localhost:9000,以默认的admin/admin即可登录进系统页面,此访问地址即是sonar扫描结果的展示系统。
注意:以上的前提是在jdk1.8环境,此时采用的是sonarqube的内置数据库,不支持数据的迁移和维护,一般不用内置数据库,连接外部数据库需要修改conf/sonar.properties文件

2、搭建mysql数据库环境(这里以mysql为例)

sonarqube6.7.5可支持的mysql版本为5.6、5.7,其他版本均不适配!在本地电脑上搭建相应版本的mysql数据库。
在这里插入图片描述
在这里插入图片描述

3、修改sonarqube的数据库连接配置

修改conf/sonarqube.properties文件,该文件可修改访问端口(默认是9000)和指定连接外部数据库(默认是内置数据库)
修改以下几项即可:
sonar.jdbc.username
sonar.jdbc.password
sonar.jdbc.url
sonar.jdbc.driverClassName

#--------------------------------------------------------------------------------------------------
# DATABASE
#
# IMPORTANT:
# - The embedded H2 database is used by default. It is recommended for tests but not for
#   production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.
# - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=root
sonar.jdbc.password=mydatabasepassword
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092

#----- 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
sonar.jdbc.url=jdbc:mysql://23.60.22.121:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
.....
.....
.....

修改完毕重启即可。

4、本地代码扫描结果自动提交到sonarqube服务

1)由于使用maven命令执行代码扫描,所以修改本地maven的conf/settings.xml文件,将以下内容添加到该文件中并保存。

<pluginGroups>
    <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>

<profiles>
    <profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <sonar.host.url>
                http://localhost:9000
            </sonar.host.url>
        </properties>
    </profile>
  </profiles>

2)在本地IDE工具中执行mvn clean verify sonar:sonar命令,即可下载相关插件,并将代码扫描结果提交到sonarqube服务。
扫描结果展示:
在这里插入图片描述
原理图:

提交
读取
本地代码扫描结果
sonarqube连接的数据库
sonarqube页面读取数据库并展示结果
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值