经历了一段时间的加班赶项目进度之后,今天终于闲下来了。忽然不知道干啥。于是,想着做点什么吧。突然想起了码云上面有个代码分析的功能,用的是 Sonar

于是想来玩玩这个。

一、下载Sonar,和初始化,启动

打开浏览器,搜索sonarqube,进入官网,找到download按钮,下载安装包。浏览器下载慢的话, 可以复制下载链接 到迅雷里边下载。

下载之后,解压到任意目录。

解压后,如果是 windows 64位系统,进入windows-x86-64  目录,双击InstallNTService.bat,成功之后,双击StartSonar.bat 启动。

然后打开浏览器输入:http://192.168.2.100:9000/。可以看到一个界面。

二、配置和创建数据库

sonar需要java环境的支持,分析数据会保存到保存到数据库。支持mysql,oracle,PostgreSQL,SQLServer

所以,这里需要默认你已经配置好了Java开发环境,已经安装了mysql数据库。

打开 sonar  解压目录的,conf,可以看到sonar.properties 文件中有如下配置:

# 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=
sonar #----- 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 #----- Oracle 11g/12c
# - Only thin client is supported
# - Only versions 11.2.x and 12.x of Oracle JDBC driver are supported
# - The JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/
# - If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000
#sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE

上面红色加粗的东西本来是注释掉的,现在我给解开了。并增加了配置。这里的配置,说明的数据库信息,和连接的用户信息。

但是,咱们还没有这个用户,和这个数据库,所以需要执行如下命令来创建用户和数据库。:

#mysql -u root -p

mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> FLUSH PRIVILEGES;

数据库弄好之后,同时需要复制一个数据库驱动放到安装目录的:extensions\jdbc-driver\mysql  下面。比如我用的是:mysql-connector-java-6.0.3.jar

到这里工作完成一半了。如果要想运行分析,则还需要一个东西。根据官方的文档,有如下内容:

 

Once the SonarQube platform has been installed, you're ready to install an analyzer and begin creating projects. A project is created in the platform automatically on its first analysis. However, if you need to set some configuration on your project before its first analysis, you have the option of provisioning it.

Running Analysis

First, you should install the plugin(s) for the language(s) of the project to be analyzed, either by a direct download or through the update center.

Then, you need to choose an analysis method. The following are available:

Note that we do not recommend running an antivirus on the machine where a SonarQube analysis runs, it could result in unpredictable behavior.

SonarQube.com User?

也就是,我们需要下载一个SonarQube Scanner 才能运行代码分析。上面提供了6种,我选择了第一种。下载点这里

1.下载之后,解压到随意目录。假设目录是:D:\develop\sonarqube-6.2\sonar-scanner-2.8

2. 将目录D:\develop\sonarqube-6.2\sonar-scanner-2.8\bin 配置到环境变了path中。

3.打开D:\develop\sonarqube-6.2\sonar-scanner-2.8\conf 下的sonar-scanner.properties 文件,进行如下配置:

#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here #----- Default SonarQube server
sonar.host.url=http://localhost:9000 #----- Default source code encoding
#sonar.sourceEncoding=UTF-8 #----- Global database settings (not used for SonarQube 5.2+)
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar #----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar #----- MySQL
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 #----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE #----- Microsoft SQLServer
#sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor

三、运行代码分析

假设上的步骤你都配合好了。并且,启动了SonarQube。

假设现在有项目myproject,进入到项目的根目录创建一个文件sonar-project.properties,文件内容如下:

# must be unique in a given SonarQube instance
sonar.projectKey=my:myproject #key是唯一的
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=myproject #项目名字
sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=. # Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

进入CMD.进入到项目的目录,就是刚才包含sonar-project.properties文件的这个目录。

执行命令:sonar-scanner

然后就是等待执行结果,大概几分钟吧。成功之后,进入:http://192.168.2.100:9000/

可以看到项目分析的结果,当然了,可能需要等待一会儿才能看到,因为有处理时间,我在运行上面的命令之后,看到cmd窗口成功完成了。马上到这个页面看,发现还没有。

大概一分钟之后刷新,发现,有了!如下所示:

点这个进去看就知道了。里边有很详细的结果汇报。有问题的代码片段展示,并提示如何修改。还是挺好的。

错误记录:

当时分析了三四个项目,其中有一个失败了,其他都成功了,观看日志发现描述是这样的:

INFO: Analysis report generated in 9263ms, dir size=11 MB
INFO: Analysis reports compressed in 5699ms, zip size=4 MB
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 6:28.455s
INFO: Final Memory: 29M/1274M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: Failed to upload report - 500: An error has occurred. Please contact your administrator
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

结果是“EXECUTION FAILURE” 失败了。

失败原因是啥呢? 注意我加粗的文字。 分析的报告大小是 11MB,压缩之后4MB。还是很大的。上传失败的原因是 MYSQL配置问题。

在mysql的终端窗口输入如下命令:

show VARIABLES like '%max_allowed_packet%';

  可以看到max_allowed_packet的大小,用这个值 max_allowed_packet / 1024 /1024 = ? M

所以把这个设置大点就好了。在mysql的命令行窗口执行如下命令:

set global max_allowed_packet=6*1024*1024

  

使用 Sonar 检测代码质量的更多相关文章

  1. RubyCritic:一款不错的检测代码质量工具

    关注代码质量是高效开发必须要做的一件事,那么在 Ruby 开发的过程中,是否有什么好的代码质量检测工具呢?下面由 Ruby 工程师路英瑞介绍一下 RubyCritic--一款还不错的代码质量检测工具. ...

  2. 测试框架:使用SONAR分析代码质量

    介绍 Sonar是一个用于代码质量管理的开源平台,用于管理Java源代码的质量.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具,比如pmd-cpd.checkstyl ...

  3. ant+sonar+jacoco代码质量代码覆盖率扫描

    使用ant构建的java web项目如何做sonar代码质量扫描?以下就是实际遇到并成功使用的案例一.做sonar扫描的准备工作    1.给web项目增加build.xml构建脚本.    2.下载 ...

  4. 通过Sonar的代码质量报告学习【如何写安全高质量的代码】

    1.不要用.size(),改用isEmpty() Using Collection.size() to test for emptiness works, but using Collection.i ...

  5. sonar的安装与代码质量检测实例

    说明:sonar依赖数据库. mysql优化 1.笔者使用的是mysql数据库.首先对mysql做简单的优化配置. [root@localhost bin]# cat /etc/my.cnf [mys ...

  6. Docker 搭建代码质量检测平台 SonarQube

    开始搭建 1.获取 postgresql 的镜像 $ docker pull postgres 2.启动 postgresql $ docker run --name db -e POSTGRES_U ...

  7. Linux下SonarQube代码质量平台的安装和使用方法

    Sonar简介: Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量,可以从七个维度检测代码质量 通过插件形式,可以支持包括java,C#,C/C++,PL/SQL,Cobol,Java ...

  8. 代码质量检测-Sonar

    一. Sonar简介 sonarqube系统是一个代码质量检测工具 由以下四个组件组成(https://docs.sonarqube.org/display/SONAR/Architecture+an ...

  9. 代码质量检测(SonarQube)整合中文版+阿里P3C

    代码质量检测(SonarQube)整合中文版+阿里P3C 简介 SonarQube是一种自动代码审查工具,用于检测代码中的错误,漏洞和代码异味.它可以与您现有的工作流程集成,以便在项目分支和拉取请求之 ...

随机推荐

  1. Linux之文本编辑器Vim

    一.什么是vim vi是一种模式编辑器.vi 是Unix世界里极为普遍的全屏幕文本编辑器,几乎可以说任何一台Unix机器都会提供这套软体,其他的文本编辑器则不一定会存在,但是目前我们使用比较多的是 v ...

  2. SVN Error:Error performing cleanup for

    这个错误,是由于我误删了lib中的jar导致的 一 首先,下载  sqlite3 然后把sqlite3.exe 放到项目文件夹中的.svn文件夹. 如下: 二 接着运行cmd 转到.svn下 三 输入 ...

  3. jzoj6099. 【GDOI2019模拟2019.4.1】Dist

    题目链接:https://jzoj.net/senior/#main/show/6099 考虑直接统计某个点到其它所有点的距离和 我们先把整个团当成一个点建图,处理出任意两个团之间的距离\(dis(i ...

  4. IP地址及网络常识

    一.IP 互联网网络协议(internret protocol address ,IP),IP地址是IP协议提供的一种统一的标准化的地址格式,它会为互联网中的每个网络和每台主机备提供一个逻辑地址,来区 ...

  5. 内网ntp时间同步配置

    选择局域网中的一台机器作为ntp服务器,在ntp server上安装并启动ntpd客户端上要关闭ntpd,安装ntpdateCentOS7上这两个软件都是自带的,只需根据需要打开或者关闭.注意客户端机 ...

  6. Luogu P3731 [HAOI2017]新型城市化

    题目显然可以转化为求每一条边对二分图最大独立集的贡献,二分图最大独立集\(=\)点数\(-\)最大匹配数,我们就有了\(50pts\)做法. 正解的做法是在原图上跑\(Tarjan\),最开始我想复杂 ...

  7. EM算法(Expectation Maximization Algorithm)初探

    1. 通过一个简单的例子直观上理解EM的核心思想 0x1: 问题背景 假设现在有两枚硬币Coin_a和Coin_b,随机抛掷后正面朝上/反面朝上的概率分别是 Coin_a:P1:-P1 Coin_b: ...

  8. LFYZ-OJ ID: 1026 数的计数(数的计算)NOIP2001

    数的计算(数的计数) 题目描述 我们要求找出具有下列性质数的个数(包含输入的自然数n).先输入一个自然数n(n<=1000),然后对此自然数按照如下方法进行处理: 不作任何处理; 在它的左边加上 ...

  9. JDBC事务管理

    JDBC事务管理 概念回顾: 事务:一个包含多个步骤的业务操作,如果这个业务操作被事务管理,那么这个业务操作的多个步骤要么同时失败,要么同时成功 事务操作: 开启事务 提交事务 回滚事务 使用Conn ...

  10. SpringBoot系列: 单元测试2

    之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现 ...