Sonar: VSCode configures SonarLint/SonarLint to connect SonarQube

Description of Requirement

The company has configured Sonar detection for the project code, hoping to synchronize remote detection when developing projects in VSCode.

Preparation

First of all, you need to build a java (version 11+) environment. It is recommended to download it directly from the official website: JDK 11.0.18 ; no Oracle accounthighly recommendedCreate one, the process is very simple, just fill in some information~ The resources and documents on the official website are the most convenient to obtain, with the least problems, much more reliable than most information on the Internet! ! ! (Advice from Ben · Stepping on countless pits · miscellaneous fish)
Java environment variable configuration can refer to: Java environment variable configuration and detailed explanation (the most detailed tutorial on the whole network)
JDK 11.0.18 download

SonarLint

Click "Extension" on the left toolbar of VSCode, search for "SonarLint" and install the plug-in shown in the figure:
Install SonarLint
From the change log, it can be found that SonarLint no longer supports connecting to SonarQube services lower than 7.9 from 2.0.0, so the following will Introduce the operation of connecting the high version and low version SonarQube service respectively.
SonarLint changelog

SonarQube version 7.9+

① After the plug-in installation is complete, click "View" → "Open View..." on the toolbar at the top of VSCode, and click "SonarLint" in the pop-up window:
open view..
Open the SonarLint panel

② You can see the "SONARLINT CONNECTED MODE" panel appears at the bottom left of VSCode:
SONARLINT CONNECTED MODE

③Click "Add SonarQube Connection", and enter the Sever URL of SonarQube in the tab that appears: "http://IP:9000/"
Add SonarQube connection

④ Click "Generate Token", click "Allow Connection" on the redirected SonarQube page, the generated token will be automatically sent to VSCode and filled in the User Token column:
Generate Token
allow connection

⑤ Click "Save Connection" to save the connection, and the established connection will appear under the "SONARLINT CONNECTED MODE" panel:
save connection

⑥Click "Add Project Binding" on the right side of the remote connection, select the corresponding remote project to bind:
bind remote project

SonarQube version < 7.9

① After the plug-in installation is complete, click the configuration icon of the SonarLint plug-in, select "Install another version", and select 1.19.0 in the pop-up window (the latest versions before 2.0.0 are fine):
install another version
Choose 1.19.0 to install

②Log in to the SonarQube console (http://XXX/sessions/new) with the admin account, click the avatar in the upper right corner -> "My Account" -> "Security", enter the token name on the page and click "Generate" to create token (create and copy and save your token string):
My Account
Generate Tokenes

③ After the 1.19.0 installation is complete, click the gear button in the lower left corner -> "Settings" to open the settings page, and click "Open Settings (json)" in the upper right corner to open the user's settings.json:
set up
settings.json

④ Add SonarQube connection configuration items:

"sonarlint.ls.javaHome": "E:\\你的JAVA_HOME路径",
"sonarlint.connectedMode.connections.sonarqube": [
    {
        "serverUrl": "http://IP:9000",
        "token": "你的token",
        "connectionId": "conn1" // 如果有多个连接,此id用于唯一标识连接
    }
],

Add configuration

⑤ In the "SONARLINT RULES" panel on the left, you can view the synchronization detection rules, and the problems detected in the currently open file will appear in the problem panel below:
SonarLint error display

other

After binding the SonarQube service below 7.9, you can also bind the remote project through configuration, but after configuration, you can still only detect the currently opened file, which is not the expected effect of displaying all problems under the project; I feel that the configuration is not bad, just No record, interested friends can refer to the SonarLint-Connected Mode configuration to try~ The operation of this blog is based on this official Wiki.

Press the "Ctrl + Shift + P" shortcut key, enter "sonar" in the command search box, and click "SonarLint: Update all project bindings to SonarQube/SonarCloud" to refresh the local cache.
refresh local cache

Guess you like

Origin blog.csdn.net/qq_36604536/article/details/129350675