How to add JaCoCo in maven - java

I have written a unit test case using JUnit now I want to add JaCoCo in my build tool that is moving 3.2.1.I am new to Maven. While adding it, I have to doubt that I want to add it in the dependency or plugin ? There are both are available,such that is following
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.2-SNAPSHOT</version>
</plugin>
<dependency>
<groupId>org.codehaus.sonar.plugins</groupId>
<artifactId>sonar-jacoco-plugin</artifactId>
<version>3.2.1</version>
</dependency>
I desire to append it in the dependency is it enough for the plugin?
Please any body clarify it

JaCoCo Java Code Coverage Library
JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries for many years. Example
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<configuration>
<skip>false</skip>
<check/>
<rules>
<rule>
<element>CLASS</element>
<excludes>
<exclude>*Test</exclude>
</excludes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
<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>
<configuration>
<outputDirectory>${project.build.directory}/jacoco</outputDirectory>
</configuration>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<check>
<instructionRatio>100</instructionRatio>
<branchRatio>95</branchRatio>
<lineRatio>90</lineRatio>
<methodRatio>90</methodRatio>
<classRatio>90</classRatio>
</check>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
GitHUb rpository JaCoCo
Projects that use JaCoCo
Maven Surefire Plugin
Maven Release Plugin

You need to add something like the below to your <build><plugins>:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.1.201405082137</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.20</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
That should generate you coverage reports in target/site/jacoco when you build your project with i.e. mvn clean install site
Note in my example plugin configuration the COVEREDRATIO limit is very low, you might want to set a higher value like 80 or so. The idea is to let a build fail if coverage is below that limit.

Add this 2 plugins to pom.xml file. Before adding these plugins make sure you add the dependencies "junit-jupiter-api" and "junit-jupiter-engine". You can find generated HTML file on target > site > jacoco > index.html.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</plugin>
<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>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>

Here is a complete pom that will help you:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>dummyJar</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>dummyJar</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.1.201405082137</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The report will be at this location:
/target/site/jacoco/index.html
And see this location for all goals and their config params:
http://www.eclemma.org/jacoco/trunk/doc/maven.html

Related

Exclude files/packages from SonarQube coverage

I have a package, com.org.projectname.model, in my project. What I want to do is exclude all the files within this package from SonarQube coverage. I tried,
<exclude>**/model /*.class</exclude> and <exclude>**/com/org/projectname/model/*.class</exclude>, but this didn't work.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<configuration>
<excludes>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>target/coverage-data/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>target/coverage-data/jacoco-ut.exec</dataFile>
<outputDirectory>target/coverage-reports/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
How to fix this issue? Or is there any other way?
Have you tried using sonar.exclusions in your POM properties?
<properties>
<sonar.exclusions>
**/model/*.java
</sonar.exclusions>
</properties>

Jacoco coverage percentage maven

When runnning mvn test, my spring boot application builds succesfully. When I do mvn clean install or mvn clean verify, build fails. This is because I have minimum code coverage ratio configured to 80%. When I run mvn clean install/verify, the result shows instructions covered ratio is 0.00, but expected minimum is 0.50.
Before setting the minimum threshold, running mvn clean install, gave the below result, which says 65% covered. What is missing in my configuration? Am I missing anything? How can I get jacoco pick the correct code covered?
Here is my pom
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.5</version>
<classifier>runtime</classifier>
<scope>test</scope>
</dependency>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
<dependencies>
<dependency>
<groupId>com.github.bitmc</groupId>
<artifactId>checkstyle</artifactId>
<version>8.35.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<excludes>
<exclude>com.pip</exclude>
</excludes>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
I am using java version "1.8.0_202"
In you's plugin config need set like that;
`
...
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<excludes>
<exclude>**/PACKEGE YOU WANT EXCLUDE.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>65%</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
`
in your's exemple the tag pluguin is out from plugins tag.
If you are using Jacoco with PowerMockito, and the class which you want to cover is taken in PrepareForTest, then in that case it will show you 0% coverage.
Try removing the target class from PrepareForTest

Findbugs plugin configuration in maven pom.xml

My goal is to execute findbugs on a maven project -> generate xml -> convert xml to html & finally fail build if there are HIGH priority FindBugs warnings. Below is the plugin configuration configuration in pom.xml I have configured
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<executions>
<execution>
<id>noFailOnError</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<failOnError>false</failOnError>
<xmlOutput>true</xmlOutput>
<findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>${project.build.directory}/findbugs</dir>
<outputDir>${project.build.directory}/findbugs</outputDir>
<stylesheet>fancy-hist.xsl</stylesheet>
<!--<stylesheet>default.xsl</stylesheet> -->
<!--<stylesheet>plain.xsl</stylesheet> -->
<!--<stylesheet>fancy.xsl</stylesheet> -->
<!--<stylesheet>summary.xsl</stylesheet> -->
<fileMappers>
<fileMapper
implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>.html</targetExtension>
</fileMapper>
</fileMappers>
</transformationSet>
</transformationSets>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<executions>
<execution>
<id>failing-on-high</id>
<phase>verify</phase>
<goals>
<goal>findbugs</goal>
</goals>
<configuration>
<failOnError>true</failOnError>
</configuration>
</execution>
</executions>
</plugin>
My problem is that html transformation is not happening stating that
[WARNING] No files found for transformation by stylesheet fancy-hist.xsl
Can the pom.xml correctness be verified & also can someone help me with the reason on why html tansformation is not happening ?
The issue with the above plugin configuration is that failing-on-high is configured during the verify phase rather than install phase. So in case of build error in verify phase, no output xml is generated because of which the output xml was not found. This was fixed by changing it to install
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<executions>
<execution>
<id>failing-on-high</id>
<phase>install</phase>
<goals>
<goal>findbugs</goal>
</goals>
<configuration>
<failOnError>true</failOnError>
</configuration>
</execution>
</executions>
</plugin>

Failure to execute maven goal with Jacoco

I'm trying with more than 15 version on jacoco and still is something wrong.
I've tried with different pom.xml files found on internet, but still without any effects.
Below my pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.build</artifactId>
<version>0.6.5.201403032054</version>
<relativePath>../org.jacoco.build</relativePath>
</parent>
<artifactId>jacoco</artifactId>
<packaging>pom</packaging>
<name>JaCoCo :: Distribution</name>
<description>JaCoCo Standalone Distribution</description>
<properties>
<jarsigner.skip>true</jarsigner.skip>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<reuseForks>true</reuseForks>
<argLine>${argLine} -Xmx2048m</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<reuseForks>true</reuseForks>
<argLine>${argLine} -Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.5.201403032054</version>
<executions>
<execution>
<id>prepare-unit-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-integration-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<propertyName>itCoverageAgent</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>jacoco-${qualified.bundle.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-distribution-size</id>
<goals>
<goal>enforce</goal>
</goals>
<phase>verify</phase>
<configuration>
<rules>
<requireFilesSize>
<maxsize>2500000</maxsize>
<minsize>2100000</minsize>
<files>
<file>${project.build.directory}/jacoco-${qualified.bundle.version}.zip</file>
</files>
</requireFilesSize>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And the output:
[ERROR] Failed to execute goal org.jacoco:jacoco-maven- plugin:0.6.5.201403032054:check (default-cli) on project jacoco: The parameters 'rules' for goal org.jacoco:jacoco-maven-plugin:0.6.5.201403032054:check are missing or invalid -> [Help 1]
And the output depends of version jacoco.
Do you have any ideas?
I suspect you are running a specific goal of jacoco (the check goal to be specific), meaning you are not running any specific phase of any lifecycle.
Like:
mvn jacoco:check
Note that your execution enforce-distribution-size is bound to the verify phase of the default lifecycle - so, if you want the execution enforce-distribution-size to occur, you should either:
run with a phase that is at least verify (I assume you don't want to do this) or
rename the <id> of that <execution> to default-cli (see this), if you require a maven run with specific jacoco goal to work, say the check goal. Like, change the following line:
<id>enforce-distribution-size</id>
to
<id>default-cli</id>
That's all you'll need to do.

maven cargo and selenium

i use maven cargo and selenium for automation. here is the code:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0.5</version>
<configuration>
<wait>false</wait>
<container>
<containerId>tomcat6x</containerId>
<zipUrlInstaller>
<url>
http://mirrors.enquira.co.uk/apache/tomcat/tomcat-6/v6.0.30/bin/apache-tomcat-6.0.30.zip
</url>
<installDir>${installDir}</installDir>
</zipUrlInstaller>
<output>
${project.build.directory}/tomcat6x.log
</output>
<log>${project.build.directory}/cargo.log</log>
</container>
<configuration>
<home>
${project.build.directory}/tomcat6x/container
</home>
<properties>
<cargo.logging>high</cargo.logging>
<cargo.servlet.port>8081</cargo.servlet.port>
</properties>
<files>
<copy>
<file>${project.basedir}/src/main/resources/datasource.properties</file>
<todir>webapps</todir>
<configfile>true</configfile>
<overwrite>true</overwrite>
</copy>
</files>
<properties>
<customMessage>${catalina.home}</customMessage>
</properties>
</configuration>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>configure</goal>
<goal>start</goal>
<goal>deploy</goal>
</goals>
<configuration>
<deployer>
<deployables>
<deployable>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<type>war</type>
<pingURL>**the url**</pingURL>
<pingTimeout>180000</pingTimeout>
<properties>
<context>**war-name**</context>
</properties>
</deployable>
</deployables>
</deployer>
</configuration>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
but as the war started getting bigger the pingtimeout started to increase, i dont want to use ping timeout, but i am being forced to at the moment, as deployment takes a bit of time and selenium does not wait if the pingtimeout is not mentioned.
is there any solution to this problem?
What about using Jetty? The maven-jetty-plugin will wait until your webapp is loaded. Alternatively, you can use the tomcat-maven-plugin and its deploy goal to deploy your webapp to a running Tomcat instance through the Tomcat Manager. This plugin will also wait with the execution (and therefore the launch of your Selenium tests) until the war is deployed.
This is my configuration. It will launch Jetty, deploy the application, launch Selenium, launch Selenium tests and finally quits all the servers:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<contextPath>/</contextPath>
<scanIntervalSeconds>0</scanIntervalSeconds>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<configuration>
<background>true</background>
</configuration>
<executions>
<execution>
<id>start-selenium</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start-server</goal>
</goals>
</execution>
<execution>
<id>stop-selenium</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-server</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>selenium-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*SeleniumTest.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources