Sonar 本地开发环境安装部署及应用


一、什么是 Sonar?
  Sonar是一个用于代码质量管理的开源平台,用于管理Java源代码的质量。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具,比如pmd-cpd、checkstyle、findbugs、Jenkins。通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。 此外,Sonar 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。

二、安装
本地开发环境大多是 windows ,所以本介绍只针对 win 端,在安装 sonarqube 时,必须先安装 JDK,并配置了环境变量。 
下载地址: https://www.sonarqube.org/downloads/ ,下载后解压 
下载中文插件:https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/latest ,下载后将 jar 文件扔到 根目录/extensions/plugins 文件夹下 
运行根路径/bin/windows-x86-64/StartSonar.bat 启动 Sonar,Sonar server 默认使用 9000 端口,如果 9000 端口已经占用,修改根目录/conf/sonar.properties 文件 sonar.web.port=9000 
打开浏览器进入 http://localhost:9000,将会显示 Sonar 界面,登陆,默认用户名和密码都是admin

三、应用
1. Sonar 分析后台代码 
Eclipse 中在工程上右键 Run As -> Maven build… 运行 maven 命令 : 
jacoco:prepare-agent test jacoco:report sonar:sonar -Dsonar.core.codeCoveragePlugin=jacoco -Dsonar.jacoco.reportPaths=target/jacoco.exec 
然后刷新 http://localhost:9000 
PS:目前测试环境下 sonar 在每次运行时都会转你的单体测试代码,这是为了计算单体覆盖率,如果你的单体直接操作 db 可能会产生脏数据,切记

2. Sonar 分析前台代码 
安装 sonar-scanner , 
下载地址:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner 
解压后设置环境变量到 bin 路径,在前台工程根路径创建 sonar-project.properties 配置文件,

#----- Default SonarQube server
#sonar.host.url=http://localhost:9000
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
# must be unique in a given SonarQube instance
sonar.projectKey=jk:paper-store
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=paper-store
sonar.projectVersion=0.0.1-SNAPSHOT
# This property is optional if sonar.modules is set.
sonar.sources=src
sonar.exclusions=src/mock/**,src/assets/**
sonar.scm.forceReloadAll=true
配置项参见 API:https://docs.sonarqube.org/display/SONAR/Analysis+Parameters

在项目根路径下运行 sonar-scanner 命令,刷新 http://localhost:9000

猜你喜欢

转载自www.cnblogs.com/lyy12332133/p/10152334.html
今日推荐