菜单
一、SonarScanner安装(windows)
1.1下载压缩包并解压
https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/
1.2 配置文件
修改conf/sonar-scanner.properties文件
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://192.168.8.21:9010
#----- Default source code encoding
sonar.sourceEncoding=UTF-8
1.3 配置环境变量
把bin目录放在环境变量中
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图](http://www.fcors.com/wp-content/uploads/2022/08/image-13-1024x417.png)
1.4 检测是否部署成功
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图1 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图1](http://www.fcors.com/wp-content/uploads/2022/08/image-14.png)
二、实现代码扫描
2.1 在SonarQube-Service端创建项目
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图2 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图2](http://www.fcors.com/wp-content/uploads/2022/08/image-15-1024x697.png)
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图3 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图3](http://www.fcors.com/wp-content/uploads/2022/08/image-16-1024x555.png)
创建令牌Token
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图4 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图4](http://www.fcors.com/wp-content/uploads/2022/08/image-17-1024x415.png)
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图5 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图5](http://www.fcors.com/wp-content/uploads/2022/08/image-18-1024x437.png)
生成Sonar-scanner命令
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图6 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图6](http://www.fcors.com/wp-content/uploads/2022/08/image-19-1024x437.png)
2.2在项目的根目录下创建sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=php-demo
# --- optional properties ---
# defaults to project key
sonar.projectName=php-demo
# defaults to 'not provided'
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Defaults to .
sonar.sources=.
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
##如果是java项目需要添加下面binaries,并不要存在target/classes目录
##sonar.java.binaries=target/classes
扩展java扫描
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图7 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图7](http://www.fcors.com/wp-content/uploads/2022/08/image-21.png)
# must be unique in a given SonarQube instance
sonar.projectKey=java-hello2
# --- optional properties ---
# defaults to project key
sonar.projectName=java-hello2
# defaults to 'not provided'
sonar.projectVersion=1.0
#(指明只扫描ava语言)
sonar.language=java
# Path is relative to the sonar-project.properties file. Defaults to .
sonar.sources=src/com
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
sonar.java.binaries=target/classes
2.2 执行扫描命令
sonar-scanner.bat -D"sonar.projectKey=php-demo" -D"sonar.sources=." -D"sonar.host.url=http://192.168.8.21:9010" -D"sonar.login=sqp_f6de55a9ec9a930516a24a8eae4cbcecb91a4324"
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图8 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图8](http://www.fcors.com/wp-content/uploads/2022/08/image-20.png)
代码扫描成功。
2.3 查看报告
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图9 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图9](http://www.fcors.com/wp-content/uploads/2022/08/image-22-1024x545.png)
三、IDEA和SonarLint集成
3.1 添加Sonarlint插件
打开IDEA菜单,File –> Settings –> Plugins,搜索sonar插件,选择SonarLint进行Install,重启IDEA即可
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图10 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图10](http://www.fcors.com/wp-content/uploads/2022/08/image-23.png)
安装完毕后,重启IDEA
3.2 IDEA配置SonarQube配置
打开IDEA菜单,File –> Settings –> Tools,打开SonarLint
添加关联的SonarQube服务端
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图11 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图11](http://www.fcors.com/wp-content/uploads/2022/08/image-24-1024x471.png)
创建Token
http://192.168.8.21:9010/account/security
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图12 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图12](http://www.fcors.com/wp-content/uploads/2022/08/image-25-1024x447.png)
可以选择token或者密码登录
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图13 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图13](http://www.fcors.com/wp-content/uploads/2022/08/image-26-1024x366.png)
3.3 执行代码扫描
点击项目,右键SonarLint–>Analyze woth SpmarLint,查看log日志,确认连接server配置进行检测
![信息安全SonarQube系列(二)SonarScanner安装及实战案例插图14 信息安全SonarQube系列(二)SonarScanner安装及实战案例插图14](http://www.fcors.com/wp-content/uploads/2022/08/image-27-1024x634.png)