21
votes

I'm having trouble generating AHP reports via JaCoCo in one of my modules. When the build starts, I see JaCoCo correctly setting argLine with:

[INFO] jacoco.agent.argLine set to -javaagent:<...>/.m2/repository/org/jacoco/org.jacoco.agent/0.7.2.201409121644/org.jacoco.agent-0.7.2.201409121644-runtime.jar=destfile=<...>/target/jacoco.exec

However, the .exec hasn't been created yet by the time maven tries to run JaCoCo:

[INFO] Skipping JaCoCo execution due to missing execution data file:<...>/target/jacoco.exec

The jacoco.exec does eventually get created, after maven has skipped JaCoCo execution. Therefore, I can still generate AHP reports, if I re-run the build without cleaning.

I've seen in various other questions that I need to be careful using Maven Surefire with JaCoCo. However, I don't explicitly use argLine in my Surefire plugins, or in any plugin for that matter. I'm starting to wonder if one of the other plugins is hijacking the argLine parameter automatically like JaCoCo does.

Here is a list of all plugins used:

  • jacoco-maven-plugin
  • vertx-maven-plugin
  • maven-resources-plugin
  • maven-dependency-plugin
  • maven-surefire-plugin
  • maven-failsafe-plugin
  • maven-surefire-report-plugin
  • maven-assembly-plugin

I do see one suspicious message in the build output:

[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ <module> ---
[INFO] Changes detected - recompiling the module!

I'm not sure if that's relevant, but it appears twice before the Skipping message, and doesn't appear in a module where JaCoCo works properly.

Any ideas?

*edit - Here's the jacoco config

    <plugins>
        <...>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.jacoco</groupId>
                                    <artifactId>
                                        jacoco-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [0.7.2.201409121644,)
                                    </versionRange>
                                    <goals>
                                        <goal>prepare-agent</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

I'm not sure exactly what that plugin management part is doing, but commenting it out doesn't fix anything. I've also tried putting the JaCoCo plugin config above the surefire/failsafe config in case order mattered for plugins sharing the same goals, but that didn't help either.

*edit 2 - Looks like the problem was surefire's includes. Commenting them out somehow fixes JaCoCo's .exec generation, and JaCoCo works properly.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <!-- <includes>
                    <include>**/unit/**/*Test*.java</include>
                </includes> -->
            </configuration>
        </plugin>

Anyone know why?

6

6 Answers

24
votes

Just an addition to the answers already given. It could happen that in your maven-surefire-plugin configuration you already use the argLine configuration to override something like the memory used. If you do so the argline set by jacoco-maven-plugin will not be used failing to generate the jacoco report. In this case assign a property name to the jacoco-maven-plugin config and then reference it in your maven-surefire-plugin argLine parameter.

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions>
                <!-- prepare agent for measuring unit tests -->
                <execution>
                    <id>prepare-unit-tests</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <append>true</append>
                        <destFile>${sonar.jacoco.reportPath}</destFile>
                        <!-- Sets the VM argument line used when unit tests are run. -->
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>
            </executions>
        </plugin>

[...]

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <printSummary>false</printSummary>
                <redirectTestOutputToFile>true</redirectTestOutputToFile>
                <forkCount>3</forkCount>
                <reuseForks>true</reuseForks>
                <argLine>${surefireArgLine} -Xmx1024m -noverify</argLine>                  
            </configuration>
        </plugin>
5
votes

To solve this issue I would use the following three steps:

  1. Determine which plugin execution is producing the jacoco.exec file. To do that, you could run Maven with debug logging enabled (mvn -X) and look for the jacoco.agent.argLine property name or its value in the output. You could also have a look at the effective POM (mvn help:effective-pom) in case the relevant configuration comes from the parent POM. Note that my guess is that it's an execution of the maven-failsafe-plugin.

  2. Determine the phase that plugin is executed in. E.g. for maven-failsafe-plugin this would likely be integration-test.

  3. Change the configuration of the JaCoCo plugin so that the report goal is executed later in the build lifecycle. E.g. if the jacoco.exec file is produced in the integration-test phase, you could execute the report goal in the post-integration-test phase.

2
votes

In my case the solution was what @massimo mentioned, i was overriding memory values inside <surefireArgLine> tag, after i removed the tag it started generating the report and showed code coverage.

1
votes

for me one of the reason for exec file not getting generated is when i placed the prepare-agent , prepare-package goals inside plugin section of pluginManagement

when i tried the same outside pluginManagement , i was sucessfully able to run the same and jacoco-exec file was generated using mvn clean install command

my pomfile is as below :

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>mmt</groupId>
  <artifactId>jacoco</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>jacoco</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <argLine>-Xms256m -Xmx1524m -XX:MaxPermSize=256m -Duser.language=en</argLine>
 </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>


<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.7.201606060606</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>
    </plugins>
  </build>
</project>
1
votes

I did not find any solution, but I did the following:

mvn clean jacoco:prepare-agent install create file jacoco.exec, but execute mvn sonar:sonar not create File.

Solution:

Step 1: mvn clean jacoco:prepare-agent install

Step 2: copy file jacoco.exec from /src/main/resources Step 3:

Add next plugin on pom.xml

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${basedir}/target/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>
                                        ${basedir}/src/main/resources/ </directory>
                                    <includes>
                                        <include>jacoco.exec</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <encoding>cp1252</encoding>
                </configuration>
            </plugin>

Step 4: mvn clean install

Step 5:

mvn sonar:sonar

READY

Add my section Plugins

<pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>cp1252</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${basedir}/target/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>
                                        ${basedir}/src/main/resources/ </directory>
                                    <includes>
                                        <include>jacoco.exec</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-folder</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <resources>
                                <resource>
                                    <filtering>false</filtering>
                                    <directory>${project.basedir}/src/main/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <encoding>cp1252</encoding>
                </configuration>
            </plugin>
            <!-- Plugin Sonar -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.1</version>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.6.201602180812</version>

                <configuration>
                    <destFile>${project.build.directory}/jacoco.exec</destFile>
                </configuration>

                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${basedir}/target/jacoco.exec</destFile>
                            <dataFile>${basedir}/target/jacoco.exec</dataFile>
                        </configuration>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
0
votes

I also faced the same problem where jacoco plugin was added to parent pom, and jacoco.exec file was generating for one of the sub modules. I had to remove the argLine arguments of maven surefire plugin, then it got generated.