工具安装:代码检测工具SonarQube安装步骤

SonarQube 是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。目前支持java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy等二十几种编程语言的代码质量管理与检测。持续检查项目整体的健康程度,如项目整体的Bugs、Vulnerabilities、Code Smells。

下载地址:https://www.sonarqube.org/downloads/

下载社区版本: community 的版本

下载后直接解压,如下图 windows系统启动:

直接启动

一些小问题:双击 StartSonar.bat 可能会失败,再次打开的时候提示正在运行:

这是系统的一个小Bug,此时需要打开资源管理器(Ctrl + Shift + Esc) 找到Java(TM) Platform SE binary,直接结束任务,可能会多个,都要结束掉。再次重启即可!

配置数据库

找到 conf 目录,如下图:

打开 sonar.properties 文件,添加内容如下:

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=root
sonar.jdbc.password=root
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin

效果如下:

配置结束后,需要在数据库中创建一个 sonar 库,创建空数据库即可,系统会自动初始化数据表。

此时,再次启动即可,初次启动可能比较慢,需要几分钟的时间。

登录

启动成功后,在浏览器中输入 http://localhost:9000 即可打开登录页面,用户名/密码为默认的 admin/admin

使用:

Maven命令式启用

在工程根目录(pom.xml所在目录)执行命令: 

mvn sonar:sonar

Sonar Scanner 方式启用

Windows系统下

下载 sonar-scanner-cli-3.3.0.1492-windows 包,直接解压即可

  • 1. 修改配置文件 

内容如下:

#Configure here general information about the environment, such as SonarQube server connection 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
  • 2. 配置环境变量: 

新建变量:SONAR_SCANNER_HOME = D:\software\sonar-scanner-cli-3.3.0.1492-windows

配置path: %SONAR_SCANNER_HOME%\bin;

配置结束后,在cmd命令中执行  sonar-scanner -h  如下,即配置成功:

在项目父工程pom.xml同目录创建 sonar-project.properties 

# must be unique in a given SonarQube instance
sonar.projectKey=project-name
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=project-name
sonar.projectVersion=1.0 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=./
sonar.java.sources=./
sonar.java.binaries=./
sonar.exclusions**/*Test.java, target/**
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

在同目录,执行cmd命令:

sonar-scanner

编译结果如下:

打开指定链接地址,即可查看检测结果

 

Linux 系统下(配置环境变量)

vim /etc/profile  添加内容如下:

export SONAR_SCANNER_HOME=/app/sonarqube/sonar-scanner-4.2.0.1873-linux
export PATH=$PATH:$SONAR_SCANNER_HOME/bin

其它步骤与windows系统下一致

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值