2

I am trying to get the code coverage with jenkins and jacoco plugins.

I have a jacoco agent jar on the machine where my testing is being executed. I then retrieve the dump and try to get the code coverage on jenkins.

However I keep getting the below error,

[JaCoCo plugin] Collecting JaCoCo coverage data...
[JaCoCo plugin] \**/coverage/jacoco.exec;\**/coverage/classes-cov;\**/application/; locations are configured
[JaCoCo plugin] Number of found exec files for pattern \**/coverage/jacoco.exec: 1
[JaCoCo plugin] Saving matched execfiles:  /home/ec2-user/slave/workspace/Automation_Code_Coverage_POMS/coverage/jacoco.exec
[JaCoCo plugin] Saving matched class directories for class-pattern: \**/coverage/classes-cov:  /home/ec2-user/slave/workspace/Automation_Code_Coverage_POMS/coverage/classes-cov
[JaCoCo plugin] Saving matched source directories for source-pattern: \**/application/: 
[JaCoCo plugin] Loading inclusions files..
[JaCoCo plugin] inclusions: [\**/com/test/poms/\**]
[JaCoCo plugin] exclusions: [\**/poms/convertors/\**:\**/poms/scheduler/\**]
ERROR: Publisher 'Record JaCoCo coverage report' aborted due to exception: 
java.io.IOException: Error while analyzing class /home/ec2-user/.jenkins/jobs/Automation_Code_Coverage_POMS/builds/43/jacoco/classes/com/test/poms/convertors/DtoToSroConverter.83f57acb46d004b5.class.
    at org.jacoco.core.analysis.Analyzer.analyzerError(Analyzer.java:150)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:144)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:175)
    at org.jacoco.core.analysis.Analyzer.analyzeAll(Analyzer.java:208)
    at hudson.plugins.jacoco.ExecutionFileLoader.analyzeStructure(ExecutionFileLoader.java:126)
    at hudson.plugins.jacoco.ExecutionFileLoader.loadBundleCoverage(ExecutionFileLoader.java:133)
    at hudson.plugins.jacoco.JacocoReportDir.parse(JacocoReportDir.java:102)
    at hudson.plugins.jacoco.JacocoBuildAction.loadRatios(JacocoBuildAction.java:291)
    at hudson.plugins.jacoco.JacocoBuildAction.load(JacocoBuildAction.java:273)
    at hudson.plugins.jacoco.JacocoPublisher.perform(JacocoPublisher.java:371)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
    at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:726)
    at hudson.model.Build$BuildExecution.post2(Build.java:185)
    at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:671)
    at hudson.model.Run.execute(Run.java:1769)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:374)
    Caused by: java.lang.IllegalStateException: Can't add different class with same name: com/test/poms/convertors/DtoToSroConverter
    at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:106)
    at org.jacoco.core.analysis.Analyzer$1.visitEnd(Analyzer.java:92)
    at org.objectweb.asm.ClassVisitor.visitEnd(ClassVisitor.java:317)
    at org.jacoco.core.internal.flow.ClassProbesAdapter.visitEnd(ClassProbesAdapter.java:98)
    at org.objectweb.asm.ClassReader.accept(ClassReader.java:697)
    at org.objectweb.asm.ClassReader.accept(ClassReader.java:506)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:107)
    at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:142)
    ... 17 more
Notifying upstream projects of job completion

JaCoCo Can't add different class with same name: org/hamcrest/BaseDescription The above link suggest to exclude the files, but if you look at the above logs, they are already being excluded but I still see this issue.

Community
  • 1
  • 1
Rajeev Agile
  • 46
  • 1
  • 4

1 Answers1

1

In my case see this: 12:35:12 [JaCoCo plugin] exclusions: [**/*koba*.class] in Jenkins logs while Jenkins Jacoco plugin performs the analysis. There's no backslashes as compared to what you get.

Secondly, the error that you are getting is due to either of the 2 reasons:

  1. You have .java/.groovy files and after compilation, you create .class files. It seems like there's class file (for ex: abc.java or com/test/poms/convertors/DtoToSroConverter in your case) which is present in the source folder that you have mentioned in the plugin's "source" field.

  2. If you are creating any class files (for which you don't have a .java/.groovy file in source (src/main/java or src/main/groovy or src/test/java, src/test/groovy, src/xxx/java or src/xxx/groovy folders) then, jacoco analysis will error out with the same error i.e. it won't be able to find the respective source file (.java/.groovy) for the .class file it's analyzing.

Check, how many files are there with name starting with: DtoToSroConverter in your project.

Then, make sure the values that you mention in Jacoco plugin in Jenkins looks like this. NOTE: source code should NOT contains any test (unit/non-Unit test source) folders.

enter image description here

In my case, I'm saying, process all .exec files (anywhere in my project's workspace after the build/tests/jacoco process is complete) i.e. ****/*.exec**

Then, Path to class directories should always mention only the MAIN source class files (not test classes of either unit/non-unit types) i.e. I only used "build/classes/main" as these classes are generated against my source main code (src/main/java OR src/java). This value is the folder which contains your main source code classes only.

Path to source directories field should always contain the folder where the actual main source code exists (instead of including any test source code) i.e. I have used "src/java". I could have used "src/main/java" which is the Gradle/Maven standard folder structure for main source code. In my case, my main source code is in src/java folder.

Check if Path to source directories field and Path to class directories is set correctly? If yes, is there more than one file with the name: DtoToSroConverter

AKS
  • 16,482
  • 43
  • 166
  • 258
  • Now I do not see that error I have mentioned but I do not see any lines getting covered during execution. I am sure I am running proper tests. I see that jacoco.exec is 0 in size – Rajeev Agile Aug 10 '15 at 09:42
  • that's because you may not be attaching the jacocoagent.jar to the target JVM (Tomcat's JVM) while running your non-Unit tests. Are you running Unit tests OR non-Unit tests (i.e. Integration Tests, Acceptance, Selenium browser based etc tests). This is what I do. See couple of my posts on Jacoco OR see jacoco docs on how to attach jacocoagent.jar (destfile=....) and then set a variable TOMCAT_EXTRA_OPTS=...to that value. Now use this variable while starting Tomcat. Start tomcat, run your tests, stop tomcat (important). At this time, you'll have valid jacocoIT.exec file. – AKS Aug 10 '15 at 14:45
  • this should be marked as answer. The part " Path to class directories should always mention only the MAIN source class files" was very important – ventura8 Jul 29 '16 at 08:35
  • Can I put jar files in the class directories ? – Mohit Arora Aug 03 '16 at 10:25
  • @MohitArora which jar files? You should not. Which .jar file are you trying to put in classes folder? You don't need to add jacocoagent.jar in the compiled classes folder. You can find jacocoagent.jar in your workspace when jacoco tasks runs (Gradle) or place it anywhere in a folder and specify it's path/filename.jar while setting jacoco options for generating coverage data in .exec file. In Jenkins jacoco plugin, you just specify .exec file(s) like shown in the snapshot, folder path for class files and main source code, that's it. – AKS Aug 04 '16 at 14:00
  • Also note that coverage data shown in Jenkins dashboard using Jacoco plugin (of Jenkins) and what you see (using jacocoTestReport Gradle task's generated index.html) and what you #s (X% covered out of Y or N lines out of M) are different. For ex: one shows 40 lines missed out of 100 and other will show 60 lines covered out of 100. – AKS Aug 04 '16 at 14:02
  • @ArunSangal I have the .exec files and have generated report through ant using jar file. The jar file contains all the classes. So I was asking if i can put my jar file which has all the classes instead of the folder path. – Mohit Arora Aug 05 '16 at 08:46
  • classpath should contains files with .class extension. I haven;t tried putting .jar there. What would happen is when Jacoco extension will try to map the class files with the corresponding src files (.java/.groovy) then it'll easily find those but if those .class files are inside .jar and you just put .jar or some .class and .jar (containing other .class files) then, Jacoco plugin may not support the coverage correctly. – AKS Aug 05 '16 at 10:56