Jenkins Jacoco Sonar 整合

使用Jenkins部署项目,Sonar进行代码质量扫描,Jacoco做单元测试覆盖率扫描

本文主要参考

https://www.jianshu.com/p/5e4a145af439

https://ningyu1.github.io/site/post/77-jenkins-sonarqube-jacoco-junit/

maven项目pom文件添加Jacoco插件
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>maven-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                    <skipTests>false</skipTests>
                    <argLine>${argLine} -Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.1</version>
                <configuration>
                    <skip>false</skip>
                    <destFile>${basedir}/target/jacoco.exec</destFile>
                </configuration>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <configuration>
                            <dataFile>${basedir}/target/jacoco.exec</dataFile>
                            <outputDirectory>${basedir}/target</outputDirectory>
                        </configuration>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

maven-surefire-plugin来生成单元测试报告,jacoco-maven-plugin用来生成覆盖率。项目中貌似不添加surefire插件应该也可以,应该这是单元测试的默认插件。但是由于我使用的Jenkins是Docker构建的jenkins/jenkins:lts,必须在surefire插件中配置useSystemClassLoader为false的属性,否则会出现Error: Could not find or load main class org.apache.maven.surefire.booter.ForkedBooter。这是openJdk早期版本的一个bug,因为jenkins镜像是由openjdk的镜像打包而来.

Jenkins Sonar配置

关于Sonar的搭建,本文并不会叙述。以下默认你已装好sonar

本文Jenkins版本是Jenkins ver. 2.150.3,Sonar版本是Sonar Community Edition Version 7.7 (build 23042)

首先Jenkins需要下载Jacoco和Sonar的插件JaCoCo pluginSonarQube Scanner for Jenkins

下载完毕后先配置Sonar,Manage Jenkins->Configure System中配置SonarQube servers

在这里插入图片描述

其中Server authentication token是Sonar管理员账号提供

点击用户头像,点击My Account

在这里插入图片描述

选择Security选项,在Generate Tokens中输入任意字符,点击Generate就生成了token。

在这里插入图片描述

复制Token后在Jenkins上点击Add添加一个Secret text类型的凭据。

在这里插入图片描述

SonarQube Scanner 配置

下载 SonarQube Scanner。由于官网下载非常慢,这里提供我下好的sonar-scanner-cli-3.3.0.1492-linux.zip

https://pan.baidu.com/s/1aDtU13UxflkWM4iARqDaGg 提取码:ilfh
下载后解压zip包,进入该目录的conf文件夹下,修改sonar-scanner.properties将sonar.host.url改为你自己的sonar地址。

#----- Default SonarQube server
sonar.host.url=http://192.168.237.10:9000
#----- Default source code encoding
sonar.sourceEncoding=UTF-8

将文件夹复制到jenkins服务器上,然后到Manage Jenkins->Global Tool Configuration中配置SonarQube Scanner

在这里插入图片描述

在Jenkins上新建Maven项目,然后在Post Steps步骤中添加Execute SonarQube Scanner

在这里插入图片描述

在这里插入图片描述

Analysis properties

sonar.projectKey=com.example:maven-test
sonar.projectName=maven-test
sonar.projectVersion=1.0
sonar.language=java
sonar.sources=src
sonar.sourceEncoding=UTF-8
sonar.core.codeCoveragePlugin=jacoco
sonar.jacoco.reportPaths=target/jacoco.exec
sonar.sources=src/main/java
sonar.tests=src/test/java
sonar.binaries=target/classes
# 单元测试报告目录
sonar.junit.reportsPath=target/surefire-reports
# 代码覆盖率插件
sonar.java.coveragePlugin=jacoco
# jacoco插件版本
jacoco.version=0.8.1
sonar.login=admin
sonar.password=admin
项目 Jacoco配置

Add Post-build Actions步骤中选择Record JaCoco coverage report

在这里插入图片描述

在这里插入图片描述

修改Path to exec file,Path to class directories等属性,按照pom文件中定义好的填写。

注意:一定要将上图Inclusions中的内容删除,否则覆盖率扫描到的类就会为0

下面是配置好的属性,可供参考

在这里插入图片描述

开始构建

简单配好Build属性后即可开始构建

在这里插入图片描述

构建完毕后查看项目target目录,已经正确生成文件

在这里插入图片描述

在Jenkins上也显示出了效果

在这里插入图片描述

点击SonarQube可跳转到sonar的项目预览上

在这里插入图片描述

点击覆盖率图表,查看详细覆盖率显示。

在这里插入图片描述

整合中如果出现各种问题可以参考这里

部分构建日志如下

[maven-test] $ /var/jenkins_home/sonar-scanner-3.3.0.1492-linux/bin/sonar-scanner -Dsonar.host.url=http://192.168.237.10:9000 ******** -Dsonar.sourceEncoding=UTF-8 -Dsonar.jacoco.reportPaths=target/jacoco.exec -Dsonar.sources=src/main/java -Dsonar.language=java -Dsonar.core.codeCoveragePlugin=jacoco -Dsonar.login=admin -Dsonar.junit.reportsPath=target/surefire-reports -Dsonar.binaries=target/classes -Dsonar.projectVersion=1.0 -Dsonar.projectKey=com.example:maven-test -Dsonar.password=admin -Dsonar.tests=src/test/java -Djacoco.version=0.8.1 -Dsonar.java.coveragePlugin=jacoco -Dsonar.projectName=maven-test -Dsonar.projectBaseDir=/var/jenkins_home/workspace/maven-test
INFO: Scanner configuration file: /var/jenkins_home/sonar-scanner-3.3.0.1492-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: /var/jenkins_home/workspace/maven-test/sonar-project.properties
INFO: SonarQube Scanner 3.3.0.1492
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-957.12.2.el7.x86_64 amd64
INFO: User cache: /var/jenkins_home/.sonar/cache
INFO: SonarQube server 7.7.0
INFO: Default locale: "en", source code encoding: "UTF-8"
INFO: Load global settings
INFO: Load global settings (done) | time=459ms
INFO: Server id: 64FF84BE-AWtAeLgPjb67EKk_9O5z
INFO: User cache: /var/jenkins_home/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=406ms
INFO: Load/download plugins (done) | time=537ms
INFO: Process project properties
INFO: Execute project builders
INFO: Execute project builders (done) | time=3ms
INFO: Project key: com.example:maven-test
INFO: Base dir: /var/jenkins_home/workspace/maven-test
INFO: Working dir: /var/jenkins_home/workspace/maven-test/.scannerwork
INFO: Load project settings for component key: 'com.example:maven-test'
INFO: Load project settings for component key: 'com.example:maven-test' (done) | time=377ms
INFO: Load project repositories
INFO: Load project repositories (done) | time=487ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=429ms
INFO: Load active rules
INFO: Load active rules (done) | time=7382ms
INFO: Indexing files...
INFO: Project configuration:
INFO: 7 files indexed
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for java: Sonar way
INFO: ------------- Run sensors on module maven-test
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=684ms
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=49ms
INFO: JavaTestClasspath initialization
WARN: Bytecode of dependencies was not provided for analysis of test files, you might end up with less precise results. Bytecode can be provided using sonar.java.test.libraries property.
INFO: JavaTestClasspath initialization (done) | time=0ms
INFO: Java Main Files AST scan
INFO: 4 source files to be analyzed
INFO: 4/4 source files have been analyzed
INFO: Java Main Files AST scan (done) | time=1483ms
INFO: Java Test Files AST scan
INFO: 3 source files to be analyzed
INFO: Java Test Files AST scan (done) | time=280ms
INFO: Sensor JavaSquidSensor [java] (done) | time=3169ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 3/3 source files have been analyzed
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=28ms
INFO: Sensor SurefireSensor [java]
INFO: Property 'sonar.junit.reportsPath' is deprecated. Use property 'sonar.junit.reportPaths' instead.
INFO: parsing [/var/jenkins_home/workspace/maven-test/target/surefire-reports]
INFO: Sensor SurefireSensor [java] (done) | time=71ms
INFO: Sensor JaCoCoSensor [java]
WARN: Property 'sonar.jacoco.reportMissing.force.zero' is deprecated and its value will be ignored.
INFO: Analysing /var/jenkins_home/workspace/maven-test/target/jacoco.exec
INFO: No information about coverage per test.
INFO: Sensor JaCoCoSensor [java] (done) | time=134ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=1ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=24ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=1ms
INFO: Sensor Java CPD Block Indexer
INFO: Sensor Java CPD Block Indexer (done) | time=32ms
INFO: 2 files had no CPD blocks
INFO: Calculating CPD for 2 files
INFO: CPD calculation finished
INFO: Analysis report generated in 261ms, dir size=83 KB
INFO: Analysis report compressed in 53ms, zip size=21 KB
INFO: Analysis report uploaded in 644ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.237.10:9000/dashboard?id=com.example%3Amaven-test
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://192.168.237.10:9000/api/ce/task?id=AWtP57KDfv1pLALVruLj
INFO: Analysis total time: 18.678 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 21.134s
INFO: Final Memory: 15M/50M
INFO: ------------------------------------------------------------------------
[JaCoCo plugin] Collecting JaCoCo coverage data...
[JaCoCo plugin] target/jacoco.exec;target/classes;src/main/java; locations are configured
[JaCoCo plugin] Number of found exec files for pattern target/jacoco.exec: 1
[JaCoCo plugin] Saving matched execfiles:  /var/jenkins_home/workspace/maven-test/target/jacoco.exec
[JaCoCo plugin] Saving matched class directories for class-pattern: target/classes: 
[JaCoCo plugin]  - /var/jenkins_home/workspace/maven-test/target/classes 4 files
[JaCoCo plugin] Saving matched source directories for source-pattern: src/main/java: 
[JaCoCo plugin] Source Inclusions: 
[JaCoCo plugin] Source Exclusions: 
[JaCoCo plugin] - /var/jenkins_home/workspace/maven-test/src/main/java 4 files
[JaCoCo plugin] Loading inclusions files..
[JaCoCo plugin] inclusions: []
[JaCoCo plugin] exclusions: []
[JaCoCo plugin] Thresholds: JacocoHealthReportThresholds [minClass=0, maxClass=0, minMethod=0, maxMethod=0, minLine=0, maxLine=0, minBranch=0, maxBranch=0, minInstruction=0, maxInstruction=0, minComplexity=0, maxComplexity=0]
[JaCoCo plugin] Publishing the results..
[JaCoCo plugin] Loading packages..
[JaCoCo plugin] Done.
[JaCoCo plugin] Overall coverage: class: 75, method: 67, line: 54, branch: 25, instruction: 57
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐