# **环境准备 **
### 内置数据库
Sonar安装成功后,默认内置H2数据库,用于记录单次的扫描结果,对同一个project重复扫码,会覆盖之前的扫描记录,所以H2 数据库只应用于测试,不可以用于生产环境,那如果你是想玩玩,不想长期扫描你的项目,可以不必配置数据库
#### 自定义数据库:
* Oracle
* SQL Server(只支持SQL Server 2014 以及 SQL Server2016)
* PostgreSQL
* MYSQL 版本限制: 8.0>MYSQL>=5.6
(注官方说明,从Sonar7.9版本,不再支持Mysql),以下链接
**End of Life of MySQL Support : SonarQube 7.9 and future versions do not support MySQL.
Please migrate to a supported database. Get more details at **
[https://community.sonarsource.com/t/end-of-life-of-mysql-support](https://community.sonarsource.com/t/end-of-life-of-mysql-support)
[https://jira.sonarsource.com/browse/SONAR-11963](https://jira.sonarsource.com/browse/SONAR-11963)
# **安装步骤**
#### 1.安装sonarqube:
安装的是windows 7.4 community社区版
https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.4.zip
我之前是用最新的8.2版本,不过问题很多,而且评价说最新的不是很稳定,不支持mysql,最后使用的7.4版本,
官网下载比较慢,大家如果无法下载可以留言回复我,或者关注文末二维码私信我。
#### 2.安装JDK:
我安装的JDK11(1.8版本无法安装最新的sonarqube 8.2,7.x是可以的,大家选择时候需要注意)
如果无法找到下载地址,私信我。
#### 3.选择数据库
#### 4.配置:
1. 安装完毕,打开此目录,运行StartSonar.bat
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141728199-729334644.jpg)
2.启动浏览器,访问http://localhost:9000 , 如出现下图则表示安装成功。
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141728700-677255601.jpg)
3.配置连接数据库 :
打开conf文件夹下的sonar.properties文件,底部追加如下:
如果是SQL Server,追加:
* sonar.jdbc.url=jdbc:sqlserver://10.60.215.202;databaseName=sonar
* sonar.jdbc.username=sa
* sonar.jdbc.password=123456
如果是MYSQL:
* sonar.jdbc.url=jdbc:mysql://10.60.253.34:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
* sonar.jdbc.username=root
* sonar.jdbc.password=123456
* sonar.sorceEncoding=UTF-8
* sonar.login=admin
* sonar.password=admin
4.重启,即重新运行StartSonar.bat,使用admin ,admin登陆系统。配置数据库之后,会初始化表信息,耐心等待一段时间。
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141729533-810902315.jpg)
5.Sonar中创建project ,并配置命令
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141730032-416103192.jpg)
5.在IDEA中进到要扫描的项目根目录,执行:
mvn sonar:sonar \ -Dsonar.projectKey=test_project \ -Dsonar.host.url=http://10.60.25.121:9000 \ -Dsonar.login=6dc9cd89575d252d12d450267a64c0ee4bca3c65
扫描结果:
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141730648-829468685.jpg)
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141731386-1398298080.jpg)
这里扫码的时候,IDEA提示lifecycle-mapping jar包找不到,使用这个方式安装一下就好
git clone https://github.com/mfriedenhagenhttps://img.qb5200.com/download-x/dummy-lifecycle-mapping-plugin.git , 然后mvn package即可安装这个缺失的包。
# **集成阿里巴巴p3c**
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141732176-113481443.jpg)
该指南整合了阿里巴巴集团技术团队多年来的最佳编程实践。 随着我们鼓励重用和更好地理解彼此的程序,大量的Java编程团队对项目之间的代码质量提出了苛刻的要求。 过去我们已经看到许多编程问题。 例如,有缺陷的数据库表结构和索引设计可能会导致软件体系结构缺陷和性能风险。 另一个例子是难以维护的混乱代码结构。 此外,未经身份验证的易受攻击的代码容易受到黑客的攻击。 为了解决这类问题,我们在阿里巴巴为Java开发人员开发了本文档。alibaba p3c 地址: https://github.com/alibaba/p3c
如何应用该指南来规范我们的项目代码呢,我们可以在sonar中引入sonar-p3c这个开源插件,下面介绍下详细步骤:
### **build sonar-p3c plugin插件:**
sonar 7.4版本,一定要参照如下链接: https://github.com/rhinoceros/sonar-p3c-pmd/releases/tag/pmd-3.2.0-beta-with-p3c1.3.6-pmd6.10.0 , 100%无问题。
#### 具体步骤
##### a.打包branch
git clone --single-branch --branch p3c-pmd-1.3.6-branch https://github.com/rhinoceros/p3c.git
clone下来之后,打包
mvn clean install -Dgpg.skip=true
##### b.打包plugin
git clone https://github.com/rhinoceros/sonar-p3c-pmd/tree/pmd-3.2.0-beta-with-p3c1.3.6-pmd6.10.0
clone下来之后,打包
mvn clean package
### 部署p3c插件
将上一步打好的jar包 ,放到sonar extensions/plugins下,重启SonarQube
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141732774-1117598650.jpg)
点击Quality Proifles,并点击Create
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141733424-434637050.jpg)
选择Java并命名
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141733946-674104751.jpg)
点击Active More
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141734483-666353322.jpg)
左侧filter搜索p3c,并选择Bulk Change,将p3c规则加入刚创建的profile中
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141735055-295245984.jpg)
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141735485-856064249.jpg)
回到Quality Profiles,设置刚刚的profile为默认
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141736139-1652751633.jpg)
到此,p3c规则成功集成到Sonar中。现在测试下:
点击Create new project
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141736611-50208819.jpg)
选择Java,并生成token,生成mvn命令,Copy,直接在项目里执行即可
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141737103-141403387.jpg)
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141737779-1710432820.jpg)
Idea中扫描会很快,Sonar中会一直显示In Progress,需要等一会才有分析结果。成功执行后结果如下,说明p3c已经成功检查我们的代码
![file](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141738394-335812915.jpg)
------------------------------------------ END ------------------------------------------------------
** 刚刚开始写文章,还望老哥点赞推荐下。**
更多好文,关注作者:
![](https://img2020.cnblogs.com/other/508250/202003/508250-20200312141738807-332880375.png)
> 本文由博客群发一文多发等运营工具平台 [OpenWrite](https://openwrite.cn?from=article_bottom) 发布