jacoco : Cannot exclude classes

后端 未结 3 904
灰色年华
灰色年华 2020-12-06 08:02

I have a maven project and I want to use jacoco for code coverage. Here is the relevant section of my pom

          
                      


        
3条回答
  •  醉话见心
    2020-12-06 09:00

    Property excludes of report goal specifies which files should be excluded from analysis during generation of report. In case of /path/tp/my/project/target/classes/META-INF/bundled-dependencies/some-third-party-1-jar-with-dependencies.jar@org/slf4j/event/EventConstants.class file is /path/tp/my/project/target/classes/META-INF/bundled-dependencies/some-third-party-1-jar-with-dependencies.jar , and the rest is about class in JAR file.

    Therefore as one of examples of correct configuration:

    
      
        META-INF/**
      
    
    

    As a proof having pom.xml

    
    
      4.0.0
    
      org.example
      example
      1.0-SNAPSHOT
    
      
        UTF-8
      
    
      
        
          junit
          junit
          4.12
          test
        
      
    
      
        
          
            org.jacoco
            jacoco-maven-plugin
            0.8.1
          
        
      
    
    
    

    src/main/java/Example.java

    public class Example {
    }
    

    and src/test/java/ExampleTest.java

    public class ExampleTest {
      @org.junit.Test
      public void test() {
      }
    }
    

    Execution of

    mvn clean jacoco:prepare-agent package
    mkdir target/classes/META-INF/
    cp ~/.m2/repository/org/slf4j/slf4j-api/1.4.2/slf4j-api-1.4.2.jar target/classes
    cp ~/.m2/repository/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar target/classes/META-INF
    mvn jacoco:report
    

    will fail with message

    Error while analyzing /private/tmp/j/target/classes/slf4j-api-1.4.2.jar@org/slf4j/helpers/BasicMarker.class. Can't add different class with same name: org/slf4j/helpers/BasicMarker
    

    while same execution with pom.xml containing exclusion of META-INF/**

    
    
      4.0.0
    
      org.example
      example
      1.0-SNAPSHOT
    
      
        UTF-8
      
    
      
        
          junit
          junit
          4.12
          test
        
      
    
      
        
          
            org.jacoco
            jacoco-maven-plugin
            0.8.1
            
              
                META-INF/**
              
            
          
        
      
    
    
    

    will succeed.

    As a side note: semantic of property excludes of prepare-agent goal is different - it specifies class names (regardless of their on-disk location) that should be excluded from instrumentation during execution of tests.

提交回复
热议问题