Exclude files/packages from SonarQube coverage - java

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>

Related

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

Is it possible to only run the JaCoCo maven plugin during the site lifecycle?

Consider the follow build configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${surefireArgLine}</argLine>
<parallel>all</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<parallel>classes</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/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}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
And reporting configuration:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<id>JaCoCo-UnitTest</id>
<reports>
<report>report</report>
</reports>
<configuration>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</reportSet>
<reportSet>
<id>JaCoCo-IntegrationTest</id>
<reports>
<report>report</report>
</reports>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
</configuration>
</reportSet>
</reportSets>
</plugin>
The awesome part is mvn clean site works awesome, I get my reports. However, this now means that the JaCoCo agent is starting during the normal lifecycle, ie: mvn clean install verify.
Is there a way to disable the JaCoCo agent during the normal lifecycle but not the site cycle?
Simply skip the JaCoCo execution using a property:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Skip this phase if jacoco.skip property is set to true -->
<skip>${jacoco.skip}</skip>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Skip this phase if jacoco.skip property is set to true -->
<skip>${jacoco.skip}</skip>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
You can also disable JaCoCo execution when tests are skipped:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<!-- Skip tests if skip.unit.tests property is set to true -->
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Skip this phase if unit tests are skipped -->
<skip>${skip.unit.tests}</skip>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Skip this phase if unit tests are skipped -->
<skip>${skip.unit.tests}</skip>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>

Ignore Classes and Methods form Code Coverage (jacoco)

In my application I want to ignore to classes (packageName1/Starter/packageName1/IO/CmdException.class) and for every class equals and hashCode method.
My pom.xml looks like:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.0.201403182114</version>
<configuration>
<instrumentation>
<ignores>
<ignore>*hashCode</ignore>
<ignore>*equals</ignore>
</ignores>
<excludes>
<exclude>packageName1/Starter.class</exclude>
<exclude>packageName1/IO/CmdException.class</exclude>
</excludes>
</instrumentation>
</configuration>
<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>
</plugins>
</build>
The problem is that coverage still threats these classes and methods.
This Works for me to exclude classes.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<configuration>
<ignores>
<ignore>com.sample.package.bean.*</ignore>
</ignores>
<excludes>
<!-- all class which we want to exclude from the coverage report -->
<exclude>**/*class1.class</exclude>
<exclude>**/*class2.class</exclude>
<exclude>**/*class3.class</exclude>
<exclude>**/*class4.class</exclude>
</excludes>
</configuration>
<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>

How to add JaCoCo in maven

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

Is there any maven sleep functionality?

I have maven profile set for testing where in pre-integration-tests maven starts two jetty servers and afterwards tests are started. The problem I've stumbled into is in the servers, they aren't fully loaded when tests start. It seems like the problem is fixed by adding 5 second sleep time into the tests, but I wish to add it in maven and remove from tests. Is there anyway I could do that? Please look into code sample below for additional information
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.work.projectname</groupId>
<artifactId>projectname-web</artifactId>
<version>${project.version}</version>
<type>war</type>
<destFileName>projectname-${project.version}.war</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.work.projectname</groupId>
<artifactId>projectname-stubs-web</artifactId>
<version>${project.version}</version>
<type>war</type>
<destFileName>projectname-stubs-${project.version}.war</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-server.version}</version>
<executions>
<execution>
<id>start-projectname</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy-war</goal>
</goals>
<configuration>
<daemon>true</daemon>
<reload>manual</reload>
<war>${project.build.directory}/projectname-${project.version}.war</war>
<webAppXml>${basedir}/src/jetty/jetty-loginService.xml</webAppXml>
<webAppConfig>
<contextPath>/projectname</contextPath>
<parentLoaderPriority>true</parentLoaderPriority>
<tempDirectory>${project.build.directory}/tmp-projectname</tempDirectory>
</webAppConfig>
<stopPort>8006</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>start-projectname-stubs</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy-war</goal>
</goals>
<configuration>
<daemon>true</daemon>
<reload>manual</reload>
<war>${project.build.directory}/projectname-stubs-${project.version}.war</war>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>${stub-jetty-server.port}</port>
<maxIdleTime>300000</maxIdleTime>
</connector>
</connectors>
<stopPort>8008</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>stop-projectname</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<stopPort>8006</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
<execution>
<id>stop-projectname-stubs</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<stopPort>8008</stopPort>
<stopKey>STOP</stopKey>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<configuration>
<parallel>classes</parallel>
<threadCountClasses>33</threadCountClasses>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can do it with the help of the maven antrun plugin. Something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<configuration>
<tasks>
<sleep seconds="5" />
</tasks>
</configuration>
<executions>
<execution>
<id>sleep-for-a-while</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
EDIT: In case someone will catch this. Based on the comment from jl, tasks have indeed been deprecated and here is the same thing based on using targets instead. Sligthly reorganized but has the same function.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>sleep-for-a-while</id>
<phase>pre-integration-test</phase>
<configuration>
<target>
<sleep seconds="5" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I have created a maven-sleep-plugin once. Not sure how much it works now, you can try and let us know.
Usage (after checking out the source and building):
<plugin>
<groupId>org.jboss.maven.plugins</groupId>
<artifactId>maven-sleep-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>sleep-5-seconds</id>
<phase>pre-integration-test</phase>
<goals>
<goal>sleep</goal>
</goals>
<configuration>
<delay>5</delay>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources