菜单
一、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目录放在环境变量中
1.4 检测是否部署成功
二、实现代码扫描
2.1 在SonarQube-Service端创建项目
创建令牌Token
生成Sonar-scanner命令
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扫描
# 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"
代码扫描成功。
2.3 查看报告
三、IDEA和SonarLint集成
3.1 添加Sonarlint插件
打开IDEA菜单,File –> Settings –> Plugins,搜索sonar插件,选择SonarLint进行Install,重启IDEA即可
安装完毕后,重启IDEA
3.2 IDEA配置SonarQube配置
打开IDEA菜单,File –> Settings –> Tools,打开SonarLint
添加关联的SonarQube服务端
创建Token
http://192.168.8.21:9010/account/security
可以选择token或者密码登录
3.3 执行代码扫描
点击项目,右键SonarLint–>Analyze woth SpmarLint,查看log日志,确认连接server配置进行检测