Jacoco coverage report in XML format using ant

Hi community, I’m using SonarQube 8.9 now and this property is not supported anymore sonar.jacoco.reportPath so I was looking for some guides how to use JaCoCo with sonar in 8.9 version and found next topic:
[Coverage & Test Data] Importing JaCoCo coverage report in XML format
In this topic said: " By default the generated report will be saved under target/site/jacoco/jacoco.xml ; this location will be checked automatically by the sonar-jacoco plugin so no further configuration is required." And there are examples of setuping sonar-jacoco plugin for maven and gradle. I’ve downloaded sonar-jacoco-plugin-2.3.jar from the repository link which attached to the above topic, but where should I place this jar? I’ve also tried this scenario : https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-ant/ant-coverage (added all needed jars, add all information from build.xml file which was related to jacoco report to my xml file) but it didn’t work for me, build was failed with next exception:
BUILD FAILED

[2022-04-07T14:02:04.698Z] /home/jenkins/hybris/bin/platform/build.xml:72: The following error occurred while executing this line:

[2022-04-07T14:02:04.698Z] /home/jenkins/hybris/bin/platform/resources/ant/sonar.xml:104: Unable to read execution data file /home/jenkins/hybris/bin/platform/target/jacoco.exec
Caused by: java.nio.file.NoSuchFileException: /home/jenkins/hybris/bin/platform/target/jacoco.exec

My configuration in xml:

<property name="src.dir" location="./src/main/java" />
	<property name="result.dir" location="./target" />
	<property name="result.classes.dir" location="${result.dir}/classes" />
	<property name="result.report.dir" location="${result.dir}/site/jacoco" />
	<property name="result.exec.file" location="${result.dir}/jacoco.exec" />
<property name="sonar.coverage.jacoco.xmlReportPaths" value="${result.report.dir}/report.xml" />

<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
		<!-- Update the following line with the location to the jacocoant.jar -->
		<classpath path="path/to/jacoco-0.8.7/lib/jacocoant.jar" />
	</taskdef>

<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
			<!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
			<classpath path="path/to/sonar/ant/task/lib/sonarqube-ant-task-*.jar" />
		</taskdef>

<target name="sonar">
<jacoco:report>

			<!-- This task needs the collected execution data and ... -->
			<executiondata>
				<file file="${result.exec.file}" />
			</executiondata>

			<!-- the class files and optional source files ... -->
			<structure name="JaCoCo Ant Example">
				<classfiles>
					<fileset dir="${result.classes.dir}" />
				</classfiles>
				<sourcefiles encoding="UTF-8">
					<fileset dir="${src.dir}" />
				</sourcefiles>
			</structure>

			<!-- to produce reports in different formats. -->
			<html destdir="${result.report.dir}" />
			<csv destfile="${result.report.dir}/report.csv" />
			<xml destfile="${result.report.dir}/report.xml" />
		</jacoco:report>
<sonar:sonar />
</target>

Does anybody have any links/guides how do the same for ant (configure sonar-jacoco plugin for ant) so on Jenkins build when sonar performs JaCoCo xml report will be generated and in SonarQube project we will have lines coverage in % ?

Hi,

You may find that some other community member has advice for running JaCoCo from Ant, but this topic is out of scope for this community, so please don’t be surprised if no one has a helpful answer.

That said, you probably need to run JaCoCo before your analysis task, not inside it. I would move it to a new task and make your sonar:sonar task depends on it.

 
HTH,
Ann

Hello @George_Glibishchuk ,

You need to specify these paths.

Did you download Jacoco somewhere? Then should update this with the path to it.

Where is the ant sonarqube-ant-task-X.jar? Enter the path here.

Hi @Joe thanks for your response, I’ve specified paths to this jars, I put the random path just as example, sorry for this miss understanding

Hi @ganncamp thank you for your response, I will try to move jacoco in a separate task and make sonar depends on it

I know that it probably already is not related to sonar, but maybe you could help me with this issue

sonarcheck:

[2022-04-08T15:45:15.763Z] [antlib:org.jacoco.ant] Could not load definitions from resource org/jacoco/ant/antlib.xml. It could not be found.

[2022-04-08T15:45:15.763Z] 

[2022-04-08T15:45:15.763Z] BUILD FAILED

[2022-04-08T15:45:15.763Z] /home/jenkins/hybris-commerce-suite/hybris/bin/platform/build.xml:72: The following error occurred while executing this line:

[2022-04-08T15:45:15.763Z] /home/jenkins/hybris-commerce-suite/hybris/bin/platform/resources/ant/sonar.xml:33: Group name must be supplied

[2022-04-08T15:45:15.763Z] 	at org.jacoco.ant.ReportTask.createReport(ReportTask.java:541)

[2022-04-08T15:45:15.763Z] 	at org.jacoco.ant.ReportTask.execute(ReportTask.java:496)

[2022-04-08T15:45:15.763Z] 	at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)

[2022-04-08T15:45:15.763Z] 	at jdk.internal.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)

[2022-04-08T15:45:15.763Z] 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

jacoco jar in my build xml file

<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath>
            <path path="${platformhome}/resources/ant/sonar/lib/jacocoant.jar"/>
        </classpath>
    </taskdef>

Sorry, nothing on my side.

 
Ann

1 Like

Hello @George_Glibishchuk , please take a look at what causes the error here: https://github.com/jacoco/jacoco/blob/master/org.jacoco.ant.test/src/org/jacoco/ant/ReportTaskTest.xml#L28-L50, which might help you.

1 Like