I have a project for which the code coverage numbers are pretty low. After doing a deep-dive I found out the jacoco code-coverage stats are depending on target directoy instead of src.
target/generated-sources/delombok/com/
If I exclude the target directory the coverage goes down 0%. So isnt code-coverage supposed to be measured against the code (src) rather than the target folder. Below is the screenshot from SonarQube.
So the question is how do i make sure the code coverage is measured for src instead of target?
Following is the pom without dependencies:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>removed</groupId>
<artifactId>xxxx</artifactId>
<version>0.0.11-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cassandra-maven-plugin</artifactId>
<version>2.1.7-1</version>
<configuration>
<script>${basedir}/src/main/cassandra/local_run.cql</script>
<cqlVersion>3.2.0</cqlVersion>
<startNativeTransport>true</startNativeTransport>
<nativeTransportPort>9142</nativeTransportPort>
<jmxPort>17199</jmxPort>
<storagePort>17000</storagePort>
<loadFailureIgnore>false</loadFailureIgnore>
<cuLoadFailureIgnore>false</cuLoadFailureIgnore>
</configuration>
<executions>
<execution>
<id>start</id>
<goals>
<goal>start</goal>
<goal>load</goal>
</goals>
<phase>process-classes</phase> <!--Bind cassandra start and load to process-classes which is the phase that is called up before tomcat is run -->
</execution>
<execution>
<id>stop</id>
<goals>
<goal>stop</goal>
</goals>
<!--Bind cassandra stop to generate-test-sources which is the phase that is called up before
tests are run. This is because our tests start their own cassandra -->
<phase>integration-test</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
</plugin>
<!--Needed to add this so that src/main/java is recognized as a source folder -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<!-- Unit Test -->
<!-- Integration Test -->
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>activationloginedge</warName>
</configuration>
</plugin>
<!-- Tomcat Integration Test -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/activationloginedge</path>
</configuration>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>run</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<systemProperties>
<!-- We want test configuration for running integration tests. -->
<archaius.deployment.environment>test</archaius.deployment.environment>
<logback-lib.env>filesystem</logback-lib.env>
</systemProperties>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</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>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/jacoco-ut.exec</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>${project.build.directory}/jacoco-ut.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution> <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>${project.build.directory}/jacoco-it.exec</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>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<skipTests>${skip.unit.tests}</skipTests>
<!-- Excludes integration tests when unit tests are run. -->
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version> <executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<!-- Sets the VM argument line used when integration tests are run-->
<argLine>${failsafeArgLine}</argLine>
<!-- Skips integration tests if the value of skip.integration.tests property is true-->
<skipTests>${skip.integration.tests}</skipTests> </configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
OK, the Lombok Maven plugin modifies the list of source folders during the Maven build, this is why the target/generated-sources/delombok folder is recognized by SonarQube as a source folder.
In order to force SonarQube to consider only your own source files, you should add the following property to your POM:
<properties>
<sonar.sources>src/main/java</sonar.sources>
</properties>
This will solve your problem.
Related
I am using a buildnumber-maven-plugin to generate a sequence number for my jar. I will use it during my CICD process.
I followed all the examples available online. However, I can get the ${buildNumber} to print it, but while packaging the jar, I can't get the number, and I got the ${buildNumber} text. I searched for Maven LifeCycle, and I found I need to add it in the validation before any other plugin. But still, I can't solve the issue.
<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>${group.id}</groupId>
<artifactId>${artifact.id}</artifactId>
<version>${model.version}</version>
<profiles>
<profile>
<id>scala-2.11</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
......
</dependencies>
</profile>
</profiles>
<scm>
<connection>scm:svn:http://127.0.0.1/dummy</connection>
<developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
<tag>HEAD</tag>
<url>http://127.0.0.1/dummy</url>
</scm>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<finalName>${artifact.id}.${model.version}</finalName>
<plugins>
<!-- buildnumber-maven-plugin for automatic increment the version number -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<buildNumberPropertiesFileLocation>${build.number.dir}</buildNumberPropertiesFileLocation>
<revisionOnScmFailure>no.scm.config.in.pom</revisionOnScmFailure>
<doCheck>true</doCheck>
<doUpdate>true</doUpdate>
<format>{0,number}</format>
<items>
<item>buildNumber</item>
</items>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${build.number.dir}</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo>current build number is "${buildNumber}"</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>
<!-- ================== ;-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.source.version}</source>
<target>${java.source.version}</target>
<skipMain>true</skipMain> <!-- skip compile -->
<skip>true</skip> <!-- skip testCompile -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven.assembly.plugin.version}</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>
src/main/assembly/assembly-jar.xml
</descriptor>
</descriptors>
<finalName>${artifact.id}</finalName>
</configuration>
</execution>
<execution>
<id>bin</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>
src/main/assembly/assembly-bin.xml
</descriptor>
</descriptors>
<finalName>${tar.name}</finalName>
</configuration>
</execution>
</executions>
</plugin>
<properties>
<argLine>-Dfile.encoding=UTF-8 -Dlog4j.skipJansi=false -DmodelLogLevel=${modelLogLevel}</argLine>
<modelLogLevel>DEBUG</modelLogLevel>
<build.number.dir>${project.basedir}/buildNumber.properties</build.number.dir>
<java.source.version>1.8</java.source.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Plugin Versions -->
<maven.compiler.plugin.version>3.6.2</maven.compiler.plugin.version>
<maven.reports.plugin.version>2.9</maven.reports.plugin.version>
<maven.assembly.plugin.version>3.1.0</maven.assembly.plugin.version>
<surefire.plugin.version>2.7</surefire.plugin.version>
<group.id>com.test.model_name</group.id>
<artifact.id>proj_abc</artifact.id>
<build.type>snapshot</build.type>
<major.minor.version>0.1</major.minor.version>
<!-- here I am trying to retrieve the actual number-->
<build.number>${buildNumber}</build.number>
<model.version>${major.minor.version}.${build.number}.${build.type}</model.version>
</properties>
</project>
Note: If I run validate I could found the echo message below which means I can get the number correctly from the properties file. If I run package or run I got ${artifact.id}_source-.0.1.${buildNumber}.snapshot without the number.
main:
[echo] current build number is "90"
I also checked this similar question here & enter link description here and other links but can't figure out the problem.
I think what's happening here is that Maven resolves properties near the beginning of the build process.
<build.number>${buildNumber}</build.number>
<model.version>${major.minor.version}.${build.number}.${build.type}</model.version>
When it resolves build.number, ${buildNumber} does not have a value. So, it leaves the variable name unchanged. And I suspect you'd have the same problem if you modified model.version to this:
<model.version>${major.minor.version}.${buildNumber}.${build.type}</model.version>
for the same reason.
You can try using the build-helper-maven-plugin as described in this answer. However, this still may not work since model.version is used as the value of the project.version, which is resolved quite early as it's part of the GAV coordinates. It's worth a try though.
I am trying to get code coverage report for integration tests. Jacoco maven plugin is able to give the code coverage for unit tests , but giving 0% coverage for the integration tests. The integration tests are hitting the rest api end points of the app, which has been deployed in tomcat.
My maven jacoco plugin & surefire plugin look like this.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/target/jacoco-it.exec</dataFile>
<outputDirectory>${project.build.directory}/target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<!-- <skip>true</skip> -->
<!-- <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables> -->
</configuration>
<!-- <configuration> <skip>true</skip> </configuration> -->
<executions>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- Never skip running the tests when the test phase is invoked -->
<!-- <skip>true</skip> -->
<argLine>#{argLine}
-javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse
2016 CI\\JaxRsApp\\target\\jacoco.exec</argLine>
<includes>
<include>**/*UnitTest.java</include>
</includes>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- Never skip running the tests when the integration-test phase
is invoked -->
<!-- argLine>-javaagent:$WORKSPACE/target/lib/jacoco-agent-0.7.9.jar=includes=*,destfile=*/jacoco-coverage.exec,append=false</argLine -->
<skip>false</skip>
<argLine>#{argLine}
-javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse
2016 CI\\JaxRsApp\\target\\jacoco-it.exec
</argLine>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
<excludes>
<exclude>**/*UnitTest.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
I am executing unit tests in test phase & integration tests in integration test phase.
The latest error that i get is "Skipping JaCoCo execution due to missing execution data file."
Quoting documentation of prepare-agent-integration:
Same as prepare-agent, but provides default values suitable for integration-tests:
bound to pre-integration-test phase
different destFile
Quoting documentation of prepare-agent:
Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test. Depending on the project packaging type by default a property with the following name is set:
tycho.testArgLine for packaging type eclipse-test-plugin and
argLine otherwise.
In most cases argLine is automatically picked by maven-surefire-plugin that starts JVM to execute unit tests - http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine
argLine: Arbitrary JVM options to set on the command line.
This explains why you're getting coverage for unit tests.
To get the coverage for integration tests you must make sure that this property is passed to JVM of application under test, i.e. JVM that executes Tomcat, what is entirely and solely depends on a way you're launching it.
With Evgeny's help, i made this work.
Changed server to glassfish & ide to intellij for easier debugging.
Start glassfish server with following JVM options.
-DargLine=-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/dmahapat_JaxRsApp/target/coverage-reports/jacoco-it.exec
Updated pom
<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>org.mathworks</groupId>
<artifactId>JaxRsApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JaxRsApp</name>
<build>
<finalName>JaxRsApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-test-source</id>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}\src\integration-test\java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-resource</id>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<!-- Don't forget <directory> label -->
<directory>${project.basedir}\src\integration-test\resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<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>${project.build.directory}/coverage-reports/jacoco-ut.exec</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>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
<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>${project.build.directory}/coverage-reports/jacoco-it.exec</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>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<excludes>
<exclude>**/*IntegrationTest*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
<excludes>
<exclude>**/*UnitTest.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.25.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I have a a unit test (ProductDaoTest.java) and an integration test (ProductDaoIT.java) in my maven application.
I would like to execute only the integration test during the mvn verify command call but the unit test also gets executed even after excluding it using the <exclude> tag in the maven-failsafe-plugin configuration.
How can I fix this problem?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Updated POM (with solution):
<!-- For skipping unit tests execution during execution of IT's -->
<profiles>
<profile>
<id>integration-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<!-- Skips UTs -->
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- Binding the verify goal with IT -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>5000</port>
<path>${project.artifactId}</path>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
mvn clean install - Runs only unit tests by default
mvn clean install -Pintegration-test - Runs only integration tests by default
In Maven, test step is before verify step in the lifecycle.
So it you don't skip this step, it is bound to execute.
If you want to skip test , either use -Dmaven.test.skip=true as khmarbaise suggested, either create a dedicated Maven profile for IT where you will ignore unit-tests in this way :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
Generally, you create a Maven profile for integration tests, so if it is the case, gathering all the configuration in a place is better that scattering it.
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
Here is my pom.xml file and the error i got when building
Error:
Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (default-cli) on project load-xml-to-s3: Error reading assemblies: No assembly descriptors found.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tte.s3.load</groupId>
<artifactId>load-xml-to-s3</artifactId>
<version>1.0</version>
<name>load-xml-to-s3</name>
<build>
<plugins>
<!-- TOBE USED LATER - DO NOT DELETE. - KC <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId> <version>1.5.3</version> <executions> <execution>
<phase>prepare-package</phase> <goals> <goal>replace</goal> </goals> </execution>
</executions> <configuration> <file>target/classes/somefile.txt</file> <replacements>
<replacement> <token>SOME TOKEN</token> <value>SOME VALUE</value> </replacement>
</replacements> </configuration> </plugin> -->
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<mainClass>com.tte.s3.load.driver.Driver</mainClass>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>assembly</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>distribution.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The descriptors attribute of the maven-assembly-plugin expects a path starting from the base directory of your project. For example, if the file is located inside src/assembly/distribution.xml, this is the path you should specify.
There is also a problem in the phase you bound the plugin to. Phase assembly does not exist, you should use <phase>package</phase> instead.
As a side-node, you are using an old version of the plugin (2.2-beta-5). Consider using the latest version instead, which is 2.6.
Sample configuration:
<plugin>
<!-- no need to specify the groupId, it defaults to "org.apache.maven.plugins" -->
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version> <!-- explicit version is safer for build consistency than implicit -->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase> <!-- bind the execution to the "package" phase -->
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/distribution.xml</descriptor> <!-- use the path to the file starting from base directory -->
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
Running Maven with mvn clean install will invoke the plugin correctly in the package phase.