• 【Java】 Maven多模块项目上传到Sonar扫描问题合集


    上传到Soanr时,项目有单元测试数,但是覆盖率为0

    修改pom.xml

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.4.2</version>
                        <configuration>
                            <skipTests>false</skipTests>
                            <testFailureIgnore>true</testFailureIgnore>
                            <includes>
                                <include>**/*Test.java</include>
                            </includes>
                            <excludes>
                                <!--<exclude>**/Abstract*.java</exclude>-->
                                <!--<exclude>**/*Service.java</exclude>-->
                            </excludes>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.7.9</version>
                        <executions>
                            <execution>
                                <id>pre-test</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
    

    必要时带上参数

    clean cobertura:cobertura -Dcobertura.report.format=xml package -Dmaven.test.failure.ignore=true sonar:sonar -Dsonar.language=java
    

    多模块项目通过Jenkins构建扫描上传到Sonar时,代码覆盖率很低,只覆盖了单模块的代码,这时可以通过配置JaCoCo解决该问题。
    参考资料

    修改pom.xml

                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.7.9</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    通过clean org.jacoco:jacoco-maven-plugin:prepare-agent jacoco:report-aggregate install 可在target下生成jacoco-aggregate覆盖相关报告

    注:jacoco版本低于0.7.9 jacoco:report-aggregate参数可能不存在
    多模块项目整合覆盖率还可以指定report目录

     <properties>
            <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
            <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
     </properties>        
    
     <build>
             <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.7.9</version>
                        <configuration>
                            <destFile>${sonar.jacoco.reportPath}</destFile>
                            <append>true</append>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>report</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </pluginManagement>
     </build>
    
  • 相关阅读:
    [WSUS] Windows Server Update Service 更新后,出现错误不能连接
    [SQLServer] 数据库SA用户被锁定或者忘记密码的恢复
    [SQLServer] 内存占用查看
    [IIS] [PHP] 500.19 随机出现
    [OPENSSL下载][证书] OPENSSL将PFX证书转换为PEM格式
    [VS2015] [asp.net] 允许远程访问本机正在DEBUG的服务
    [VS2008] 安装 AnkhSVN 后,选项中选择它,Pending Changes 窗口一闪而过,源代码管理工具自动跳回 【None】
    [IIS] 配置PHP的过程与坑
    10分钟快速入门vue.js
    前端接口请求的几种方式
  • 原文地址:https://www.cnblogs.com/xcmelody/p/10961121.html
Copyright © 2020-2023  润新知