maven jacoco: not generating code coverage report

75,222

Solution 1

Any particular reason why you are using an outdated version of the JaCoCo plugin? For Java 8 support, you have to use at least version 0.7.0 (see changelog).

In your configuration, the report goal is bound to the verify phase, so running mvn test won't generate any report because it does not run the verify phase (test phase comes before verify). You have to use mvn verify to execute tests and generate the report.

The JaCoCo project provides example Maven configurations. You can try "this POM file for a JAR project runs JUnit tests under code coverage and creates a coverage report".

Solution 2

JaCoco Maven Plugin is overriding Surefire argLine, in case you also need to override argLine, be sure to keep argLine variable:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <argLine>-Xmx1024M ${argLine}</argLine> 
  </configuration>
</plugin>

Note you can change this property name, as describe in the jacoco plugin documentation.

Solution 3

This worked for me:

mvn clean install
mvn site

Even though the minimum code coverage was not met and mvn clean install failed, the mvn site build succeeded and created the coverage report at:

.../target/site/jacoco/index.html

Solution 4

Following works for me without any issues

<plugin>
   <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.4</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>                   
        </execution>
    </executions>
</plugin>

NOTE : phase is 'test' in-order to generate the report once tests are executed

Solution 5

I had the same issue. The problem was that i had add the jacoco plugin inside the <plugins> that was listed inside <pluginManagment> tag.

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
       ...
        <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
        <executions>
          <execution>
            <id>default-prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
...
       
      </plugins>
    </pluginManagement>

After add <plugins> as direct child of <build> everything works fine.

<build>
....
 <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.4</version>
        <executions>
          <execution>
            <id>default-prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
...
<build>
Share:
75,222
daydreamer
Author by

daydreamer

Hello Viewer, Some of the places to see my work are BonsaiiLabs My Website

Updated on July 28, 2021

Comments

  • daydreamer
    daydreamer almost 3 years

    I am trying to setup jacoco for my project's code coverage

    My project is based on Java 1.8

    Here is how things look in my project's pom.xml

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.5.10.201208310627</version>
            <configuration>
                <output>file</output>
                <append>true</append>
            </configuration>
            <executions>
                <execution>
                    <id>jacoco-initialize</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-site</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>  
    

    Then I run mvn test and see the following

    $ mvn test
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building pennyapp 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- jacoco-maven-plugin:0.5.10.201208310627:prepare-agent (jacoco-initialize) @ pennyapp ---
    [INFO] argLine set to -javaagent:/Users/harit/.m2/repository/org/jacoco/org.jacoco.agent/0.5.10.201208310627/org.jacoco.agent-0.5.10.201208310627-runtime.jar=destfile=/Users/harit/code/idea/pennyapp/target/jacoco.exec,append=true,output=file
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ pennyapp ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 1 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ pennyapp ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to /Users/harit/code/idea/pennyapp/target/classes
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ pennyapp ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory /Users/harit/code/idea/pennyapp/src/test/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ pennyapp ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to /Users/harit/code/idea/pennyapp/target/test-classes
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.17:test (default-test) @ pennyapp ---
    [INFO] Surefire report directory: /Users/harit/code/idea/pennyapp/shippable/testresults
    [INFO] Using configured provider org.apache.maven.surefire.junit4.JUnit4Provider
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    objc[13225]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
    Running HelloTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.1 sec - in HelloTest
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 4.523 s
    [INFO] Finished at: 2014-08-19T17:56:33-07:00
    [INFO] Final Memory: 10M/119M
    [INFO] ------------------------------------------------------------------------
    

    and then I run mvn jacoco:report and I see

    $ mvn jacoco:report
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building pennyapp 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- jacoco-maven-plugin:0.5.10.201208310627:report (default-cli) @ pennyapp ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.176 s
    [INFO] Finished at: 2014-08-19T17:56:51-07:00
    [INFO] Final Memory: 11M/112M
    [INFO] ------------------------------------------------------------------------
    

    Then I look at target/site/jacoco/index.html and see the following
    enter image description here

    Question
    - What is incorrect in the configuration?
    - How can I generate report?

    Thanks

  • laksys
    laksys over 2 years
    It is strange but saved me lot.
  • Shakirov Ramil
    Shakirov Ramil over 2 years
    thx. after adding <phase>test</phase> site path generated on mvn clean install
  • Ornelio Chauque
    Ornelio Chauque over 2 years
    Good to hear that @laksys.
  • BabyishTank
    BabyishTank about 2 years
    For me, <phase>test</phase> is the key, site folder is now genreated when running 'mvn test'