1. 程式人生 > >SonarQube學習入門指南

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>