• java操作Jacoco合并dump文件


           记录瞬间

    import org.apache.maven.plugin.MojoExecutionException;
    import org.jacoco.core.tools.ExecFileLoader;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MergeDump {
    
        private final String path ;
        private final File destFile ;
    
        public MergeDump(String path){
            this.path = path;
            this.destFile = new File(path + "/jacoco.exec");
        }
    
        private List<File> fileSets(String dir){
            System.out.println(dir);
            List<File> fileSetList = new ArrayList<File>();
            File path = new File(dir);
            if ( ! path.exists() ){
                System.out.println("No path name is :" + dir);
                return null;
            }
            File[] files = path.listFiles();
            try {
                if (files == null || files.length == 0) {
                    return null;
                }
            } catch (NullPointerException npe) {
                npe.printStackTrace();
            }
            for (File file : files) {
                if (file.getName().contains(".exec")) {
                    System.out.println("文件:" + file.getAbsolutePath());
                    fileSetList.add(file);
                } else {
                    System.out.println("非exec文件:" + file.getAbsolutePath());
                }
            }
            return fileSetList;
        }
    
        public void executeMerge() throws MojoExecutionException {
    
            final ExecFileLoader loader = new ExecFileLoader();
            load(loader);
            save(loader);
            // 执行完成后,删除非必须的dump文件
            for (final File fileSet : fileSets(this.path)) {
                if ( ! fileSet.getName().equals("jacoco.exec") ) {
                    fileSet.delete();
                }
            }
        }
    
        /**
         * 加载dump文件
         * @param loader
         * @throws MojoExecutionException
         */
        public void load(final ExecFileLoader loader) throws MojoExecutionException {
            for (final File fileSet : fileSets(this.path)) {
                System.out.println(fileSet.getAbsoluteFile());
                final File inputFile = new File(this.path, fileSet.getName());
                if (inputFile.isDirectory()) {
                    continue;
                }
                try {
                    System.out.println("Loading execution data file " + inputFile.getAbsolutePath());
                    loader.load(inputFile);
                    System.out.println(loader.getExecutionDataStore().getContents());
                } catch (final IOException e) {
                    throw new MojoExecutionException("Unable to read "
                            + inputFile.getAbsolutePath(), e);
                }
            }
        }
    
        /**
         * 执行合并文件
         * @param loader
         * @throws MojoExecutionException
         */
        public void save(final ExecFileLoader loader) throws MojoExecutionException {
            if (loader.getExecutionDataStore().getContents().isEmpty()) {
                System.out.println("Skipping JaCoCo merge execution due to missing execution data files");
                return;
            }
            System.out.println("Writing merged execution data to " + this.destFile.getAbsolutePath());
            try {
                loader.save(this.destFile, false);
            } catch (final IOException e) {
                throw new MojoExecutionException("Unable to write merged file "
                        + this.destFile.getAbsolutePath(), e);
            }
        }
    }

    实现了,存在多个dump文件时,通过调用jacoco的源码,对多个dump文件合并,合并成一个dump文件,便于分析。

  • 相关阅读:
    WPF控件操作之改变父控件之TabControl示例
    WPF里面的DockPanel的Fill去哪了,如何填满整个空间
    [原创]winform自定义控件之类属性-多重属性-可折叠属性
    WinForm之DataBinding
    WinForm自定义控件之DefaultValue的误解
    code snippet:依赖属性propa的小技巧
    【原创】WinForm中实现单独Time控件的方式
    node.js安装本地模块遇到的目录锁定问题【新手问题】
    《程序员思维修炼》之德雷福斯模型
    IOptions、IOptionsMonitor以及IOptionsSnapshot
  • 原文地址:https://www.cnblogs.com/wozijisun/p/10442124.html
Copyright © 2020-2023  润新知