IDEA中使用插件sonarlint連接到SonarQube對代碼質量進行管控


申明:以下部分技術是網絡上搜索來的,由於距離寫本文時過去一段時間,無法再查找到原來網址,

無法署名版權,如果您看到此處引用您的文字,請留言聯系我署名版權,相應的辦法歸原作者所有,在此表示感謝。

 

前文:本地安裝SonarQube Community8.1社區版進行代碼質量管控中已經詳細講解了SonarQube社區版的安裝方法

本文將引導大家怎樣使用IDEA插件sonarlint對代碼質量檢測並修復。

SonarLint

 

 

 菜單路徑 :File--Setting-Plugin 輸入SonarLint搜索插件

SonarLint is an IDE extension that helps you detect and fix quality issues as you write code. Like a spell checker, SonarLint squiggles flaws so they can be fixed before committing code.

You can get it directly from the IntelliJ IDEA Plugin Repository, and it will then detect new bugs and quality issues as you code (Java, Kotlin, Ruby, JavaScript, PHP and Python).
If your project is analyzed on SonarQube or on SonarCloud, SonarLint can connect to the server to retrieve the appropriate quality profiles and settings for that project. Java 8 is required to run SonarLint.

大意:

SonarLint是一個 IDE 擴展,可幫助您在編寫代碼時檢測和修復質量問題。與拼寫檢查器一樣,SonarLint 會在缺陷代碼下面畫線,以便在提交代碼之前修復這些缺陷。

你可以直接從IntelliJIDEA插件存儲庫得到它,當你編寫代碼(Java,Kotlin,Ruby,JavaScript,PHP和Python)時然后它會檢測新的錯誤和質量問題.

如果在SonarQube或SonarCloud上分析您的項目,SonarLint 可以連接到服務器以檢索該項目的相應質量配置文件和設置。運行SonarLint需求Java 8。

配置插件

1、配置Token

打開本地SonarQube管理后台,輸入賬戶密碼,默認admin/admin

 

 

創建項目

 

 

 

 在上面畫圈的任意一處,點擊新建項目

 

 

 創建令牌

 

 

 獲得令牌

 

 

 

插件安裝成功以后,打開插件配置界面,路徑:File--Setting-Other Setting

 

 

 

 打開SonarLint General Settings,按照下圖輸入

 

 

 把上一步的token輸入(寫博客截圖,忘記保存了token,下面是錯誤的)

 

 

 

2、綁定到當前項目

打開SonarLint Project Settings

 

 

 connection選擇剛剛的配置“local”,選擇我們剛剛創建的項目

 

3、項目文件分析

 

 配置好以后打開IDEA,查看控制台SonarLint選項卡

 

 

3.1、單個文件分析

 點擊左側按鈕分析,會分析當前這被打開的文件,比如筆者本地打開tts_offline.js

 

 

 他會立馬分析tts_offline.js這個文件

3.2、項目全局分析

右鍵項目:選擇SonarLint--Analyze with SonarLint

 

 

 

檢測結果

 

 

 

 登錄SonarQube管理后台 http://localhost:9000后台效果,查看質檢效果

 

 

 

 

 

 

3.3、maven方式使用SonarQube

各位看官,可以使用以上方法去檢查項目潛在質量問題,當然也可以使用maven,maven配置如下

 

 

 

代碼

<profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <sonar.host.url>http://127.0.0.1:9000/</sonar.host.url>
            </properties>
        </profile>
</profiles>

<plugins>
<plugin>
    <groupId>org.sonarsource.scanner.maven</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <version>3.6.0.1398</version>
</plugin>
</plugins>

 如果報以下錯誤:

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project mes:

Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties sonar.login and sonar.password. -> [Help 1]

maven配置文件(pom.xml)添加配置

<properties> 
      <sonar.login>admin</sonar.login>
      <sonar.password>你的sonarQube登錄密碼</sonar.password>
</properties>

 

maven使用方法:

mvn sonar:sonar

 

4 筆者自己的方法

以上方法在提交代碼的時候浪費大量時間檢查質量,筆者在網絡上找到一個辦法,直接用命令行方式運行

codeAnalysis.bat

mvn sonar:sonar -Dsonar.projectKey=test -Dsonar.host.url=http://localhost:9000 -Dsonar.login=be2a8b0cf932f7294680895e152049439675b912

保存以上文件,雙擊執行即可把質量問題提交到sonarQube平台

 如果卸載SonarLint,請參考:IDEA中關閉sonar代碼質量檢測

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM