importorg.apache.maven.plugin.MojoExecutionException;importorg.jacoco.core.tools.ExecFileLoader;importjava.io.File;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;public classMergeDump {private finalString path ;private finalFile destFile ;publicMergeDump(String path){this.path =path;this.destFile = new File(path + "/jacoco.exec");
}private ListfileSets(String dir){
System.out.println(dir);
List fileSetList = new ArrayList();
File path= newFile(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());
}
}returnfileSetList;
}public void executeMerge() throwsMojoExecutionException {final ExecFileLoader loader = newExecFileLoader();
load(loader);
save(loader);//执行完成后,删除非必须的dump文件
for (final File fileSet : fileSets(this.path)) {if ( ! fileSet.getName().equals("jacoco.exec") ) {
fileSet.delete();
}
}
}/*** 加载dump文件
*@paramloader
*@throwsMojoExecutionException*/
public void load(final ExecFileLoader loader) throwsMojoExecutionException {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 (finalIOException e) {throw new MojoExecutionException("Unable to read "
+inputFile.getAbsolutePath(), e);
}
}
}/*** 执行合并文件
*@paramloader
*@throwsMojoExecutionException*/
public void save(final ExecFileLoader loader) throwsMojoExecutionException {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 (finalIOException e) {throw new MojoExecutionException("Unable to write merged file "
+ this.destFile.getAbsolutePath(), e);
}
}
}