Cobertura with Maven - fail if coverage below threshold, but still generate site - maven

I'm using Cobertura with Maven.
I'd like the build to fail if the coverage is below a given threshold, but I would like the site (including the Cobertura report) to still be generated. This is because developers will need to refer to the coverage report to see where they can add more coverage to fix the failed build.
Currently my pom looks like:
<project>
<build>
...
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura.version}</version>
<configuration>
<check>
<totalLineRate>${cobertura.check.totalLineRate}</totalLineRate>
</check>
</configuration>
<executions>
<execution>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura.version}</version>
</plugin>
...
</plugins>
</reporting>
</project>
If I run mvn clean verify site, then it generates the HTML coverage report if the coverage target is met, but it fails the build without generating the report if the coverage target is not met. How can I change it to always generate the report?

Instead of failing the build if the code coverage target isn't met, is it possible to set it to mark the build as unstable instead? I know there are ways to do this through the Jenkins CI server, but I'm not sure if it's possible to accomplish this through pom.xml. Then again, "unstable" builds might be more Jenkins-specific, and might not exist as a possibility only through your pom file.

A quick workaround: remove the check goal:
<executions>
<execution>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
then run
mvn clean verify site cobertura:check
If you are using Hudson/Jenkins remove all checks from the pom.xml and install the Cobertura plugin to Hudson and configure the checks in the Hudson/Jenkins plugin.

Related

Not able to generate HTML reports of code coverage from Clover Maven plugin

Trying to integrate Clover maven plugin to get the code coverage of my project.
After I build my project with mvn clean install, target folder looks like below
But I am looking for code coverage with HTML reports (Which will give us the code coverage with package wise, class wise views)
Will the line <generateHtml>true</generateHtml> does it ?, if yes, something wrong in my configuration.
How can I get HTML reports out of Clover maven plugin?
Below is the pom.xml code
<build>
<plugins>
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-clover2-plugin</artifactId>
<configuration>
<!-- <targetPercentage>80%</targetPercentage> -->
<generateHtml>true</generateHtml>
<generatePdf>true</generatePdf>
<generateXml>true</generateXml>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>instrument</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-clover2-plugin</artifactId>
<configuration>
<generateHtml>true</generateHtml>
<generatePdf>true</generatePdf>
<generateXml>true</generateXml>
</configuration>
</plugin>
</plugins>
</reporting>
Please use mvn clean install clover2:clover command ad see if you get the html report generated in target/site/clover/index.html file.
The clover2:clover goal should instrument your test classes, execute the test cases and record the code coverage information.

Maven checkstyle violation count

How can I get Checkstyle to print out the total number of found violations when running the maven task? For example right now I just total up the number of violations found after each run.
For clarification, I simply want this at the end of my maven task:
Checkstyle found XXX violations
Checkstyle configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<failOnViolation>true</failOnViolation>
<configLocation>/path</configLocation>
<maxAllowedViolations>100</maxAllowedViolations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Build the project with “mvn checkstyle:checkstyle” command outside the Eclipse. and open the generated checkstyle.html in /Your_Project/target/site folder. You can see the complete report.
There is an alternative way of doing this same thing that would return you the actual number. To do this all you have to do is change the number of allowed violations to 0 and run mvn clean install this should run your checkstyle plugin and eventually fail. It will then display the number of violations compared to the number of allowed violations.

Maven Jacoco Configuration for multi-module projects [duplicate]

This question already has answers here:
How to configure multi-module Maven + Sonar + JaCoCo to give merged coverage report?
(13 answers)
Closed 6 years ago.
I was trying to generate code coverage reports using jacoco plugin in maven for a multi module project that I was working on.
I added the following in my parent pom.xml within the build tags.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8-SNAPSHOT</version>
<configuration>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>${argLine}</argLine>
</configuration>
</plugin>
On running mvn verify, the respective jacoco reports were generated for each module at "project-root\module\target\site\jacoco\"
Is it possible to generate a consolidated jacoco report at the project-root containing the test coverage details of each module?
Please suggest the best possible way to merge the individual module reports.
Sure is!
Took me a while and a few sources to cook this pattern up, but has worked well.
For a multi-module Maven project:
ROOT
|--LIB-1
|--LIB-2
The LIB projects both have their own unit tests.
ROOT pom.xml
<!- properties-->
<jacoco.reportPath>${project.basedir}/../target/jacoco.exec</jacoco.reportPath>
<!-- build/plugins (not build/pluginManagement/plugins!) -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>agent-for-ut</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${jacoco.reportPath}</destFile>
</configuration>
</execution>
</executions>
</plugin>
LIB projects pom.xml will inherit the the JaCoCo plugins execution, so just need to wire up the argline in the Surefire plugin.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>${argLine}</argLine>
</configuration>
</plugin>
I have an extended answer for rolling up integration tests as well as unit tests for JaCoCo being reported via Sonar, you can see my detailed answer here.
In addition to the steps suggested in markdsievers detailed answer, I had to setup sonarqube-5.3 (supports jdk 7+) in localhost:9000
Setup SonarQube
And use mvn package to generate jacoco.exec files.
Then mvn sonar:sonar to generate report in sonar dashboard.

IT code coverage with sonar

I have the following task at hand:
-- find IT code coverage for a project
Given situation:
-- IT code resides in a repository separate to the actual production code
-- Production code that the tests were created for reside in more than one git repository.
-- all of the above uses maven and are written in Java.
I have tried following different tutorial and blogs but couldnt find a simpler answer.
Can anyone either point me towards the right resource or give me hints for a kick start?
I will try to answer. I will post example with UT (IT is the same thing just not at the same place in the maven livecycle build, and instead of the surefire plugin its the failsafe plugin)
Lets say we use JaCoCo for the code coverage agent.
In my Parent pom, in the profile section (it is a multi module project)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Now when we build our maven project, we add the profile to inject the JaCoCo Agent
clean install -Psonar-coverage
Then we may tell sonar to analyse our project and to use the JaCoCo report with the following command
mvn org.codehaus.mojo:sonar-maven-plugin:2.4:sonar -Dsonar.dynamicAnalysis=reuseReports -Dsonar.java.coveragePlugin=jacoco -Dsonar.forceAnalysis=true -Dsonar.jacoco.reportMissing.force.zero=true -Dsonar.binaries=target/classes -Dsonar.junit.reportsPath=target/surefire-reports -Dsonar.jacoco.reportPath=target/coverage-reports/jacoco-ut.exec -Dsonar.jdbc.driver=com.mysql.jdbc.Driver -Dsonar.jdbc.url=jdbc:<YOUR SONAR INSTALLATION DB> -Dsonar.host.url=<YOUR SONAR INSTALLATION>

maven-site-plugin not generating apidocs folder with javadocs

I inherited a project that is supposed to build javadoc files and place them in the site directory. This is not being done. I have looked at all the examples I can find and I can't figure out where the configuration is broken.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</plugin>
</reportPlugins>
</configuration>
</execution>
</executions>
</plugin>
Any and all help is greatly appreciated.
You have bound maven site plugin's site goal to prepare-package phase. You have configured javadoc generation in this plugin configuration.
As such, if you run maven's default lifecycle goals like mvn package or mvn install you should get site report with javadocs.
If you ran mvn site, it would skip prepare-package phase to which your plugin configuration is bound and hence would not generate javadoc.

Resources