■1.首先介绍一下sonarqube是干什么用的。
SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误。
■2.安装环境和准备
我们介绍在linux下安装sonarqube。linux环境CentOS7,JDK环境JDK1.8,sonarqube版本sonarqube-5.6.1,sonar汉化版本sonar-l10n-zh-plugin-1.11.jar,mysql版本5.7
sonarqube-5.6.1下载地址
https://www.sonarqube.org/downloads/
sonar-l10n-zh-plugin-1.11.jar下载地址
https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases
■3.安装sonarqube
在/usr下建sonar文件夹。
将下载好的sonarqube压缩文件放到sonar文件夹下,并解压。
进入sonarqube-5.6.1/bin/linux-x86-64/文件夹下,启动sonar.sh (我的系统是64位)
打开浏览器,访问http://localhost:9000
■4.配置
打开mysql,创建数据库sonar。
编辑/usr/sonar/sonarqube-5.6.1/conf/sonar.properties
sonar.jdbc.username=root
sonar.jdbc.password=infosky
sonar.jdbc.url=jdbc:mysql://172.22.70.37:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.web.host=172.22.70.45
sonar.web.port=9000
sonar.jdbc.XXX是我们mysql的相关信息。
sonar.web.host=linux环境IP地址
sonar.web.port=设定访问端口号
重启sonarqube服务,再次访问http://172.22.70.45:9000,会有点慢,因为要初始化数据库信息。
汉化配置,将下载好的sonar-l10n-zh-plugin-1.11.jar放到/usr/sonar/sonarqube/extensions/plugins下,然后重启sonarqube服务。
再次访问http://172.22.70.45:9000,汉化已经成功。
■5在eclipse中使用sonar,我们以maven项目为例介绍如何使用。
pom.xml中加入sonar-maven-plugin插件信息
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
profiles节点中加入sonar对应profile内容
<profile>
<id>sonar</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<jar.scope>provided</jar.scope>
<war.name>alpdev</war.name>
<log4jdir>../log/</log4jdir>
<jdbc.url>jdbc:postgresql://172.22.70.60:5432/lpaa</jdbc.url>
<jdbc.username>postgres</jdbc.username>
<jdbc.password>postgres</jdbc.password>
<redis-prefix>alp-dev</redis-prefix>
<redis.host>172.22.70.28</redis.host>
<redis.port>6379</redis.port>
<iGSA.ip>172.22.70.26:7995</iGSA.ip>
<Report.ip>172.22.70.28:8891</Report.ip>
<sonar.jdbc.url>jdbc:mysql://172.22.70.37:3306/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>root</sonar.jdbc.password>
<sonar.host.url>http://172.22.70.45:9000</sonar.host.url>
</properties>
</profile>
其中id为唯一标识符
sonar.XXXX为sonar所需配置,其他为项目所需配置。
如果项目不需要特殊配置,下面这种写法即可
<profile>
<id>sonar</id>
<properties>
<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>root</sonar.jdbc.password>
<sonar.host.url>http://localhost:9000</sonar.host.url>
</properties>
</profile>
配置好后,maven执行sonar:sonar -P sonar
build成功后,再次启动http://172.22.70.45:9000,在页面上将看到项目分析结果。