When I run sonar my code coverage always coming 0. I am using clover. Below entry in pom.xml file
<properties>
<java.version>11</java.version>
<lombok.release>1.18.20</lombok.release>
<spring-cloud.version>Hoxton.SR11</spring-cloud.version>
<vigilphoenix-transformer-core.version>1.3-SNAPSHOT</vigilphoenix-transformer-core.version>
<clover.version>4.2.0</clover.version>
<sonar.java.coveragePlugin>clover</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.clover.reportPath>target\site\clover\clover.xml</sonar.clover.reportPath>
</properties>
<plugin>
<groupId>org.openclover</groupId>
<artifactId>clover-maven-plugin</artifactId>
<version>${clover.version}</version>
<configuration>
<includesTestSourceRoots>false</includesTestSourceRoots>
<targetPercentage>0%</targetPercentage>
<debug>true</debug>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>setup</goal>
</goals>
</execution>
<execution>
<id>aggregate</id>
<phase>test</phase>
<goals>
<goal>clover</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.openclover</groupId>
<artifactId>clover-maven-plugin</artifactId>
<configuration>
<!--cloverDatabase>${project.build.directory}/target/clover/cloverMerge.db</cloverDatabase-->
<generateHtml>false</generateHtml>
<generatePdf>false</generatePdf>
<generateXml>true</generateXml>
<generateHistorical>false</generateHistorical>
</configuration>
</plugin>
When i run "mvn clean install" my clover.xml generate in "\target\site\clover\clover.xml"
Not sure what I have to do. if any one can help would be more help full.
You either need to configure the clover plugin which by default take that path or in your call for sonar from maven you can add it manually:
mvn sonar:sonar -Dsonar.clover.reportPath=\target\site\clover\clover.xml
Related
I am trying to get the coverage of a few test cases related to an open source project of which I got the jar file.
I'm using maven as automatic build tool and the jacoco plugin for maven to get the coverage of the tests on the project.
What I would like to achieve is the same output that I can get with the command:
-jar jacococli.jar report jacoco.exec \
--classfiles <project.jar> \
--sourcefiles <myTestsDirectory> \
--html <jacocoReportsDirectory> \
--xml <jacocoReportsDirectory>coverageFile.xml \
--csv <jacocoReportsDirectory>coverageFile.csv
When I run this from the commandline I can retrieve the report for the coverage computed on the whole project (the jar).
I don't know how to insert this command (or something that can get me the same output) in the pom. I tried to put it in the surefire plugin argLine (with the -javaagent command) but this got me nowhere.
This is where I got so far:
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/coverage-reports/jacoco.exec</dataFile>
<outputDirectory>target/coverage-reports/jacoco-reports</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<argLine>
-javaagent:lib/jacocoagent.jar=destfile=target/coverage-reports/jacoco.exec
</argLine>
<reportsDirectory>target/coverage-reports/surefire-reports</reportsDirectory>
</configuration>
</plugin>
</plugins>
</build>
Giving the whole src directory as the sourceDirectory gets my test cases as the target of the coverage computation and I don't want that.
Note: there are no java classes in src to which the tests are targeted, so there's no need to put that as sourceDirectory. I assume this has to point to the jar somehow.
I'm using jacococ 0.8.7 version, maven 3.3.9 version, jDK 1.8 while I was running from Jenkins I'm facing below exception. what could be the issue? it worked when I used jacoco 0.7.4
command using in Jenkins:
-Dmaven.test.failure.ignore=true -U clean org.jacoco:jacoco-maven-plugin:0.8.7:prepare-agent org.jacoco:jacoco-maven-plugin:0.8.7:report test
Error
Execution default-cli of goal org.jacoco:jacoco-maven-plugin:0.8.7:prepare-agent failed: A required class was missing while executing org.jacoco:jacoco-maven-plugin:0.8.7:prepare-agent: org/jacoco/core/runtime/AgentOptions
pom.xml
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
I'm using xsdtojava with cxf maven` plugin:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
...
</configuration>
<goals>
<goal>xsdtojava</goal>
</goals>
</execution>
</executions>
</plugin>
Question: I want to prevent executing this plugin on every mvn package. Instead, I only want to trigger the source generation manually. But how? I tried setting a custom phase or goal like <phase>generate-sources-now</phase> or <goal>generate-sources-now</goal>, but did does not work.
Though I'd prefer a maven goal approach, the profile approach works as suggested:
<profiles>
<profile>
<id>xsdtojava</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xsdtojava</goal>
</goals>
</execution>
</executions>
<configuration>
.....
Run with: mvn package -P xsdtojava
Plugins don't need to have executions.
Just configure you plugin as
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<configuration>
...
</configuration>
</plugin>
and call the goal in the usual : notation (cxf-xjc-plugin:goal).
I am able to integrate JaCoCo plugin and I can see the coverage report in Sonar 5.3 when I run
mvn sonar:sonar
but whereas when I run it from Jenkins it uploads the metrics to Sonar without the coverage report and displays this message
This component does not have coverage details.
Any ideas how to resolve this issue? Below is a snippet of my Sonar properties in pom.xml:
<properties>
<java.version>1.8</java.version>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.junit.reportsPath>${project.basedir}/target/surefire-reports</sonar.junit.reportsPath>
<sonar.jacoco.reportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPath>
</properties>
`
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.2.201409121644</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<phase>process-resources</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
In Eclipse, I have used EcLEmma to see the unit test code coverage. Which worked fine.
Therefore I have tried to use the JaCoCo plugin for Maven to see the same report with Surefire from Maven build, or even better, with a certain profile, or in the site cycle. Without success. All suggested solutions here didn't work for me.
What is the best way to get a unit test code coverage report (with surefire)?
[Edit]
to be more specific why jacoco failed for me.... as I got always the
Skipping JaCoCo execution due to missing execution data
from the pom
in the properties
<jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>
in the Build section
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>${jacoco.version}</versionRange>
<executions>
<!-- Prepares the property pointing to the JaCoCo runtime agent
which is passed as VM argument when Maven the Surefire plugin is executed. -->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution
data. -->
<destFile>${jacoco.ut.execution.data.file}</destFile>
<!-- Sets the name of the property containing the settings for
JaCoCo runtime agent. -->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!-- Ensures that the code coverage report for unit tests is created
after unit tests have been run. -->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution
data. -->
<dataFile>${jacoco.ut.execution.data.file}</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
<!-- Prepares the property pointing to the JaCoCo runtime agent
which is passed as VM argument when Maven the Failsafe plugin is executed. -->
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution
data. -->
<destFile>${jacoco.it.execution.data.file}</destFile>
<!-- Sets the name of the property containing the settings for
JaCoCo runtime agent. -->
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<!-- Ensures that the code coverage report for integration tests
after integration tests have been run. -->
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution
data. -->
<dataFile>${jacoco.it.execution.data.file}</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</pluginExecutionFilter>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
was my last try but the pom becomes bigger and bigger without any result
which failes with
configuring report plugin org.apache.maven.plugins:maven-jxr-plugin:2.3
configuring report plugin org.jacoco:jacoco-maven-plugin:0.7.5.201505241946
Skipping JaCoCo execution due to missing execution data file:......\target\jacoco.exec
Skipping JaCoCo execution due to missing execution data file:......\target\jacoco-it.exec
.... => long project path
Thanks user3732793
Personnaly, I only needed to add this to my pom.xml
<project>
...
<dependencies>
...
</dependencies>
<build>
<plugins>
...
<!-- Code Coverage report generation -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>generate-code-coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Then I am running
mvn test
and I get the HTML report under ./target/site/jacoco/*.html
As always the solution is easy after reading the documentation which provides example poms jacoco documentation.
This profile:
<profile>
<id>test</id>
<properties>
<env>test</env>
<gebEnv>test</gebEnv>
<jacoco.skip>false</jacoco.skip>
<maven.test.skip>false</maven.test.skip>
<skip.unit.tests>false</skip.unit.tests>
</properties>
</profile>
This in the build section:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
And this in the reporting section:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
</plugin>
Than this does all:
mvn clean install site -P test