SonarQube & Sonar Scanner


SonarQube & Sonar Scanner 安裝使用

2020-02-29

 目錄

1 安裝版本
2 配置sonar-scanner.properties 和 環境變量
3 創建sonar-project.properties
4 在項目根目錄下執行命令
在sonarqube中查看覆蓋率

 

除了用mvn sonar:sonar命令把覆蓋率信息保存到sonarqube中外(參考:SoanrQube使用maven進行代碼分析),還可以用sonar Scanner。

1 安裝版本

 2 配置sonar-scanner.properties 和 環境變量

配置 D:\Program Files\sonar-scanner\conf\sonar-scanner.properties

#----- Default SonarQube server
sonar.host.url=http://localhost:9000

在環境變量path中添加 D:\Program Files\sonar-scanner\bin 以便可以執行cmd命令 sonar-scanner

3 創建sonar-project.properties

在項目目錄下,新建並編輯sonar-project.properties  如D:\Code\jacocodemo\sonar-project.properties

# 指定SonarQube instance必須是唯一的
sonar.projectKey=Jacoco
# 設置SonarQube UI顯示的名稱 
# PS:有人會問這里的名稱是否可以是中文名稱,我在網上搜索了好多資料都說是不可以的(至少我看到的資料都是)
#     后來自己嘗試了一下,答案是可以寫成中文的,但是要換一種方式,比如你想把項目名稱命名為“測試”,
#     那么在這里就要寫成“\u6d4b\u8bd5”,那么下面這個參數就應該這樣寫“sonar.projectName= \u6d4b\u8bd5”,
#     說白了就是將中文轉成Unicode
sonar.projectName= JacocoDemo
sonar.projectVersion=1.0
sonar.language=java
# 指定src和classes文件夾位置,當然也可以是全路徑,如果是當前工程根目錄下用“.”表示也可以,比如“sonar.sources=.”
sonar.sources=src
sonar.java.binaries=target
# 下面的這兩個參數作用是相同的,因為有時我們需要指定某個文件夾或者忽略某個文件夾
# sonar.inclusions=src1/**,src3/**
# sonar.exclusions=src2/**,src4/**
# 源碼編碼,默認是系統編碼
sonar.sourceEncoding=UTF-8
# Set jacoco Configuration
# 指定代碼覆蓋率工具
sonar.core.codeCoveragePlugin=jacoco
# 指定exec二進制文件存放路徑
sonar.jacoco.reportPaths=D:/Code/jacocodemo/target/coverage/jacoco-unit.exec
# 以下屬性可選擇性加,當然也可以不加
sonar.dynamicAnalysis=reuseReports
sonar.jacoco.reportMissing.force.zero=false
View Code

項目pom.xml 的build tag內容如下:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!--maven測試為 default 生命周期中的test階段。-->
            <!--test階段與 maven-surefire-plugin 的test目標相綁定了, 這是一個內置的綁定。-->
            <!--Maven通過插件來執行 JUnit 和 TestNG 的測試用例。-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>

            <!--執行單元測試命令:mvn test-->
            <!--結果在target目錄下生產jacoco-unit.exec文件,表明jacoco正確執行-->

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.3</version>
                <configuration>
                    <!--指定生成 .exec 文件的存放位置-->
                    <destFile>target/coverage/jacoco-unit.exec</destFile>
                    <!--Jacoco 是根據 .exec 文件生成最終的報告,所以需指定 .exec 的存放路徑-->
                    <!-- 若用jenkins 插件jacoco,在jenkins查看,這個可以不配置 -->
                    <!--<dataFile>target/coverage/jacoco-unit.exec</dataFile>-->
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <!-- 若用jenkins 插件jacoco,在jenkins查看,這個可以不配置 -->
                    <!--<execution>-->
                        <!--<id>jacoco-site</id>-->
                        <!--<phase>test</phase>-->
                        <!--<goals>-->
                            <!--<goal>report</goal>-->
                        <!--</goals>-->
                    <!--</execution>-->
                </executions>
            </plugin>
        </plugins>
    </build>
View Code

4 在項目根目錄下執行命令

mvn test
sonar-scanner

sonar-scanner輸出日志如下:

D:\Code\jacocodemo>sonar-scanner
INFO: Scanner configuration file: D:\Program Files\sonar-scanner\bin\..\conf\sonar-scanner.properties
INFO: Project root configuration file: D:\Code\jacocodemo\sonar-project.properties
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Windows 10 10.0 amd64
INFO: User cache: C:\Users\qiaoming8006\.sonar\cache
INFO: SonarQube server 7.4.0
INFO: Default locale: "zh_CN", source code encoding: "UTF-8"
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=45ms
INFO: Server id: 49B321BC-AXCAckCh9S-sJe7GUuB7
INFO: User cache: C:\Users\qiaoming8006\.sonar\cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=71ms
INFO: Load/download plugins (done) | time=91ms
INFO: Loaded core extensions:
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=43ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=50ms
INFO: Load active rules
INFO: Load active rules (done) | time=266ms
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=15ms
INFO: Project key: Jacoco
INFO: Project base dir: D:\Code\jacocodemo
INFO: -------------  Scan JacocoDemo
INFO: Base dir: D:\Code\jacocodemo
INFO: Working dir: D:\Code\jacocodemo\.scannerwork
INFO: Source paths: src
INFO: Source encoding: UTF-8, default locale: zh_CN
INFO: Load server rules
INFO: Load server rules (done) | time=70ms
INFO: Language is forced to java
INFO: Index files
WARN: File 'D:\Code\jacocodemo\src\main\resources\application.properties' is ignored because it doesn't belong to the forced language 'java'
INFO: 7 files indexed
INFO: Quality profile for java: Sonar way
INFO: Sensor JavaSquidSensor [java]
INFO: Configured Java source version (sonar.java.source): none
INFO: JavaClasspath initialization
WARN: Bytecode of dependencies was not provided for analysis of source files, you might end up with less precise results. Bytecode can be provided using sonar.java.libraries property
INFO: JavaClasspath initialization (done) | time=9ms
INFO: JavaTestClasspath initialization
INFO: JavaTestClasspath initialization (done) | time=2ms
INFO: Java Main Files AST scan
INFO: 7 source files to be analyzed
INFO: 7/7 source files have been analyzed
INFO: Java Main Files AST scan (done) | time=409ms
INFO: Java Test Files AST scan
INFO: 0 source files to be analyzed
INFO: Java Test Files AST scan (done) | time=0ms
INFO: 0/0 source files have been analyzed
INFO: Sensor JavaSquidSensor [java] (done) | time=823ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=4ms
INFO: Sensor SurefireSensor [java]
INFO: parsing [D:\Code\jacocodemo\target\surefire-reports]
INFO: Sensor SurefireSensor [java] (done) | time=54ms
INFO: Sensor JaCoCoSensor [java]
WARN: Property 'sonar.jacoco.reportMissing.force.zero' is deprecated and its value will be ignored.
INFO: Analysing D:\Code\jacocodemo\target\coverage\jacoco-unit.exec
INFO: No information about coverage per test.
INFO: Sensor JaCoCoSensor [java] (done) | time=54ms
INFO: Sensor SonarJavaXmlFileSensor [java]
INFO: Sensor SonarJavaXmlFileSensor [java] (done) | time=2ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=13ms
INFO: Sensor Java CPD Block Indexer
INFO: Sensor Java CPD Block Indexer (done) | time=12ms
INFO: SCM provider for this project is: git
INFO: 1 files to be analyzed
INFO: 0/1 files analyzed
WARN: Missing blame information for the following files:
WARN:   * src/main/java/com/example/service/MathService.java
WARN: This may lead to missing/broken features in SonarQube
INFO: 4 files had no CPD blocks
INFO: Calculating CPD for 3 files
INFO: CPD calculation finished
INFO: Analysis report generated in 200ms, dir size=43 KB
INFO: Analysis reports compressed in 265ms, zip size=20 KB
INFO: Analysis report uploaded in 54ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard?id=Jacoco
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://localhost:9000/api/ce/task?id=AXCQVIeABn6GJm7kBqtQ
INFO: Task total time: 3.721 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 4.794s
INFO: Final Memory: 20M/265M
INFO: ------------------------------------------------------------------------
View Code

5 在sonarqube中查看覆蓋率

 

 

參考

[1] SoanrQube使用maven進行代碼分析

[2] SonarQube & SonarQube Scanner

 


免責聲明!

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



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