java操作Jacoco合并dump文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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);
        }
    }
}
1
2
参考地址:
https://www.cnblogs.com/wozijisun/p/10442124.html

posted on   书梦一生  阅读(308)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示