'How to add an annotation to exclude a package from JaCoCo code coverage report?

I wrote the following custom annotation to exclude elements from JaCoCo code coverage

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PACKAGE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
public @interface ExcludeFromGeneratedReport {

}

However, when I try to use the annotation to exclude a package, by adding the annotaion in the package-info.java file, it doesn't work. The package shows up in the report.

@ExcludeFromGeneratedReport
package some.abc.clientcode;

import some.xyz.coverage.ExcludeFromGeneratedReport;

I am using Maven to build my project. See parent POM file below:

...

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

...

How do I exclude a package from JaCoCo code coverage using an annotation?

PS: The annotation works for methods but not for packages.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source