如何在jacoco:structure中排除某些类

时间:2019-03-01 12:45:12

标签: jacoco jacoco-maven-plugin

我想排除一些类文件,因为在计算代码覆盖率时不想考虑这些文件。

我的情况是,我从詹金斯(Jenkins)获取多个项目的.exec文件和类文件,并尝试创建一个汇总报告。 因此,我正在使用jacoco:merge创建合并的exec文件,并使用jacoco:report生成报告。 我想在这里排除一些班级。但是我不能在jacoco:structure内部使用。

我的代码附在这里。

<?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>wso2</groupId>
<artifactId>code-coverage-report</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<build>
    <directory>${project.basedir}/target</directory>
</build>

<profiles>
    <profile>
        <id>with-tests</id>
        <activation>
            <property>
                <name>!maven.test.skip</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-jacoco-dependencies</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <includeTypes>jar</includeTypes>
                                <includeArtifactIds>org.jacoco.ant</includeArtifactIds>
                                <stripVersion>true</stripVersion>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <!-- Ant plugin - Merge Jacoco Reports -->
                <!-- Logging and distribution modules are not checked since not relevant -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target xmlns:jacoco="antlib:org.jacoco.ant">
                                    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
                                        <classpath path="${project.build.directory}"/>
                                    </taskdef>

                                    <jacoco:merge destfile="merged.exec">
                                        <fileset dir="jacoco/exec/" includes="*.exec"/>
                                    </jacoco:merge>

                                    <jacoco:report>
                                        <executiondata>
                                            <file file="merged.exec"/>
                                        </executiondata>
                                        <structure name="Final Coverage Report">
                                            <classfiles>
                                                <fileset dir="jacoco/class/classes/"/>
                                            </classfiles>
                                            <excludes>
                                                **/org/xxx/carbon/dataservices/ui/stub/admin/*,
                                                **/org/xxx/carbon/rest/api/stub/types/aaa/*,
                                                **/org/xxx/carbon/endpoint/stub/types/aaa/*,
                                            </excludes>
                                        </structure>
                                        <html destdir="jacoco/tmp_report"/>
                                        <xml destfile="jacoco/tmp_report/coverage-report.xml"/>
                                        <csv destfile="jacoco/tmp_report/coverage-report.csv"/>
                                    </jacoco:report>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.jacoco</groupId>
                            <artifactId>org.jacoco.ant</artifactId>
                            <version>${jacoco.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

<dependencies>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>org.jacoco.ant</artifactId>
        <version>${jacoco.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<properties>
    <jacoco.version>0.7.9</jacoco.version>
</properties>

但是它给出了以下例外。当我尝试运行它时。

An Ant BuildException has occured: structure doesn't support the nested "excludes" element.

请让我知道该怎么做。大帮助。

1 个答案:

答案 0 :(得分:1)

请参见documentation

  

任务报告

     

元素结构

     

请注意,classfilessourcefiles元素接受任何Ant resource collection。因此,也可以过滤类文件集,并允许缩小报告的范围,例如:

<classfiles>
    <fileset dir="classes">
        <include name="org/jacoco/examples/important/**/*.class"/>
    </fileset>
</classfiles>

因此

<classfiles>
  <fileset dir="jacoco/class/classes/">
    <exclude name="org/xxx/carbon/dataservices/ui/stub/admin/**/*.class"/>
    <exclude name="org/xxx/carbon/rest/api/stub/types/aaa/**/*.class"/>
    <exclude name="org/xxx/carbon/endpoint/stub/types/aaa/**/*.class"/>
  </fileset>
</classfiles>