Can Jacoco do code coverage without source files showing line num and source code highlighting

49 views
Skip to first unread message

kir...@gmail.com

unread,
Mar 12, 2019, 12:57:05 PM3/12/19
to JaCoCo and EclEmma Users
I currently have a Maven project with /src/main/java which includes all of my project source and /src/test/java with my tests.

My POM uses jacoco-maven-plugin and is currently setup with prepare-agent and report goals.

Right now Jacoco works perfect giving me all detail line numbers and source code highlighting where code was not covered.

I'm curious can Jacoco do code coverage at all and do it at this level of detail if /src/main/java did not exist and only the built files(eg JAR and classes) existed in /target?

Appreciate any insight. Reading the below link seems to indicate this is possible just not seeing how everything should be setup and configured at the project level.
https://www.eclemma.org/jacoco/trunk/doc/maven.html

Marc Hoffmann

unread,
Mar 12, 2019, 1:25:25 PM3/12/19
to jac...@googlegroups.com
Hi,

indeed, JaCoCo is fully based on Java class files for analysis. As long as you continue to compile your class files with debug information you will get all the tabular information with different coverage metrics. If source files cannot be found only the pages with highlighted source are not generated and linked to.

Regards,
-marc
> --
> You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/f0f56a84-ec07-4b72-88ab-4c2523d1007c%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

kir...@gmail.com

unread,
Mar 12, 2019, 1:44:57 PM3/12/19
to JaCoCo and EclEmma Users

Thanks Marc.

Appreciate the detail. If i'm hearing you correctly the only thing I lose if the source code under /src/main/java is not available is source code highlighting.

Can you tell me is it sufficient that I only have the built files under /target to accomplish the rest of reporting or does my project structure need to be different. Any detail you can give or docs you can point me to would be great!

Evgeny Mandrikov

unread,
Mar 12, 2019, 1:46:14 PM3/12/19
to JaCoCo and EclEmma Users
"report" goal of jacoco-maven-plugin stricly requires only exec file ( https://www.jacoco.org/jacoco/trunk/doc/report-mojo.html#dataFile ) and class files ( "project.build.outputDirectory" which is typically "target/classes" ) and doesn't strictly require presence of source files

As a proof consider following example

given src/main/java/Example.java

class Example {
}

given src/test/java/ExampleTest.java

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

and given pom.xml

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

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.3</version>
      </plugin>
    </plugins>
  </build>
</project>

Execution of

mvn jacoco:prepare-agent test

without creation of report, will compile classes, execute tests and create exec-file

target/jacoco.exec

After removal of sources

rm -rf src

Execution of

mvn jacoco:report

will generate following report even in absence of source files

report.png


If maven plugin is not flexible enough for your use case, please consider usage of
JaCoCo Ant Tasks ( https://www.jacoco.org/jacoco/trunk/doc/ant.html ) , e.g. via maven-antrun-plugin
or JaCoCo Command Line Interface ( https://www.jacoco.org/jacoco/trunk/doc/cli.html )


Regards,
Evgeny

Marc Hoffmann

unread,
Mar 12, 2019, 1:49:46 PM3/12/19
to jac...@googlegroups.com
Sure, you only need is

1) class files (by default at target/classes)
2) JaCoCo exec files (by default at target/jacoco.exec)

Please see our documentation: https://www.jacoco.org/jacoco/trunk/doc/report-mojo.html

Regards,
-marc
> To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/2371cfbe-6dd6-44f6-9ab7-f019affbf04b%40googlegroups.com.

kir...@gmail.com

unread,
Mar 12, 2019, 3:09:22 PM3/12/19
to JaCoCo and EclEmma Users

Thank you Evgeny and Marc.

Really appreciate your responses as I start using this tool.

I have one final question. Does "prepare-agent" goal require /src/main/java or does it reuse existing class files in /target to instrument them?

Evgeny Mandrikov

unread,
Mar 12, 2019, 5:18:03 PM3/12/19
to JaCoCo and EclEmma Users


On Tuesday, March 12, 2019 at 8:09:22 PM UTC+1, kir...@gmail.com wrote:

I have one final question.  Does "prepare-agent" goal require /src/main/java or does it reuse existing class files in /target to instrument them?


Again please read documentation - https://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html , which states

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

No more, no less, i.e. nor source files, nor class files are involved during execution of "prepare-agent".
When JVM uses this agent ( https://www.jacoco.org/jacoco/trunk/doc/agent.html ) classes files loaded during execution of this JVM will be instrumented on-the-fly no matter from where they come.

kir...@gmail.com

unread,
Mar 12, 2019, 5:55:25 PM3/12/19
to JaCoCo and EclEmma Users
Thank you Evgeny.

That was my suspicion, but again i'm just starting to use this tool and wanted to make absolutely sure. Appreciate all of the help.
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages