SonarQube學習入門指南


 1. 什么是SonarQube?

SonarQube 官網:https://www.sonarqube.org/

SonarQube®是一種自動代碼審查工具,用於檢測代碼中的錯誤,漏洞和代碼異味。它可以與您現有的工作流程集成,以便在項目分支和拉取請求之間進行連續的代碼檢查。

2. 使用前提條件

當前版本 SonarQube 7.4

運行SonarQube的唯一先決條件是在您的計算機上安裝Java(Oracle JRE 8或OpenJDK 8)。

注意: 在Mac OS X上,強烈建議安裝Oracle JDK 8而不是相應的Oracle JRE,因為JRE安裝未正確完全設置Java環境。

2.1 硬件要求

  1. SonarQube服務器的小型(個人或小團隊)實例需要至少2GB的RAM才能有效運行,並且1GB的可用RAM用於操作系統。如果要為大型團隊或Enterprise安裝實例,請考慮以下其他建議。
  2. 您需要的磁盤空間量取決於您使用SonarQube分析的代碼量。例如,SonarClube的公共實例SonarCloud擁有超過3.5億行代碼,有5年的歷史。SonarCloud目前在集群Amazon EC2 m5.large實例上運行,每個節點分配50 Gb的驅動器空間。它處理19,000多個項目,大約有1400萬個未解決的問題。SonarCloud在PostgreSQL 9.5上運行,它為數據庫使用了大約250Gb的磁盤空間。
  3. SonarQube必須安裝在具有出色讀寫性能的硬盤上。最重要的是,“data”文件夾包含Elasticsearch索引,當服務器啟動並運行時,將在其上完成大量I / O. 因此,良好的讀寫硬盤性能將對整個SonarQube服務器性能產生很大影響。

2.2 企業硬件建議

對於SonarQube的大型團隊或企業級安裝,需要額外的硬件。在企業級別,監控SonarQube實例/實例管理/ java-process-memory是必不可少的,並且應該隨着實例的增長引導進一步的硬件升級。起始配置應至少包括:

  • 8個核心,允許主SonarQube平台與多個計算引擎工作者一起運行
  • 16GB RAM有關數據庫和ElasticSearch的其他要求和建議,請參閱硬件建議/要求/硬件建議。

2.3 支持的平台

SonarQube Java分析器能夠分析任何類型的Java源文件,無論它們遵循的Java版本如何。

但SonarQube分析和SonarQube服務器需要特定版本的JVM。

 

網頁瀏覽器

要獲得SonarQube提供的完整體驗,您必須在瀏覽器中啟用JavaScript。

 

 參考資料:https://docs.sonarqube.org/latest/requirements/requirements/

3. SonarQube 如何下載安裝配置?

參考資料:https://docs.sonarqube.org/latest/setup/get-started-2-minutes/

收費價格標准如下:

SonarQube 的社區版是免費的,其他版本是收費的

3.1 下載社區版 

3.1.1 比如我們解壓縮到這個目錄 C:\Apps\sonar\sonarqube-7.4\bin\windows-x86-64\

3.1.2 以管理員身份按照上圖順序依次運行這三個文件

 

3.1.3 登錄 http://localhost:9000/  可以看到這個

3.1.4 使用系統默認的管理員憑據(admin / admin)登陸成功。

更詳細內容移步:https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven

4.  使用SonarQube掃描儀分析Maven

Tips:

Sonar 版本7.4

Maven 版本不可小於 3.x 

能訪問之前那個登陸頁面並登陸成功,說明SonarQube已經安裝好了。

初始設置

全局設置

1. 打開位於$ MAVEN_HOME / conf〜/ .m2中的  settings.xml文件

2. 找到<pluginGroups>節點,追加 org.sonarsource.scanner.maven 這個插件

3. 找到以設置插件前綴和可選的SonarQube服務器URL。

Settings.xml 內容示例如下:

<settings>
    <pluginGroups>
        <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
    </pluginGroups>
    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Optional URL to server. Default value is http://localhost:9000 -->
                <sonar.host.url>
                  http://myserver:9000
                </sonar.host.url>
            </properties>
        </profile>
     </profiles>
</settings>

局部設置

如果只是對單個項目需要配置,也可以采取局部設置服務器地址,即在自己項目的POM.xml 中配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xingyun</groupId>
    <artifactId>SpringStaticFactoryPatternSample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- 非必須中間的可刪除 -->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
    </dependencies>
    <!-- 非必須中間的可刪除 -->

    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Optional URL to server. Default value is http://localhost:9000 -->
                <sonar.host.url>
                    http://localhost:9000
                </sonar.host.url>
            </properties>
        </profile>
    </profiles>

    <build>
        <pluginManagement>
            <plugins>
                <!-- 配置編譯插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

 

分析Maven項目

方法一:

使用Sonar 最新插件

配置好后就可以開始分析Maven項目了,在pom.xml文件所在的目錄中運行Maven命令

mvn clean verify sonar:sonar
  
# In some situation you may want to run sonar:sonar goal as a dedicated step. Be sure to use install as first step for multi-module projects
mvn clean install
mvn sonar:sonar

這樣當命令執行完畢后就可以在剛才的web 控制台看到剛才測試的項目了

打開網址:http://localhost:9000/projects

 

方法二:

指定Sonar 插件版本

如果需要指定sonar-maven-plugin的版本而不是使用最新版本

那么需要在項目的POM.xml 中指定版本如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xingyun</groupId>
    <artifactId>TestSample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Optional URL to server. Default value is http://localhost:9000 -->
                <sonar.host.url>
                    http://localhost:9000
                </sonar.host.url>
            </properties>
        </profile>
    </profiles>

    <build>
        <pluginManagement>
            <plugins>
                <!-- 配置編譯插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <!-- 配置分析掃描插件 -->
                <plugin>
                    <groupId>org.sonarsource.scanner.maven</groupId>
                    <artifactId>sonar-maven-plugin</artifactId>
                    <version>3.5.0.1254</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

 

然后執行如下命令:

# Specify the version of sonar-maven-plugin instead of using the latest. See also 'How to Fix Version of Maven Plugin' below.
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.5.0.1254:sonar

這樣當命令執行完畢后就可以在剛才的web 控制台看到剛才測試的項目了

 

打開網址:http://localhost:9000/projects

 

 

方法三:

要將JaCoCo作為Maven構建的一部分執行以生成JaCoCo 的二進制格式,即在target 目錄下生成jacoco.exec 文件

請使用以下命令: 

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar

如果需要忽略測試失敗

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false sonar:sonar

 

然后執行如下命令:

 

# Specify the version of sonar-maven-plugin instead of using the latest. See also 'How to Fix Version of Maven Plugin' below.
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.5.0.1254:sonar

 

這樣當命令執行完畢后就可以在剛才的web 控制台看到剛才測試的項目了

 

打開網址:http://localhost:9000/projects

 

 


 

開啟身份認證

不知道你有沒有發現,我們直接執行命令后便將分析報告提交到web控制台了,沒有加任何權限驗證,這樣是非常不安全的。

還記得之前我們登陸web控制台么?http://localhost:9000/

默認管理員憑據

安裝SonarQube時,會自動創建具有“管理系統”權限的默認用戶:

  • 登錄:admin
  • 密碼:admin

我們可以在項目中通過配置賬號和密碼方式來實現身份認證,但是這樣仍然不是很安全,登陸密碼容易泄露。

開啟Token 身份認證

1. 打開身份認證開關

 Administration > Configuration > General Settings > Security, 然后設置 Force user authentication 屬性為true

2. 創建一個用戶

點擊創建后輸入賬號和密碼,輸入Email , 然后點擊create 即可

3. 點擊下圖位置

彈出如下對話框

 執行成功后如下所示:

點擊sonar-users 下的圖標,彈出如下圖所示

勾選后點擊done 完成

將Execute Analysis 下對勾勾選,即可為該用戶組添加分析執行權限

 

 由於我們創建的用戶屬於這兩個用戶組,所以給這個組賦予權限,那么我們的用戶便也有權限了。

參考資料:https://docs.sonarqube.org/latest/instance-administration/security/

恢復管理員訪問權限

如果您更改了admin密碼然后丟失了密碼,則可以使用以下查詢重置密碼:

update users set crypted_password = '$2a$12$uCkkXmhW5ThVK8mpBvnXOOJRLd64LJeHTeCkSuB3lfaR2N0AYBaSi', salt=null, hash_method='BCRYPT' where login = 'admin'

如果您已刪除admin並隨后鎖定具有全局管理權限的其他用戶,則需要admin使用以下查詢重新授予用戶:

INSERT INTO user_roles(user_id, role) VALUES ((select id from users where login='mylogin'), 'admin');

 


 

參考模板示例一:
pom.xml
<project>
<!--
代碼掃描步驟 --> <profiles> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:mysql://******:3306/sonar?useUnicode=true&amp;characterEncoding=utf8&amp;rewriteBatchedStatements=true&amp;useConfigs=maxPerformance</sonar.jdbc.url> <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver> <sonar.jdbc.username>sonar</sonar.jdbc.username> <sonar.jdbc.password>sonar</sonar.jdbc.password> <sonar.host.url>http://172.*.*.*:9000</sonar.host.url> <sonar.login>ce57e****************4a8969</sonar.login> <sonar.sourceEncoding>UTF-8</sonar.sourceEncoding> <sonar.sonar.test.inclusions>.</sonar.sonar.test.inclusions> <sonar.exclusions>src/test/java/**</sonar.exclusions> </properties> </profile> <profile> <id>sonar-coverage</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <pluginManagement> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.9</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <configuration> <append>true</append> </configuration> <executions> <execution> <id>agent-for-ut</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>jacoco-site</id> <phase>verify</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
</project>

 

參考模板示例二:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xingyun</groupId>
    <artifactId>SpringStaticFactoryPatternSample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- 非必須中間的可刪除 -->
    <dependencies>

        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
    </dependencies>
    <!-- 非必須中間的可刪除 -->

    <profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Optional URL to server. Default value is http://localhost:9000 -->
                <sonar.host.url>http://localhost:9000</sonar.host.url>
                <!-- 配置字符編碼 -->
                <sonar.sourceEncoding>UTF-8</sonar.sourceEncoding>
                <!-- 該項目的獨特關鍵。允許的字符是:字母,數字 - , _ , . 和 : ,與至少一個非數字字符。 -->
                <sonar.projectKey>:</sonar.projectKey>
                <!-- 包含源文件的目錄的逗號分隔路徑。 -->
                <sonar.sources></sonar.sources>
                <!-- 將在Web界面上顯示的項目的名稱。 -->
                <sonar.projectName>Custom Project</sonar.projectName>
                <!-- 項目版本 -->
                <sonar.projectVersion>V0.0.1</sonar.projectVersion>
                <sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
            </properties>
        </profile>
    </profiles>

    <build>
        <pluginManagement>
            <plugins>
                <!-- 配置編譯插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

 




 

 

 

 

 


免責聲明!

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



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