SoanrQube使用maven进行代码分析

SoanrQube使用maven进行代码分析

2020-02-26

目录

1 安装版本
2 更改maven的settings.xml
3 执行maven命令
4 收集显示代码覆盖率

1 安装版本

C:\Users\ming8006>mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: D:\software\maven\bin\..
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: D:\Program Files\Java\jdk1.8.0_231\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

 

2 更改maven的settings.xml

D:\software\maven\conf\settings.xml

    <profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <sonar.jdbc.url>
                jdbc:mysql://127.0.0.1:3306/sonar
            </sonar.jdbc.url>
            <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
            <sonar.jdbc.username>root</sonar.jdbc.username>
            <sonar.jdbc.password>Quality123</sonar.jdbc.password>
            <sonar.host.url>http://127.0.0.1:9000</sonar.host.url>
        </properties>
    </profile>

3 执行maven命令

Maven 打包 package install deploy 区别

mvn compile
mvn sonar:sonar

 图1 代码扫描结果

如上图1,虽然世例有单元测试,但代码覆盖率为0,我们可以修改pom.xml配置,如下所示:

4 收集显示代码覆盖率

pom.xml

<?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>

    <groupId>com.demo</groupId>
    <artifactId>SonarDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--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.exec文件,表明jacoco正确执行-->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
View Code

执行命令

mvn test
mvn sonar:sonar

图2 sonarQube显示代码覆盖率

参考:

[1] SonarQube7.4安装和使用

[2] sonarqube使用maven进行代码分析 

[3] 解决sonar单元测试覆盖率为0的问题

[4] Maven进行测试Test

[5] Jenkins、sonarqube、jacoco简单集成管理自动部署、代码质量、单元测试

 

posted @ 2020-02-26 16:53  明-Ming  阅读(617)  评论(0编辑  收藏  举报