Maven Jacoco Configuration - Exclude classes/packages from report not working

前端 未结 7 2033
傲寒
傲寒 2020-11-28 21:00

I have a maven multi-module project and I\'m using jacoco-maven for code coverage reports. Some classes should not be reported, as they\'re Spring configuration and I\'m not

相关标签:
7条回答
  • 2020-11-28 21:36

    Though Andrew already answered question with details , i am giving code how to exclude it in pom

               <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.7.9</version>
                    <configuration>
                        <excludes>
                            <exclude>**/*com/test/vaquar/khan/HealthChecker.class</exclude>
                        </excludes>
                    </configuration>
                    <executions>
                        <!-- prepare agent for measuring integration tests -->
                        <execution>
                            <id>jacoco-initialize</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>jacoco-site</id>
                            <phase>package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    For Springboot application

    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>sonar-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.sonarsource.scanner.maven</groupId>
                    <artifactId>sonar-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                           <!-- Exclude class from test coverage -->
                            <exclude>**/*com/khan/vaquar/Application.class</exclude>
                            <!-- Exclude full package from test coverage -->
                            <exclude>**/*com/khan/vaquar/config/**</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    0 讨论(0)
  • 2020-11-28 21:37

    you can configure the coverage exclusion in the sonar properties, outside of the configuration of the jacoco plugin:

    ...
    <properties>
        ....
        <sonar.exclusions>
            **/generated/**/*,
            **/model/**/*
        </sonar.exclusions>
        <sonar.test.exclusions>
            src/test/**/*
        </sonar.test.exclusions>
        ....
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
        <sonar.coverage.exclusions>
            **/generated/**/*,
            **/model/**/*
        </sonar.coverage.exclusions>
        <jacoco.version>0.7.5.201505241946</jacoco.version>
        ....
    </properties>
    ....
    

    and remember to remove the exclusion settings from the plugin

    0 讨论(0)
  • 2020-11-28 21:38

    Use sonar.coverage.exclusions property.

    mvn clean install -Dsonar.coverage.exclusions=**/*ToBeExcluded.java
    

    This should exclude the classes from coverage calculation.

    0 讨论(0)
  • 2020-11-28 21:40

    Another solution:

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.5.201505241946</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>
            <execution>
                <id>default-check</id>
                <goals>
                    <goal>check</goal>
                </goals>
                <configuration>
                    <rules>
                        <rule implementation="org.jacoco.maven.RuleConfiguration">
                            <excludes>
                                <exclude>com.mypackage1</exclude
                                <exclude>com.mypackage2</exclude>
                            </excludes>
                            <element>PACKAGE</element>
                            <limits>
                                <limit implementation="org.jacoco.report.check.Limit">
                                    <counter>COMPLEXITY</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.85</minimum>
                                </limit>
                            </limits>
                        </rule>
                    </rules>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Please note that, we are using "<element>PACKAGE</element>" in the configuration which then helps us to exclude at package level.

    0 讨论(0)
  • 2020-11-28 21:55

    Here is the working sample in pom.xml file.

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
    
    
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
    
                </execution>
    
                <execution>
                    <id>default-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
    
                </execution>
            </executions>
            <configuration>
                <dataFile>target/jacoco.exec</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
                <outputDirectory>target/jacoco-ut</outputDirectory>
                <rules>
                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                        <element>PACKAGE</element>
                        <limits>
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>COMPLEXITY</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.00</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
                <excludes>
                    <exclude>com/pfj/fleet/dao/model/**/*</exclude>
                </excludes>
                <systemPropertyVariables>
    
                    <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    
    0 讨论(0)
  • 2020-11-28 21:59

    Your XML is slightly wrong, you need to add any class exclusions within an excludes parent field, so your above configuration should look like the following as per the Jacoco docs

    <configuration>
        <excludes>
            <exclude>**/*Config.*</exclude>
            <exclude>**/*Dev.*</exclude>
        </excludes>
    </configuration>
    

    The values of the exclude fields should be class paths (not package names) of the compiled classes relative to the directory target/classes/ using the standard wildcard syntax

    *   Match zero or more characters
    **  Match zero or more directories
    ?   Match a single character
    

    You may also exclude a package and all of its children/subpackages this way:

    <exclude>some/package/**/*</exclude>
    

    This will exclude every class in some.package, as well as any children. For example, some.package.child wouldn't be included in the reports either.

    I have tested and my report goal reports on a reduced number of classes using the above.

    If you are then pushing this report into Sonar, you will then need to tell Sonar to exclude these classes in the display which can be done in the Sonar settings

    Settings > General Settings > Exclusions > Code Coverage

    Sonar Docs explains it a bit more

    Running your command above

    mvn clean verify
    

    Will show the classes have been excluded

    No exclusions

    [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** ---
    [INFO] Analyzed bundle '**' with 37 classes
    

    With exclusions

    [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** ---
    [INFO] Analyzed bundle '**' with 34 classes
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题