Code coverage in maven build - Skipping JaCoCo exe

2019-04-28 05:56发布

问题:

I am able to get code coverage working fine if I have a single project but now I have a multi module project.

My application is built in the api project and all my integration tests are running in a separate project which uses the artefact built as previous module.

The build runs but I dont get a code coverage report, instead I get the info message:

Skipping JaCoCo execution due to missing classes directory

My coverage report file jacoco-it.exec is created but it seems like the jacoco plugin requires the classes in the project the tests are running in.

Can somebody tell me what I need to do to be able to create a coverage report when the classes are in another module?

回答1:

I have managed to get a workaround of sorts going through trial and error.

It seems the jacoco plugin is happy to create the exec file without the classes but it will not create the report without them, I dont understand how jacoco is working internally so if someone knows could you please explain it?

I am also not sure if what I have done is reliable but it does seem to report the coverage of my selenium driven tests.

My (possible) solution which I have come up with myself is to use the maven resources plugin to copy the classes which have been exploded from the war file in my target\cargo.. directory into the directory target\classes:

 <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy-resources</id>             
            <phase>pre-integration-test</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/classes</outputDirectory>
              <resources>          
                <resource>
                  <directory>${basedir}/target/cargo/configurations/tomcat7x/webapps/calculator-api/WEB-INF/classes</directory>
                  <filtering>false</filtering>
                  <excludes>
                        <exclude>**/*Config*.*</exclude>
                        <exclude>**/*Initialiser*.*</exclude>
                  </excludes>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
    </plugin>

This seems to keep the jacoco plugin happy and I get my code coverage, although it seems the plugin ignores my exclude list now.

Does anybody know if this is actually a solution, it 'seems' to work but I cant find anywhere online where this is a recommended approach and I am also unsure of why the exclude option on the jacoco agent set up no longer seems to work.

I have managed to get around the jacoco plugin not excluding files by just not copying them with the resources plugin but I still dont understand exactly how jacoco is working.