2

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.

c_anirudh
  • 375
  • 1
  • 22
  • How do you build the project? If you use Maven or Gradle, you may need to appropriately configure the corresponding JaCoCo plugin. If you use JaCoCo 0.8.2 or higher you can use custom annotations containing Generated in the name, so your annotation should work. But I wonder if the target can be package, or it only works at type and method level. – Nenad Apr 27 '22 at 09:40
  • I am using Maven, and have edited the question with my jacoco-maven-plugin configuration in the parent POM file. – c_anirudh Apr 27 '22 at 09:44
  • The version is 0.8.2 and the custom annotations should work with no additional configuration. Would you mind trying with the same annotation on class level, please? – Nenad Apr 27 '22 at 09:48
  • The annotation works at a class level. I am using a version greater than 0.8.2, i.e., 0.8.7, so annotations should work. I don't think changing to version 0.8.2 is the solution. – c_anirudh Apr 27 '22 at 10:06
  • No, you misunderstood me. The version is ok, there is no point in changing the version to 0.8.2. The annotations work on class level, as you already confirmed. I couldn't find in documentation, so I doubt it might not be possible to make it work on package level. But you can try to decompile the library and check the implementation if you think that's really necessary in your case. – Nenad Apr 27 '22 at 11:26
  • As Nenad said, it not work on package level. Release 0.8.2 (2018/08/21) Classes and methods annotated with annotation whose retention policy is runtime or class and whose simple name is Generated are filtered out during generation of report (GitHub #731). https://github.com/jacoco/jacoco/issues/731. – xiaobo9 Apr 29 '22 at 01:43

0 Answers0