coverage of main class method from maven

170 views
Skip to first unread message

Cristiano Costantini

unread,
Sep 28, 2015, 4:55:22 PM9/28/15
to JaCoCo and EclEmma Users
Hello all,
is it possible to perform coverage of a static void main(String[] args) and generate an XML or HTML report directly from the maven plugin?

I'm able to launch from Eclipse a generic java application with "Coverage as...", I've also been able to launch from the command line the -javaagent, and I've used the generated jacoco.exec in plugin, and from these two sessions I can generate the XML or HTML report.

But I would like to automate the generation of the coverage report with my maven build:
as far as I've seen, there is only documentation for performing the analysis during junit tests (suerefire plugin) or integration tests (failsafe plugin).

Ideally, I would like launch the command "mvn exec:java ..." and I want to collect the coverage report of this execution.
What am I missing?

Thank you very much!
Cristiano

Marc Hoffmann

unread,
Sep 29, 2015, 7:19:44 AM9/29/15
to jac...@googlegroups.com
Hi Christiano,

JaCoCo's prepare agent goal can set any property with the agent
configuration:

http://eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html

With this property you can configure the command line for java
execution.

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/89fbda2b-f6e5-41b5-ae0d-9b970fffdbe9%40googlegroups.com
> [1].
> For more options, visit https://groups.google.com/d/optout [2].
>
>
> Links:
> ------
> [1]
> https://groups.google.com/d/msgid/jacoco/89fbda2b-f6e5-41b5-ae0d-9b970fffdbe9%40googlegroups.com?utm_medium=email&utm_source=footer
> [2] https://groups.google.com/d/optout

Cristiano Costantini

unread,
Sep 29, 2015, 5:36:11 PM9/29/15
to JaCoCo and EclEmma Users
Yeah!
it works! Thank you!

I anyway now have another problem: I want to use JaCoCo to find dead code, which I search by launching the full application in a simulated runtime environment (the Junit tests are testing also the dead code !!!)

But the application code is in a <dependency> and in this way I get only the report for the classes in src/main/java (also src/test/java are excluded from the report). How can I generate the report of coverage for dependencies?

Thanks!
Cristiano

PS. for further reference, I've configured my plugins in the pom in this way:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>${argLine}</argument>
<argument>-classpath</argument>
<classpath/>
<argument>net.cristcost.TestApplication</argument>
</arguments>
<classpathScope>test</classpathScope>
</configuration>
</plugin>

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
</plugin>

and I run and generate the report with this command:

mvn jacoco:prepare-agent exec:exec jacoco:report



PPS: it is important to use the exec:exec goal and not the exec:java (this last goal runs in the same VM of the mvn process so you don't get any chance to pas to the JVM the -javaagent argument!

Cristiano Costantini

unread,
Sep 30, 2015, 2:00:17 AM9/30/15
to JaCoCo and EclEmma Users
...moreover, I note that the "jacoco.exec" produced includes coverage information for the external dependencies as if I import it into Eclipse plugin, I'm able to select projects and external Jars referenced by workspace projects, and generate report for them.

I've searched extensively on the network but I've not found any way to configure the jacoco:report goal ! 
Do you have any suggestion?

Thanks!

PS. Another note: version 0.7.5 of the plugin produce a jacoco.exec file that fails to be loaded into my version of the Eclipse plugin, someone have issue like this can try to use a previous version:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<!-- <version>0.7.5.201505241946</version> -->
<version>0.7.2.201409121644</version>
</plugin> 

Marc Hoffmann

unread,
Sep 30, 2015, 4:48:14 AM9/30/15
to jac...@googlegroups.com
Hi Cristiano,

the JaCoCo Maven plug-in currently supports coverage reports on a per
module basis only. A possible work-around is to use the Ant report task
which allow specifying any number of class/source file locations.

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/fc063078-fbb8-4046-9999-aff5ed25a4e5%40googlegroups.com
> [1].
> For more options, visit https://groups.google.com/d/optout [2].
>
>
> Links:
> ------
> [1]
> https://groups.google.com/d/msgid/jacoco/fc063078-fbb8-4046-9999-aff5ed25a4e5%40googlegroups.com?utm_medium=email&utm_source=footer
> [2] https://groups.google.com/d/optout

Cristiano Costantini

unread,
Sep 30, 2015, 5:19:24 AM9/30/15
to JaCoCo and EclEmma Users
Hi Marc!

...I can't automate with Ant, well, I can but I would increase a lot the complexity by introduce a new tool in the stack!

I've checked-out the source code and compiled by myself the 0.7.6-SNAPSHOT: I'm playing around on the plugin code and check if it is possible to extend the goal, but I lack significant background information on the project...

I could spend a little time to try and test if I can modify the plugin to customize the report, and if have success I can propose you patches.
Would you mind to help me a little bit?

I think I have detected that the code handling report generation is in AbstractReportMojo.java ( https://github.com/jacoco/jacoco/blob/e24c99cfd568e869e53a869e99c1bc352fca4ac5/jacoco-maven-plugin/src/org/jacoco/maven/AbstractReportMojo.java ), more I suspect it is in createReport and createVisitor methods.

Could you give me some suggestion, if you think it is possible, on how to modify these two methods in order to specify to the report to include additional classes/packages/dependencies?
Could you suggest me where to look for into the source code of the Ant Report Task to see how in Ant the ant task specify any number of classes/source file locations?

Any information or hint is precious, then I'll try to figure out by myself !

Thank you,
Cristiano


PS. Below, the code that I think handle the report generation from AbstractReportMojo.java:

void createReport(final IReportGroupVisitor visitor) throws IOException {
final FileFilter fileFilter = new FileFilter(this.getIncludes(),
this.getExcludes());
final BundleCreator creator = new BundleCreator(this.getProject(),
fileFilter, getLog());
final IBundleCoverage bundle = creator.createBundle(executionDataStore);
final SourceFileCollection locator = new SourceFileCollection(
getCompileSourceRoots(), sourceEncoding);
checkForMissingDebugInformation(bundle);
visitor.visitBundle(bundle, locator);
}

IReportVisitor createVisitor(final Locale locale) throws IOException {
final List<IReportVisitor> visitors = new ArrayList<IReportVisitor>();
getOutputDirectoryFile().mkdirs();
final XMLFormatter xmlFormatter = new XMLFormatter();
xmlFormatter.setOutputEncoding(outputEncoding);
visitors.add(xmlFormatter.createVisitor(new FileOutputStream(new File(
getOutputDirectoryFile(), "jacoco.xml"))));
final CSVFormatter csvFormatter = new CSVFormatter();
csvFormatter.setOutputEncoding(outputEncoding);
visitors.add(csvFormatter.createVisitor(new FileOutputStream(new File(
getOutputDirectoryFile(), "jacoco.csv"))));
final HTMLFormatter htmlFormatter = new HTMLFormatter();
htmlFormatter.setOutputEncoding(outputEncoding);
htmlFormatter.setLocale(locale);
visitors.add(htmlFormatter.createVisitor(new FileMultiReportOutput(
getOutputDirectoryFile())));
return new MultiReportVisitor(visitors);
}




Cristiano Costantini

unread,
Oct 1, 2015, 9:26:09 AM10/1/15
to JaCoCo and EclEmma Users
Hi Marc,

I'm trying to implement options for maven plugin report generation => https://github.com/cristcost/jacoco
I really hope you'll consider these developments for inclusion into the main project.

If you have any recommendation on code style, quality and approach, I'll listen to you carefully! 

In the meanwhile I'm trying to reproduce that the Ant Task does. I'm not writing any integration test as I'm testing it directly on my application.

The idea is to configure it something like this way:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6-SNAPSHOT</version>
<configuration>

<csvReport>true</csvReport>
<xmlReport>false</xmlReport>
<htmlReport>true</htmlReport>
<includeTests>true</includeTests>
<artifacts>
<artifact>net.cristcost.test:module-1</artifact>
<artifact>net.cristcost.test:module-1</artifact>
</artifacts>
</configuration>
</plugin>


Thank you,
Cristiano

P.S. Btw congratulations, I would be a happy man if my team mates would write code 1/10th as clean as JaCoCo is written ! 
 

Marc Hoffmann

unread,
Oct 2, 2015, 4:03:36 AM10/2/15
to jac...@googlegroups.com
Hi Christiano,

we're happy to accept contributions! For successful review I recommend:

* Check out existing list of enhancements:
https://github.com/jacoco/jacoco/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement

For many enhancements possible solutions have already been discussed

* Use our dev setup:
http://eclemma.org/jacoco/trunk/doc/environment.html

All formatting and compiler warnings are configured for Eclipse. Also
make sure your contributions comes with tests, we have unit and
integration tests.

* Create small, independent pull requests at GitHub

Do not mix several aspects in a single PR. If we do not agree on one
aspect the whole PR will fail to get accepted.

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/60521a70-07a1-415e-8d3f-45ddb652e76a%40googlegroups.com
> [1].
> For more options, visit https://groups.google.com/d/optout [2].
>
>
> Links:
> ------
> [1]
> https://groups.google.com/d/msgid/jacoco/60521a70-07a1-415e-8d3f-45ddb652e76a%40googlegroups.com?utm_medium=email&utm_source=footer
> [2] https://groups.google.com/d/optout

Cristiano Costantini

unread,
Oct 2, 2015, 5:15:26 AM10/2/15
to JaCoCo and EclEmma Users
Hi Marc,
I've already worked on that and just proposed a Pull Request, before reading your reply with suggestions!

So, as I've already proposed the Pull Request at https://github.com/jacoco/jacoco/pull/349 , please forgive me if it does not respect some rule or advice you may have given.

I'll check your recommendations this evening, and if I find any mistake I've made, I'll start another pull request. 

If you check my pull request before I check your recommendations, please give me some direct feedback, it is really appreciated!

Thanks,
Cristiano

Cristiano Costantini

unread,
Oct 15, 2015, 9:48:04 AM10/15/15
to JaCoCo and EclEmma Users
Hello all,

I would like to kindly ask you if the https://github.com/jacoco/jacoco/pull/349 has not been considered up to now because of time, or because it has some issues.

If it simply a question of time, please don't mind this message :-)

If the other case I would like to know if there is something more I can do to make it suitable for being integrated.

Thanks!
Cristiano

P.S: When do is it planned the next release?

Marc Hoffmann

unread,
Oct 19, 2015, 7:29:19 AM10/19/15
to jac...@googlegroups.com
Hi Cristiano,

because of time and because of basic issues. Please see my previous mail
about consumable PRs. I'll comment on the PR request.

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/34e08870-8333-4a21-9f6a-7c1bd068ea9f%40googlegroups.com
> [1].
> For more options, visit https://groups.google.com/d/optout [2].
>
>
> Links:
> ------
> [1]
> https://groups.google.com/d/msgid/jacoco/34e08870-8333-4a21-9f6a-7c1bd068ea9f%40googlegroups.com?utm_medium=email&utm_source=footer
> [2] https://groups.google.com/d/optout

Cristiano Costantini

unread,
Oct 20, 2015, 1:52:15 AM10/20/15
to JaCoCo and EclEmma Users
Hello and thanks Marc,

I've seen the feedback on the PR, I'll manage to make it more consumable :-)
I'll split the PR in two, but I'll first submit the one for external dependency coverage and later the one for format options.

I'm just wondering how to write valuable tests for it (never developed a maven plugin professionally) so if you have time, I really would appreciate some suggestion (i.e. indication of some existing test to take inspiration from!) 

Finally, do you have any plan for next release date (how much time I have to fix the PR and have it included into the next release)?

Thanks and have a nice day.
Cristiano

Marc Hoffmann

unread,
Oct 22, 2015, 4:48:40 AM10/22/15
to jac...@googlegroups.com
Hi Cristiano,

there is no next release date, but we try to release on a continuous
base whenever a reasonable set of new features or urgent bug fixes have
been done.

There are already a few integration tests for the Maven goals:
https://github.com/jacoco/jacoco/tree/master/jacoco-maven-plugin.test/it

Regards,
-marc
>>> https://github.com/jacoco/jacoco/pull/349 [1] has not been
>> considered up
>>> to now because of time, or because it has some issues.
>>>
>>> If it simply a question of time, please don't mind this message
>> :-)
>>>
>>> If the other case I would like to know if there is something more
>> I
>>> can do to make it suitable for being integrated.
>>>
>>> Thanks!
>>> Cristiano
>>>
>>> P.S: When do is it planned the next release?
>>>
>>> --
>>> 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/34e08870-8333-4a21-9f6a-7c1bd068ea9f%40googlegroups.com
>> [2]
>>> [1].
>>> For more options, visit https://groups.google.com/d/optout [3]
>> [4]
>>> [2] https://groups.google.com/d/optout [3]
>
> --
> 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/4f541758-ae81-46ba-9f63-c53ab186228b%40googlegroups.com
> [5].
> For more options, visit https://groups.google.com/d/optout [3].
>
>
> Links:
> ------
> [1] https://github.com/jacoco/jacoco/pull/349
> [2]
> https://groups.google.com/d/msgid/jacoco/34e08870-8333-4a21-9f6a-7c1bd068ea9f%40googlegroups.com
> [3] https://groups.google.com/d/optout
> [4]
> https://groups.google.com/d/msgid/jacoco/34e08870-8333-4a21-9f6a-7c1bd068ea9f%40googlegroups.com?utm_medium=email&amp;utm_source=footer
> [5]
> https://groups.google.com/d/msgid/jacoco/4f541758-ae81-46ba-9f63-c53ab186228b%40googlegroups.com?utm_medium=email&utm_source=footer

Cristiano Costantini

unread,
Oct 23, 2015, 2:07:30 AM10/23/15
to jac...@googlegroups.com
Thank you Marc!
I'll work on the PR after the 15 of November then, so I'll have more time to accurately study and implement some integration test.

Regards!
Cristiano



You received this message because you are subscribed to a topic in the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jacoco/LHBbmUBqRfs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/e16dfaf1997ec293127520587630598a%40mountainminds.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages