简体   繁体   中英

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.

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: 示例JaCoCo插件配置

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM