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