Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have code coverage in Jenkins with Jacoco and multiple modules?

My code structure is as follows :

events
messages
other-code
functional-tests

In the build script for jacoco, first it has to copy all classes and use that class directory to run the tool on. Can you please describe the steps wrt the target directories here. I mean how do I mention the directory on which to run the code coverage upon.

Upon build, each folder has its own target folder with classes in side them.

Here are the steps:

  1. Build project as a job in Jenkins
  2. Deploy it to user-stage
  3. Run jacoco report job in Jenkins

Jacoco report job explanation:

  1. Build step - maven 3.3.3 goals - clean test and testsuite, user-stage details and other info are passed.

  2. Post steps - execute shell

    Unable to format and paste here, so uploaded here: https://zerobin.net/?8a988cd05bf3d752#fbzMlW1b7uzD+HZnmwnd9WjQYBI3j95Q7DCIx6q+l0U=

  3. Invoke top level maven targets - maven 3.3.3 -f jacoco_pom.xml jacoco:dump antrun:run@report

The clasDir variable that's been used in the shell paste should have classes. So far, I have done this in the start of shell script.

mkdir -p target/classes
cp -R messages/target/classes target/classes

and set clsDir = target/classes,

This way I got the report on that module. My testsuite contained only one class. I want to include other modules also like events, other-code and also link the sources.

I need help on how should i set it up. The main purpose is to generate code coverage reports of functional tests.

EDIT:

Answer: Below answer helped, but it was all to be done with shell commands not any UI. So, cloning the repo, doing mvn clean package -DskipTests and copying over all first level subfolders from all the module folders helped, but then it showed error in coverage Execution data mismatch for class files. Which means that the class instrumented and the class we are using for coverage are built separately by different JVM (one in Jenkins another one thats deployed in stage).

So the solution was to not clone and recompile them, but download the manifest from the deployment repository and unpacking all the jars. This way I had all the classes in same version.

like image 396
xploreraj Avatar asked Feb 16 '17 05:02

xploreraj


People also ask

How does Jenkins integrate code coverage?

If you click on the Coverage Report icon, you will see code coverage for each package in your application, and even drill down to see the code coverage (or lack thereof) for an individual class (see Figure 2.31, “Jenkins lets you display code coverage metrics for packages and classes”).

What is difference between JaCoCo and SonarQube?

JaCoCo vs SonarQube: What are the differences? JaCoCo: A code coverage library for Java. It is a free code coverage library for Java, which has been created based on the lessons learned from using and integration existing libraries for many years; SonarQube: Continuous Code Quality.


1 Answers

To include multiple class directories by changing the Jenkins JaCoCo plugin configuration, you would need to modify the 'Path to class directories' field.

For instance, if you want to include any classes under the directories 'events' and 'other-code', you would add the following to the 'Path to class directories' section:

**/events,**/other-code

Keep in mind that if you want to add multiple directories, you have to separate each one by a comma and there can be no spaces (due to a bug with the plugin). Note: this comma bug is true of all text fields in this plugin configuration.

Here is my working JaCoCo plugin configuration: Example JaCoCo Plugin Configuration

EDIT: As mentioned in comments, one solution to this problem is to utilize whatever build tool you are dealing with (Ant, Maven, Gradle) to drop the target (ie: jar, war, etc.) into a common directory and just point the jacoco plugin there.

like image 89
lax1089 Avatar answered Oct 05 '22 09:10

lax1089