jacocotestdemo

2.代码结构

在这里插入图片描述

3.程序入口,先执行dump,再执行create

public static void main(String[] args) {
//获取Jenkins的docker中,业务service的java源码路径和classes路径,目前仅支持 %s-service 模式路径的应用
String dirStr = “/var/jenkins_home/workspace/%s/%s-service”;
String jobName = args[0];

String sname = Utils.getSname(jobName);
String host = Utils.getHost(jobName);
int port = Integer.valueOf(args[1]);
//先执行dump
try {
new DumpService().dump(false,jobName,host,port);
} catch (IOException e) {
e.printStackTrace();
}
//再执行create
try {
new ReportCreateService(new File(String.format(dirStr,jobName,sname))).create();
} catch (IOException e) {
e.printStackTrace();
}
}

4.dump实现

public void dump(boolean reset,String jobName,String host,int port) throws IOException {
dump(reset, false,host,jobName,port);
}

protected void dump(boolean reset, boolean append,String host,String jobName,int port) throws IOException {
ExecDumpClient dumpClient = new ExecDumpClient() {
@Override
protected void onConnecting(InetAddress address, int port) {
System.out.println(“Connecting to {”+address+"}:{"+port+"}");
}

    @Override

protected void onConnectionFailure(IOException exception) {
System.out.println(exception.getMessage());
}
};

String sname = Utils.getSname(jobName);

dumpClient.setReset(reset);
dumpClient.setRetryCount(retryCount);

final ExecFileLoader dump = dumpClient.dump(host, port);
dump.save(new File(String.format(dumpStr,jobName,sname,sname)), append);
System.out.println(String.format(“Dump successful! Create dumped file: {}”, dumpStr));
}

5.ReportCreateService实现类

public class ReportCreateService {

private final String title;
private final File executionDataFile;
private final File classesDirectory;
private final File sourceDirectory;
private final File reportDirectory;

private ExecFileLoader execFileLoader;
public ReportCreateService(final File projectDirectory) {
this.title = projectDirectory.getName();
this.executionDataFile = new File(projectDirectory, “dump.exec”);
this.classesDirectory = new File(projectDirectory, “target/classes”);
this.sourceDirectory = new File(projectDirectory, “src/main/java”);
this.reportDirectory = new File(projectDirectory, “report”);
}
public void create() throws IOException {
loadExecutionData();
final IBundleCoverage bundleCoverage = analyzeStructure();
createReport(bundleCoverage);
}

private void createReport(final IBundleCoverage bundleCoverage)
throws IOException {
final HTMLFormatter htmlFormatter = new HTMLFormatter();
final IReportVisitor visitor = htmlFormatter
.createVisitor(new FileMultiReportOutput(reportDirectory));
visitor.visitInfo(execFileLoader.getSessionInfoStore().getInfos(),
execFileLoader.getExecutionDataStore().getContents());
visitor.visitBundle(bundleCoverage, new DirectorySourceFileLocator(
sourceDirectory, “utf-8”, 4));
visitor.visitEnd();

}

private void loadExecutionData() throws IOException {
execFileLoader = new ExecFileLoader();
execFileLoader.load(executionDataFile);
}

private IBundleCoverage analyzeStructure() throws IOException {
final CoverageBuilder coverageBuilder = new CoverageBuilder();
final Analyzer analyzer = new Analyzer(
execFileLoader.getExecutionDataStore(), coverageBuilder);

analyzer.analyzeAll(classesDirectory);

return coverageBuilder.getBundle(title);
}

6.获取jobName 和 host 的工具类,目前只能识别 B环境和C环境

public class Utils {

public static String getSname(String jobName){
    String[] split = jobName.split("_");

return split[0];
}

public static String getHost(String jobName){
    String[] split = jobName.split("_");

if(“b”.equalsIgnoreCase(split[1])){
return “10.1.1.37”;
}else {
return “10.1.1.36”;
}
}
7.生成报告目录

在Jenkins的docker中,/var/jenkins_home/workspace/KaTeX parse error: Expected group after '_' at position 10: {service}_̲{env}/${service}-service/report

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值