Jacoco生命周期

2024-05-01 14:38
文章标签 生命周期 jacoco

本文主要是介绍Jacoco生命周期,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近看Jacoco源码并进行修改来实现增量覆盖率的功能,Jacoco的插桩使用分为on-the-fly和offline两种方式:

  •  on-the-fly:通过在程序运行时指定javaagent,运行产出只包含覆盖率的结果Jacoco.exec,对原始jar不做修改。
  •  offline:通过修改字节码生成插桩后的字节码,然后执行插桩后的字节码运行,同样产出覆盖率文件Jacoco.exec。

通过maven插件均可以应用on-the-fly和offline两种模式。


offline模式

    <plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.8-SNAPSHOT</version><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-report</id><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.60</minimum></limit></limits></rule></rules></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.12.2</version><configuration><systemPropertyVariables><jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile></systemPropertyVariables></configuration></plugin>

 首先看下maven的生命周期:

<phases><phase>validate</phase><phase>initialize</phase><phase>generate-sources</phase><phase>process-sources</phase><phase>generate-resources</phase><phase>process-resources</phase><phase>compile</phase><phase>process-classes</phase> --------------- instrument<phase>generate-test-sources</phase><phase>process-test-sources</phase><phase>generate-test-resources</phase><phase>process-test-resources</phase><phase>test-compile</phase><phase>process-test-classes</phase><phase>test</phase>                         <phase>prepare-package</phase> --------------- restore-instrumented-classes<phase>package</phase><phase>pre-integration-test</phase><phase>integration-test</phase><phase>post-integration-test</phase><phase>verify</phase> ------------------------ report / check<phase>install</phase><phase>deploy</phase>                   
</phases>

而上述pom配置明确指定了4项配置:

compile:编译成class文件,相当于执行以下命令:

mvn clean compile

instrument:对class文件进行,相当于执行以下命令:

mvn org.jacoco:jacoco-maven-plugin:0.8.8-SNAPSHOT:instrument 

会对原class进行插桩,生存插桩后的class文件,例如:

    protected boolean canEqual(Object other) {boolean[] var2 = $jacocoInit();boolean var10000 = other instanceof BasicStatic;var2[13] = true;return var10000;}public int hashCode() {boolean[] var1 = $jacocoInit();int PRIME = true;int result = 1;Object $statics = this.getStatics();int var10000 = result * 59;int var10001;if ($statics == null) {var10001 = 43;var1[14] = true;} else {var10001 = $statics.hashCode();var1[15] = true;}int result = var10000 + var10001;var1[16] = true;return result;}

通过插桩后的代码很清楚的验证了jacoco的插桩规则。

  • 同一个类用一个插桩数组表示;
  • 在逻辑分支后插桩;

test:执行测试,生成覆盖率文件Jacoco.exec

restore-instrumented-classes:删除插桩标记,恢复class文件,相当于执行:

mvn org.jacoco:jacoco-maven-plugin:0.8.8-SNAPSHOT:restore-instrumented-classes

report:生成测试报告,相当于执行:

mvn org.jacoco:jacoco-maven-plugin:0.8.8-SNAPSHOT:report

在target目录下生成site文件夹,内含报告文件

check:按配置的覆盖率阈值来校验结果


on-the-fly模式

            <plugin><groupId>org.jacoco</groupId><artifactId>jacoco-maven-plugin</artifactId><version>0.8.8-SNAPSHOT</version><executions><execution><id>default-prepare-agent</id><goals><goal>prepare-agent</goal></goals></execution><execution><id>default-report</id><phase>test</phase><goals><goal>report</goal></goals></execution></executions></plugin>
prepare-agent:准备mvn编译参数,添加jacocoAgent,执行完输出以下日志:

argLine set to -javaagent:/Users/test/.m2/repository/org/jacoco/org.jacoco.agent/0.8.8-SNAPSHOT/org.jacoco.agent-0.8.8-SNAPSHOT-runtime.jar=destfile=/Users/test/project/hive-udf/target/jacoco.exec
关于添加此参数后的完整命令需要了解mvn编译具体执行。

添加此参数后执行compile,不会生产带有插桩信息的类;执行test,会产生覆盖率数据jacoco.exec。

然后执行report,生产覆盖率报告信息。

这篇关于Jacoco生命周期的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/951953

相关文章

Maven(插件配置和生命周期的绑定)

1.这篇文章很好,介绍的maven插件的。 2.maven的source插件为例,可以把源代码打成包。 Goals Overview就可以查看该插件下面所有的目标。 这里我们要使用的是source:jar-no-fork。 3.查看source插件的example,然后配置到riil-collect.xml中。  <build>   <plugins>    <pl

【Vue】关于Vue3的生命周期

目录 Vue3中新增了一个setup生命周期函数:(1) setup执行的时机是在beforeCreate生命周期函数之前执行,在setup函数中是不能通过this来获取实例的;(2) 为了命名的统一性,将beforeDestroy 改名为 beforeUnmount,destroyed 改名为 unmounted 生命周期函数: setup —— 不能通过this来获

09 生命周期

生命周期 beforeCreatecreatedbeforeMountmountedbeforeUpdateupdatedbeforeDestorydestoryed 辣子鸡:香辣入口,犹如吃了炫迈一样 - - - 根本停不下来 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport"

Maven生命周期:深入理解构建过程

目录 1. Maven生命周期简介 2. 默认生命周期的阶段 3. 清理生命周期 4. 站点生命周期 5. Maven生命周期的灵活性 6. 结论         在Java开发中,Maven是一个不可或缺的工具,它通过自动化项目的构建、依赖管理和文档生成等任务,极大地提高了开发效率。Maven的核心之一是其构建生命周期,它定义了项目构建过程中的一系列阶段。在这篇文章中,我们将深

【前端】animation动画以及利用vue制作简单的透明度改变动画,包含vue生命周期实现

一. 问题描述 想做一个文字透明度从1到0然后再从0到1的css动画。 二. 代码写法 2.1 animation写法 2.1.1 animation属性key 2.1.2 代码展示 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=de

【C++多线程编程】 线程安全与对象生命周期管理

目录 类的线程安全 实现线程安全  构造函数在多线程中的安全性 析构函数多线程环境的安全 智能指针实现多线程安全  shared_ptr 非完全线程安全 shared_ptr可能导致对象生命周期延长 const引用可以减少传递shared_ptr开销 shared_ptr 智能指针块模块的优点  析构所在线程问题分析  RAII的使用 enable_shared_from_

【ReactJS】通过一个例子学习React组件的生命周期

源代码 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Reac

1.1 Avtivity的生命周期全面分析

本文将Activity的生命周期分为两部分内容,一部分是典型情况下的生命周期,另一部分是异常情况下的生命周期。所谓典型情况下的生命周期,是指在有用户参与的情况下,Activity所经过的生命周期的改变;而异常情况下的生命周期是指在Activity被系统回收或者由于当前设备的Configuration发生改变从而导致Activity被销毁重建,异常情况下的生命周期的关注点和典型情况下略有不同。 1

LeakCanary测试app内存泄露+registerActivityLifecycleCallbacks管理Activity的生命周期

public class MyApplication extends Application {private String tag = "MyApplication";private static Stack<Activity> activityStack;//检测内存泄露private RefWatcher refWatcher;@Overridepublic void onCreate()

SpringMVC request生命周期

第一步:用户发起一个请求到前端控制器(DispatcherServlet) 第二步:前端控制器请求HanderMapping查找Handler,可以根据xml配置文件,注解进行查找。 第三步:处理器映射器HandlerMapping向前端控制器返回Handler 第四步:前端控制器调用处理器适配器去执行Handler 第五步:处理器适配器去执行Handler 第六步:Handler执行完