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

时间:2017-01-18 16:23:28

标签: java maven sonarqube jacoco-maven-plugin

对于SonarQube 6.0的代码覆盖,JaCoCo maven插件的集成存在很大问题。

我有一个多模块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(我们也为其他类型的类做...)

当我运行mvn命令行时:

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

我的所有报告都很好地生成了,但在声纳中,没有考虑到驱逐因素......

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

2 个答案:

答案 0 :(得分:6)

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

在主模块中

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

答案 1 :(得分:1)

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

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