1

I've been working in Java Eclipse with a Maven project for school. I've been using Eclemma for code coverage data, but my professor wants to run my code from the command line and get a code coverage report from there. I've been trying to get Jacoco to work, but I really have never worked with Maven or Pom.xmls before and am pretty lost. Thise is what mine looks like at the moment

<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> yada yada yada </groupId>
  <artifactId> yada yada yada </artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jacoco</groupId>
      <artifactId>org.jacoco.ant</artifactId>
      <version>0.8.1</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.1</version>
          <executions>
            <execution>
              <id>default-prepare-agent</id>
              <goals>
                <goal>prepare-agent</goal>
              </goals>
            </execution>

            <execution>
              <id>post-integration-test</id>
              <phase>post-integration-test</phase>
              <goals>
                <goal>report</goal>
              </goals>
              <configuration>

                <dataFile>target/jacoco.exec</dataFile>

                <outputDirectory>target/jacoco-ut</outputDirectory>
              </configuration>
            </execution>
            <execution>
              <id>default-check</id>
              <goals>
                <goal>check</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
          <configuration>
            <source>1.5</source>
            <target>1.5</target>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.6.0</version>
          <configuration>
            <mainClass> yada yada yada </mainClass>
          </configuration>
          <executions>
            <execution>
              <id>run-selenium</id>
              <phase>integration-test</phase>
              <goals><goal>java</goal></goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.21.0</version>
          <configuration>
            <!-- nothing -->
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

When I execute

$> mvn clean test jacoco:report

or just jacoco:report, I get

[INFO] --- jacoco-maven-plugin:0.8.1:report (default-cli) @ MyFileLocation ---

[INFO] Skipping JaCoCo execution due to missing execution data file.

Not sure what to do at this point...

Paul Myers
  • 41
  • 1
  • 1
  • 6
  • check the answer and let me know if any problem – Faiz Akram Jul 30 '18 at 07:55
  • If the problem continues despite of applying the workarounds, you may have a look at my answer on [maven jacoco: not generating code coverage report](https://stackoverflow.com/questions/25395255/maven-jacoco-not-generating-code-coverage-report/71661614#71661614). – Murat Yıldız Mar 29 '22 at 12:02

2 Answers2

0

Check plugins

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.4.0.905</version>
        </plugin>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <configuration>
                <destFile>${sonar.jacoco.reportPath}</destFile>
                <append>true</append>
            </configuration>
            <executions>
                <execution>
                    <id>agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

Add only This plugins

 <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.9</version>
        <configuration>
            <destFile>${sonar.jacoco.reportPath}</destFile>
            <append>true</append>
        </configuration>
        <executions>
            <execution>
                <id>agent</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
Faiz Akram
  • 559
  • 4
  • 10
  • This did not change anything – Paul Myers Jul 30 '18 at 07:59
  • I ran mvn clean, and mvn eclipse:eclipse after including your alterations. I'm still getting the [INFO] --- jacoco-maven-plugin:0.8.1:report (default-cli) @ MyFileLocation --- [INFO] Skipping JaCoCo execution due to missing execution data file. – Paul Myers Jul 30 '18 at 08:04
0

To record coverage using JaCoCo you need to execute tests with JaCoCo Java agent.

According to https://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html :

Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test.

By default it sets property argLine which is picked by maven-surefire-plugin, however seems that you are not actually using maven-surefire-plugin and instead using java goal of exec-maven-plugin, which

So make sure that JaCoCo agent is used during your tests, e.g. use exec goal of exec-maven-plugin and pass argLine to it - see https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html#arguments

Godin
  • 9,801
  • 2
  • 39
  • 76
  • I am using exec-maven-plugin in order to run my java program, with command "mvn exec:java". To test, I'm using the maven-surefire-plugin command "mvn test". Also, I'm not sure what you mean by "pass argLine" to it. What is argLine supposed to be here? You are saying that this exec-maven-plugin is messing with the command I said I was using in OP, "mvn jacoco:report"? I don't understand – Paul Myers Jul 30 '18 at 18:18
  • @PaulMyers `prepare-agent` sets property `argLine` that points on JaCoCo agent, "make sure that JaCoCo agent is used during your tests" – Godin Jul 30 '18 at 18:24
  • how would I alter the POM to do that? What commands would I run after? – Paul Myers Jul 30 '18 at 18:30
  • 1
    running "mvn jacoco:prepare-agent" beforehand or run them together "mvn jacoco:prepare-agent jacoco:report", or even with test, I still get that message "Skipping JaCoCo execution due to missing execution data file." – Paul Myers Jul 30 '18 at 18:53