How to configure in ant build to not instrument the class twice

1,366 views
Skip to first unread message

praum...@gmail.com

unread,
Jun 17, 2015, 3:31:08 PM6/17/15
to jac...@googlegroups.com
Hi everyone,

Project = Ant Build
Unit-Test = Mockito, PowerMock, Junit
Code Coverage Enging = Jacoco

Since we jacoco and powermock are incompatible, the only workaround that is mentioned so far is to pre-instrument the classes. Doing, so I get some error message saying error while instrumenting because classes are already instrumented. If I want to get rid of this error message how do I configure it.

Online documentation does not have good example.

This is what is writted on online document and is not clear.

It is possible to also use offline-instrumented classes with the JaCoCo Java agent. In this case the configuration is taken from the agent options. The agent must be configured in a way that pre-instrumented classes are excluded, e.g. with "excludes=*". Otherwise it will result in error messages on the console if the agent instruments such classes again.


It would be really nice if someone could provide the configuration example on how to do this.

Also if possible it would be great to have similar example for maven project as well.

Evgeny Mandrikov

unread,
Jun 17, 2015, 3:45:24 PM6/17/15
to jac...@googlegroups.com, praum...@gmail.com, praum...@gmail.com
Hi,

You can find an example of offline instrumentation with Ant in our documentation - see link "Offline Example" near "Ant Usage Example" at http://www.eclemma.org/jacoco/trunk/doc/ , which points to http://www.eclemma.org/jacoco/trunk/doc/examples/build/build-offline.xml , which is from our sources at https://github.com/jacoco/jacoco/tree/master/org.jacoco.examples/build

Maven example of offline instrumentation also can be found in the same place.

P.S. Please don't open consecutively multiple threads on the same subject.

umesh prajapati

unread,
Jun 17, 2015, 4:04:51 PM6/17/15
to Evgeny Mandrikov, jac...@googlegroups.com
Also I do not see that configuration setting on online example where it defines not to instrument the classes twice if pre instrumentation is done.

On the document it says: "The agent must be configured in a way that pre-instrumented classes are excluded, e.g. with "excludes=*". " But on the example that you have provided I do not see that being configured anywhere

praum...@gmail.com

unread,
Jun 17, 2015, 4:07:18 PM6/17/15
to jac...@googlegroups.com, praum...@gmail.com
Thank you for a quick response. Yes I did have a look at that example and followed the same approach. I do get code coverage for all the unit test that does not involve powermock. But if powermock is involved in the unit test I am not getting the code coverage for them even with offline approach.


The online example which you have provided for Ant build project for offline mode does not have powermock in it. In my case, too if there is no powemock unit test, I do get code coverage but I dont get code coverage for unit test that has powermock.

Evgeny Mandrikov

unread,
Jun 17, 2015, 4:20:45 PM6/17/15
to umesh prajapati, jac...@googlegroups.com
Because in case of offline instrumentation you are not obligated to attach agent to JVM, even if you can as mentioned in doc, which you quote - notice that it uses word "possible". And this is exactly what is done in example.

I'm not using nor PowerMock, nor Ant, so don't have a working example for both at hands right now. While from other threads and messages in internet seems that people can get working combination (e.g. thread https://groups.google.com/d/msg/jacoco/z5-CNSNO-xc/uXTgKx-0SdYJ ).

The best option which I can imagine to help you quickly - you'll publish very simple example (e.g. at GitHub) which currently doesn't work for you, so that I can try/change it locally without need to build one from scratch and avoiding long email threads ( like it was done with thread https://groups.google.com/d/msg/jacoco/kdePtX4dJf8/hL1_j-qoeKsJ ).

Otherwise - be patient and wait when me or someone else will find a time to publish such an example.

--
Best regards,
Evgeny Mandrikov aka Godin <http://godin.net.ru>
http://twitter.com/_godin_

umesh prajapati

unread,
Jun 17, 2015, 4:35:16 PM6/17/15
to Evgeny Mandrikov, jacoco
I agree this approach works for Maven Projects. I do have a Maven Project where we are using jacoco and powermock and this approach works like a charm but the same approach does not work for ant :(. 


I will try to get a simple example for ant project and try to reproduce the issue. 

umesh prajapati

unread,
Jun 18, 2015, 3:45:32 PM6/18/15
to Evgeny Mandrikov, jacoco
Hi Evgeny,

I am not sure why this is happening but for some reason it is creating 2 jacoco.exec file. 

1. In the location that I have mentioned <jacoco:coverage destfile="${gec}/lib/jacoco/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant" >
2. In the location from where I am executing build script.

I generated 2 seprate report and noticed that jacoco.exe which is in the locaiton that I have mentioned have classes that has been modified by powermock and code coverage is only for the unit test without powermock and jacoco.exec in the location from where build script is running does not have any classes that has been modified by jacoco. I can see that this jacoco.exe has code coverage for powermock. So, I just merged these 2 jacoco.exec. Its weird why it would generate 2 jacoco.exec. Anyways the problem that I am facing where powermock code coverage  is not coming is resolved with this workaround. Thank you.

<jacoco:coverage destfile="${gec}/lib/jacoco/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant" >
<junit printsummary="true" failureproperty="junit.failure" fork="true">
<classpath>
<pathelement path="${gec}/lib/jacoco/jacocoagent.jar}" />
<pathelement path="../instrumented-Classes" />
<pathelement path="../gec/Application/lib/mockito/powermock-mockito-1.5-full.jar" />
<pathelement path="../gec/Application/lib/mockito/mockito-all-1.9.5.jar" />
<pathelement path="../gec/Application/lib/mockito/junit-4.5.jar" />
<pathelement path="${gec}/lib/lti-jar-repo/java-json.jar;" />
<pathelement path="${gec}/lib/lti-jar-repo/jackson-all-1.8.5.jar" />
<pathelement path="${gec}/lib/mockito/javassist-3.16.1-GA.jar" />
<pathelement path="${gec}/lib/mockito/commons-configuration-1.6.jar" />
<pathelement path="${gec-thirdparty-jars}" />
<pathelement path="${classes}" />
  </classpath>

<batchtest todir="${test-xml-reports}">
<fileset dir="${classes}" excludes="com/gec/test/util/**,com/gec/test/testlib/**"/>
<formatter type="xml"/>
</batchtest>
</junit>
</jacoco:coverage>

Evgeny Mandrikov

unread,
Jun 22, 2015, 4:13:22 PM6/22/15
to jac...@googlegroups.com, praum...@gmail.com, praum...@gmail.com
I highly suspect that Ant task "jacoco:coverage" adds an agent for online instrumentation, which is not required when you do an offline instrumentation as was said earlier.
And IMO this is a perfect explanation for both of problems: two files and classes instrumented twice - online by agent and offline.
Also notice that our example of offline instrumentation doesn't use task "jacoco:coverage", while example of online instrumentation does.

Best regards,

umesh prajapati

unread,
Jun 22, 2015, 4:36:34 PM6/22/15
to Evgeny Mandrikov, jacoco

Yea that's what I was guessing too. And thank you for your response. 

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages