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

标签 java maven sonarqube jacoco-maven-plugin

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

我有一个多模块 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>

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

<!-- 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 中,排除并未考虑在内......

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

最佳答案

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

在主模块中

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

在子模块中

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

关于java - 如何使用 JaCoCo maven 插件排除 SonarQube 代码覆盖率的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41724423/

相关文章:

java - System.in.read() 不会阻止读取更多输入字符?

java - 如何使应用程序构建 block 线程安全?

java - IntelliJ Maven 项目有 UnsatisfiedLinkError : failed to locate library: lwjgl. dll

java - Servlet 调用失败。使用 Spring 支架

java - Gradle:将多项目依赖项压缩到应用程序JAR文件中?

java - 有没有办法让 SonarQube 只警告不完整的 Switch 语句?

java - 令人困惑的 Sonar 错误

java - 在 TextView 上绘制

java - Java 中的农夫、狼、山羊和卷心菜广度优先和深度优先搜索

ant - 使用 Ant 将 JaCoCo 集成到 Sonar 中