1. 下載SonarScanner
下載地址切心。選擇對(duì)應(yīng)的系統(tǒng)版本下載灼捂。(如果下載慢池户,我這兒有安裝包)
2. 安裝解壓
安裝解壓之后咏雌,根目錄(后文稱(chēng)這個(gè)目錄為
$install_dir
)中有個(gè)/conf/sonar-scanner.properties
, 打開(kāi)這個(gè)文件-
修改
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 # 可以是本地localhost 也可以是服務(wù)器地址赊抖, 如: localhost:9000 sonar.host.url=http://121.41.11.11:9000 #----- Default source code encoding sonar.sourceEncoding=UTF-8 # 如果連接遠(yuǎn)程服務(wù),需要身份驗(yàn)證 sonar.login= <用戶(hù)名> sonar.password = <密碼>
- 配置環(huán)境變量寨典,將
$install_dir/bin
添加到path中
- 打開(kāi)cmd熏迹, 輸入
sonar-scanner -h
,如果出現(xiàn)下圖凝赛,說(shuō)明配置成功注暗。
3. 在項(xiàng)目中使用sonar-scanner
- 在需代碼檢查的項(xiàng)目的根路徑下,創(chuàng)建
sonar-project.properties
配置文件墓猎,內(nèi)容如下:
# must be unique in a given SonarQube instance
# 你的項(xiàng)目名稱(chēng)
sonar.projectKey=abcde
# --- optional properties ---
# defaults to project key
# 你的項(xiàng)目名稱(chēng)捆昏,一般跟projectKey對(duì)應(yīng)
sonar.projectName=abcde
# defaults to 'not provided'
#sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Defaults to .
# 檢查的代碼路徑
sonar.sources=./src
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
- 在項(xiàng)目路徑下打開(kāi)
CMD
(不能用git bush),輸入sonar-scanner
毙沾。完事骗卜。
3. 配置SonarLint
插件
必要環(huán)境:
JRE8
或JRE11
,NODEJS
額外插件:
Language support for Java
-
配置步驟
-
在VS Code中打開(kāi)setting左胞, 配置
JRE
路徑"sonarlint.ls.javaHome": "C:\\Program Files\\Java\\jre1.8.0_201",
-
連接
SonarQube
遠(yuǎn)程服務(wù)"sonarlint.connectedMode.connections.sonarqube": [{ "connectionId": "abcde", "serverUrl": "https://121.41.11.11:9000", "token": "6a19b85b095abcde0ae70702e31ef27c5a0f7b" }]
其中
token
是在SonarQube
項(xiàng)目中的“賬戶(hù)=》安全”中獲得的 -
在前端項(xiàng)目的根目錄中創(chuàng)建
.vscode=>setting.json
寇仓,輸入類(lèi)似下面的代碼{ "sonarlint.connectedMode.project": { "connectionId": "abcde", "projectKey": "abcde" } }
配置完成
-