如何使用 JaCoCo maven 插件排除 SonarQube 的代码覆盖率文件

How can I exclude files of the Code Coverage of SonarQube using JaCoCo maven plugin

我在为 SonarQube 6.0 的代码覆盖集成 JaCoCo maven 插件时遇到了一个大问题。

我有一个 multi-module Maven 项目 让我们说:

master
    |--core
    |--web
    |--process

在大师 pom.xml 中,我设置了这样的报告配置文件:

<profile>
        <id>reporting</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>pre-unit-test</id>
                            <!--<phase>test</phase> -->
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <!-- Sets the path to the file to write the execution data to. -->
                                <destFile>${sonar.jacoco.reportPath}</destFile>
                                <!-- Connection with SureFire plugin -->
                                <propertyName>sonarUnitTestArgLine</propertyName>
                            </configuration>
                        </execution>
                        <execution>
                            <id>post-unit-test</id>
                            <phase>test</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                            <configuration>
                                <!-- Sets the path to where the execution data is located. -->
                                <dataFile>${sonar.jacoco.reportPath}</dataFile>
                                <!-- Sets the output directory for the code coverage report. -->
                                <outputDirectory>${jacoco.ut.outputdir}</outputDirectory>
                            </configuration>
                        </execution>

                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <forkMode>once</forkMode>
                        <argLine>${sonarUnitTestArgLine} -XX:MaxPermSize=512M -Xms512m -Xmx512m</argLine>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

在孩子中,我通过添加一些排除项来重载配置:

<!-- First attempt -->
<properties>
    <sonar.jacoco.excludes>**/model/**/*</sonar.jacoco.excludes>
</properties>

<!-- Second attempt -->
<properties>
    <sonar.coverage.exclusions>**/model/**/*</sonar.coverage.exclusions>
</properties>

<!-- Third attempt -->
<profiles>
    <profile>
        <id>reporting</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <!-- Exclude model classes (POJO's) -->
                            <exclude>**/model/**/*.class</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

这里的想法是去除代码覆盖的Pojo(我们也对其他类型Class ...)

当我运行mvn命令行时:

mvn clean generate-sources install verify -P reporting -fn

我的所有报告都生成良好,但在 Sonar 中,排除项没有被考虑在内...

你能帮我解决这个问题吗?

经过大量研究,我找到了这个问题的解决方案,我 post 帮助遇到同样问题的人的答案:

在master-module

<properties>
    <sonar.coverage.exclusions>
       **/patternA/**/*,
       **/patternB/**/*
    </sonar.coverage.exclusions>
</properties>

在sub-modules

<profiles>
    <profile>
        <id>report</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <!-- Exclude model classes (POJO's) -->
                            <exclude>**/patternA/**/*.class</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

在主 pom

我尝试在属性部分添加以下详细信息,它对我有用。

<sonar.coverage.exclusions>
     **/patternA/*.java,
     **/patternB/*.java
</sonar.coverage.exclusions>