本文主要是介绍jacoco之java api调用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我们在使用jacoco的时候,除了使用命令行的方式,可能需要把jacoco的方法嵌入到我们应用程序,自定义一些步骤,有很多小伙伴对这种调用方式很迷惑,这里简单demo,供大家参开下,具体api的方法可以参考官网
环境准备
我们使用api的方式使用jacoco,首先需要知道我们要做什么动作,比如我们需要生成报告
1.引入jacoco core和report包(如果需要使用增量,请使用我的代码编译出的包)
这里有个小问题是,core打包的时候是排除了依赖包的,所以我们这里需要将core的依赖包重新引用下
<asm.version>9.0</asm.version><fastjson.version>1.2.75</fastjson.version>
<dependencies><dependency><groupId>org.ow2.asm</groupId><artifactId>asm</artifactId></dependency><dependency><groupId>org.ow2.asm</groupId><artifactId>asm-commons</artifactId></dependency><dependency><groupId>org.ow2.asm</groupId><artifactId>asm-tree</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency></dependencies>
编写生成报告代码
package com.dr.flowable;import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.tools.ExecFileLoader;
import org.jacoco.report.DirectorySourceFileLocator;
import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.html.HTMLFormatter;import java.io.File;
import java.io.IOException;/*** @ProjectName: flowable* @Package: com.dr.flowable* @Description: 覆盖报告生成* @Author: ray* @CreateDate: 2021/8/26 14:17* @Version: 1.0* <p>* Copyright: Copyright (c) 2021*/
public class CoverApiTest {/*** clsss的类目录*/private File classesDirectory;/*** 源码目录*/private File sourceDirectory;/*** exec文件目录*/private File executionDataFile;/*** 报告生成目录*/private File reportDirectory;public CoverApiTest(File classesDirectory, File sourceDirectory, File executionDataFile, File reportDirectory) {this.classesDirectory = classesDirectory;this.sourceDirectory = sourceDirectory;this.executionDataFile = executionDataFile;this.reportDirectory = reportDirectory;}public static void main(String[] args) {try {String projectDirectory = "D:\\IdeaProjects\\base\\base-service\\application";//目录下必须包含源码编译过的class文件,用来统计覆盖率。所以这里用server打出的jar包地址即可,运行的jar或者Class目录File classesDirectory = new File(projectDirectory, "target\\classes\\com");File sourceDirectory = new File(projectDirectory, "src\\main\\java");//覆盖率的exec文件地址File executionDataFile = new File("D:\\jacoco\\jacoco-demo.exec");//要保存报告的地址File reportDirectory = new File("D:\\jacoco", "coveragereport");CoverApiTest coverApiTest = new CoverApiTest(classesDirectory, sourceDirectory, executionDataFile, reportDirectory);//解析execExecFileLoader execFileLoader = coverApiTest.loadExecutionData(executionDataFile);//对比exec和class类,生成覆盖数据IBundleCoverage bundleCoverage = coverApiTest.analyzeStructure(execFileLoader);//生成报告coverApiTest.createReport(bundleCoverage, execFileLoader);} catch (IOException e) {e.printStackTrace();}}/*** 第一步解析exec文件** @throws IOException*/public ExecFileLoader loadExecutionData(File executionDataFile) throws IOException {// 解析execExecFileLoader execFileLoader = new ExecFileLoader();execFileLoader.load(executionDataFile);return execFileLoader;}public IBundleCoverage analyzeStructure(ExecFileLoader execFileLoader) throws IOException {//增量覆盖
// final CoverageBuilder coverageBuilder = new CoverageBuilder("{method:"add"}");final CoverageBuilder coverageBuilder = new CoverageBuilder();final Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(), coverageBuilder);analyzer.analyzeAll(this.classesDirectory);return coverageBuilder.getBundle("报告率");}public void createReport(final IBundleCoverage bundleCoverage, ExecFileLoader execFileLoader)throws IOException {// Create a concrete report visitor based on some supplied// configuration. In this case we use the defaultsfinal HTMLFormatter htmlFormatter = new HTMLFormatter();final IReportVisitor visitor = htmlFormatter.createVisitor(new FileMultiReportOutput(this.reportDirectory));// Initialize the report with all of the execution and session// information. At this point the report doesn't know about the// structure of the report being createdvisitor.visitInfo(execFileLoader.getSessionInfoStore().getInfos(), execFileLoader.getExecutionDataStore().getContents());// Populate the report structure with the bundle coverage information.// Call visitGroup if you need groups in your report.visitor.visitBundle(bundleCoverage, new DirectorySourceFileLocator(this.sourceDirectory, "utf-8", 4));visitor.visitEnd();}}
如果其他功能不知道api的调用方法,可以去官网查询api或demo
这篇关于jacoco之java api调用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!