sonarqube+maven实现代码质量检测
一、安装sonarqube
- 下载安装包 https://www.sonarqube.org/ 注:7.9以上版本不支持mysql
- 下载后目录,bin下是各个系统启动方式,如Windows-bat,Linux-sh。conf目录下为sonar配置文件。
二、配置sonarqube
编辑conf目录下sonar.properties
// 需要为sonar准备一个数据库,也可以更改默认端口 #sonar.web.port=9000
sonar.jdbc.username=root
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://xxx/sonar?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&&useSSL=false&&allowMultiQueries=true
三、启动环境配置
- ES不能再root账号下启动,要创建一个用户
#新增用户
$ adduser sonar
#配置密码
$ passwd sonarUser
#配置路径权限
$ chown -R sonarUser:sonarUser sonarqube-6.7.2
- 启动时报错及解决方式(bin目录下启动)
[1]: max number of threads [1024] for user [hadoop] is too low, increase to at least [2048]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[3]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
vim /etc/security/limits.conf
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
vim /etc/security/limits.d/90-nproc.conf
soft nproc 2048
vi /etc/sysctl.conf
vm.max_map_count=262144
sysctl -p
vim elasticsearch.yml
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
四、maven配置
主要配置sonar服务地址
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>http://localhost:9000</sonar.host.url>
</properties>
</profile>
<activeProfile>sonar</activeProfile>
五、集成jacoco生成报告
生成报告,路径在target下,index.html为网页的报告内容
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
六、生成sonar报告
mvn sonar:sonar -Dsonar.projectKey=com.funtl:itoken-uaa -Dsonar.host.url=http://localhost:9000 -Dsonar.login=token
根据自己的项目配置改动