SonarQube & Sonar Scanner 安装使用
2020-02-29
目录
1 安装版本
2 配置sonar-scanner.properties 和 环境变量
3 创建sonar-project.properties
4 在项目根目录下执行命令
5 在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=falseView 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中查看覆盖率
参考
[2] SonarQube & SonarQube Scanner