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.
Related
I've this problem with Jacoco maven plugin in IntelliJ IDEA 2020.
I put the Jacoco plugin configuration in my pom.xml maven file and then executed the mvn clean install command. It generates the target\site folder but when I open the index.html file it shows me a coverage of 0%, listing all my classes, although I get a 92% coverage in IntelliJ's integrated tool. The jacoco.exec file is generated in target folder, but it's empty!
How can I fix this issue?
Here's my pom.xml fragment:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<configuration>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
<goal>check</goal>
</goals>
<configuration>
<rules>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
I'm using the latest Java version (14).
Thanks in advance!
I have a maven build with 2 relevant profiled called integration-build (to run integration tests) and sonar (for coverage).
The integration build without coverage works
the normal build without integration tests + coverage in sonar works (coverage appears in sonarqube)
the integration build with coverage does not work (coverage is 0)
So the last type of build fails. My command is:
mvn clean install -Pintegration-build,sonar
This seems the relevant part in my integration-build maven profile:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>-Xms256m -Xmx2048m</argLine>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<configuration>
<argLine>-Xms256m -Xmx2048m</argLine>
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
And this is my sonar profile:
<profile>
<id>sonar</id>
<properties>
<envTarget>tst</envTarget>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>#{surefire.argLine} -Xms256m -Xmx2048m</argLine>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<configuration>
<argLine>#{failsafe.argLine} -Xms256m -Xmx2048m</argLine>
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<append>true</append>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>surefire.argLine</propertyName>
<destFile>${project.basedir}/target/jacoco.exec</destFile>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.basedir}/target/jacoco.exec</dataFile>
</configuration>
</execution>
<execution>
<id>pre-it-test</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<propertyName>failsafe.argLine</propertyName>
<destFile>${project.basedir}/target/jacoco-it.exec</destFile>
</configuration>
</execution>
<execution>
<id>post-it-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.basedir}/target/jacoco-it.exec</dataFile>
</configuration>
</execution>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<createChecksum>true</createChecksum>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Now I updated these lines in my integration-build profile:
<argLine>-Xms256m -Xmx2048m</argLine> to <argLine>#{surefire.argLine} -Xms256m -Xmx2048m</argLine>
and
<argLine>-Xms256m -Xmx2048m</argLine> to <argLine>#{failsafe.argLine} -Xms256m -Xmx2048m</argLine>
Then it works. after his I run mvn sonar:sonar -Psonar and I get integration tests and coverage.
But when I now run just with only the integration-build profile (without sonar) then it fails!
Then I get errors like:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.0:test (default-test) on project app-models: There are test failures.
[ERROR]
[ERROR] Please refer to /srv/path//build/build-IB/app-models/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date]-jvmRun[N].dump, [date].dumpstream and [date]-jvmRun[N].dumpstream.
[ERROR] The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
[ERROR] Command was /bin/sh -c cd "/srv/autobuild/workspace/build/build-IB/app-models" && /srv/path/tools/hudson.model.JDK/IBM-JDK8-x86_64/jre/bin/java '#{surefire.argLine}' -Xms256m -Xmx2048m -jar '/srv/autobuild/workspace/build/build-IB/app-models/target/surefire/surefirebooter6984324204710827506.jar' '/srv/autobuild/workspace/build/build-IB/app-models/target/surefire' 2020-03-20T11-00-50_881-jvmRun1 surefire5755643707151660451tmp surefire_04579178873474557152tmp
[ERROR] Error occurred in starting fork, check output in log
[ERROR] Process Exit Code: 1
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
[ERROR] Command was /bin/sh -c cd "/srv/path/workspace/build/build-IB/app-models" && /srv/path/tools/hudson.model.JDK/IBM-JDK8-x86_64/jre/bin/java '#{surefire.argLine}' -Xms256m -Xmx2048m -jar '/srv/path/workspace/build/build-IB/app-models/target/surefire/surefirebooter6984324204710827506.jar' '/srv/path/workspace/build/build-IB/app-models/target/surefire' 2020-03-20T11-00-50_881-jvmRun1 surefire5755643707151660451tmp surefire_04579178873474557152tmp
How can I fix this but keep my coverage?
I think the main problem is that you put <configuration> in the failsafe and surefire plugin that is not compatible when you have both profiles activated.
Remember that profiles are merged when you activate them. So you suddenly have two <argLine> definitions etc.
I guess you need to move as much as possible of the <configuration> into the <execution> that needs it.
Can you tell me how to call maven surefire in command line with the following configuration ?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<id>Custom tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<classesDirectory>target/generated/classes/normalCase/</classesDirectory>
<reportsDirectory>target/generated/reports/normalCase/</reportsDirectory>
</configuration>
</execution>
</executions>
</plugin>
When surefire is defined like this in my pom.xml and I execute the phase test, it work exactly how i want it to work : it tries to run my tests on the classes located in target/generated/classes/normalCase.
So I tried this command line:
mvn surefire:test -DclassesDirectory="target/generated/classes/normalCase/"
But no, it keep checking the classes in the default value directory which is "target/classes".
So how can I achieve this in command line ?
To recap the situation you have. You are generating multiple version of your source code during the build, each of those version ends up in a separate folder under target. For each of those versions, you would like to execute your unit tests with the maven-surefire-plugin. Let's consider the base directory to be target/generated/classes. That means you have multiple subdirectories target/generated/classes/version1, target/generated/classes/version2... for each version.
A possible solution would be to use the iterator-maven-plugin to iterate over all subdirectories of a folder and invoke the maven-surefire-plugin from all those subdirectories. The variable #item# holds the current item.
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.3</version>
<executions>
<execution>
<id>iterate</id>
<phase>test</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<folder>target/generated/classes</folder>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<goal>test</goal>
<configuration>
<classesDirectory>target/generated/classes/#item#</classesDirectory>
<reportsDirectory>target/generated/reports/#item#</reportsDirectory>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
I want to skip a plugin execution when I run the command 'mvn deploy'. Say in below example, I DON'T want to execute the 'properties-maven-plugin' in 'mvn deploy'
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${session.executionRootDirectory}/xxxx.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<tagNameFormat>#{artifactId}/#{artifactId}-#{version}</tagNameFormat>
</configuration>
</plugin>
<plugins>
Your plugin is bound to prepare-package phase, so I think you can not avoid it execution when deploying.
But, it is possible to create a profile that contains that plugin, such way you activate (command line, Jenkins configuration, ...) the profile when you want to run the plugin.
This way you can control whether to run it, but it is not an answer to your question, because this way you can not avoid the plugin execution on deploy.
I use maven in my java build process. The following is a snippet of code that creates an single jar with all dependencies. In order to reduce the data transfer on small changes to the build I'd like to place all project files (including dependencies) in the folder target/build . I plan to rsync the folder with the remote machine running the app and run the app with:
java -cp target/build/* <classname>
How do I modify this snippet to achieve this? I've read the documentation here but don't know how to piece the fix together:
http://maven.apache.org/plugins/maven-assembly-plugin/
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Are you asking how to get maven to copy your dependencies to the target folder when you build?
I think you want the maven dependency plugin. It copies the dependencies of your project to an output folder you specify.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${targetDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
It sounds like you may also need to maven jar plugin to tell it where to package your jar to.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>${dir}</outputDirectory>
</configuration>
</plugin>
Use the maven dependency plugin
It has the gole: copy-dependencies. This should do what you want.
Example (take from the documentation)
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>