参考博客:
https://blog.csdn.net/u010028869/article/details/50736201
sonar平台页面简介
https://blog.csdn.net/t3369/article/details/77977821
安装准备:
- sonarqube-6.4.zip
- sonar-scanner-cli-3.0.1227-linux.zip
- mysql5.6+
- jdk1.8+
sonarqube-6.4.zip安装步骤:
1.将sonarqube-6.4.zip解压至指定目录/usr/local下,并在/etc/profile配置路径:export SONAR_HOME=/usr/local/sonarqube6.4
2.sonar默认端口为9000,不需要更改,在/conf/ sonar.properties配置相关数据库信息如下:
sonar.jdbc.url=jdbc:mysql://192.168.x.xx:3306/sonardb?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
sonar.jdbc.username=root
sonar.jdbc.password=111111
sonar.sorceEncoding=UTF-8
sonar.login=admin
sonar.password=admin
3.进入sonarqube目录启动:./bin/linux-x86-64/sonar.sh start
4. http://192.168.x.xx:9000/projects进入页面,因为版本不同,登录页面也不尽相同,默认登陆的用户名秘密都为admin。
sonar-scanner-cli-3.2.0.1227-linux.zip安装步骤
1.将sonar-scanner-cli-3.2.0.1227-linux.zip解压至指定目录/usr/local下,并在/etc/profile配置路径: export SONAR_RUNNER_HOME=/usr/local/sonar-scanner3.2
2.如果sonar-scanner与sonarqube在同一台服务器,则不需要继续配置了,否则需要在其conf/ sonar-scanner.properties文件中配置数据库等信息,建议安装在同一数据库中。
Maven中集成sonar
Maven 插件会自动把所需数据(如单元测试结果、静态检测结果等)上传到 Sonar 服务器上,需要说明的是,关于 Sonar 的配置并不在每个工程的 pom.xml 文件里(项目多了太麻烦),而是在 Maven 的配置文件 settings.xml 文件里,具体配置如下:
<profiles>
<profile>
<id>sonar</id>
<!--<activation>
<activeByDefault>true</activeByDefault>
</activation>-->
<properties>
<sonar.jdbc.url>
jdbc:mysql://192.168.x.xx:3306/sonardb
</sonar.jdbc.url>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password>111111</sonar.jdbc.password>
<sonar.host.url>http://192.168.5.128:9000</sonar.host.url>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sonar</activeProfile>
</activeProfiles>
网上还有其他集成方式,但可能由于版本或技术原因,我的服务器中使用该配置。
项目中pom.xml插件配置
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-maven3-plugin</artifactId>
<version>6.4.0.25310</version>
</plugin>
</plugins>
</pluginManagement>
网上有各种版本,有的会报错,sonarqube与maven版本不一致编译报错
版本需要和服务器上的sonarqube一致,不然会出现报错,并提示正确版本。
运行
编译命令如下:
mvn clean install
编译成功后,再使用如下命令:
mvn sonar:sonar
运行成功则可以在首页看到项目,接下来就可以进行优化了。
注意项
- sonarqube运行前确保mysql启动,并且mysql中有对应库sonardb;
- IDE启动时如果报错看报错信息:主要是sonarqube与pom.xml中插件版本不匹配。
- 其他一些细节问题不能一一提到,多百度就好。