缘起:
比较正在开发的branch和master代码的区别,然后统计新增代码覆盖率。
步骤:
a,代码覆盖率是通过分析exec文件来比较的,exec文件是通过ant从服务器中dump下来的,调用jacoco也是通过ant的build.xml文件来实现的,所以,branch需要从ant文件传给jacoco;
b,传给jacoco之后,通过jgit统计变更的代码;
c,在jacoco代码统计的入口处,将不需要统计的行数和类过滤掉;
踩过的坑:
1,莫名其妙的不能不能clone多个branch,最后莫名其妙的好了,至今没搞明白为什么。
command.setBranchesToClone(Arrays.asList("refs/heads/master","refs/heads/"+branch));
2,运行ant的xml文件的时候出现类检查错误,这个是因为加载类的时候会对类进行类检查,不通过的时候就会报这个错。
Type top (current frame, locals[11]) is not assignable to integer 。
这个是jvm虚拟机的机制,可以通过设置jvm的参数来去掉检查。
对于java7而言,需要添加-XX:-UseSplitVerifier(已实践)。
对于java8而言,需要添加-noverify。
ant如何设置jvm参数?下面是官方文档
The Ant wrapper script for Unix will source (read and evaluate) the file ~/.antrc before it does anything. On Windows, the Ant wrapper batch-file invokes %HOME%\antrc_pre.bat at the start and %HOME%\antrc_post.bat at the end. You can use these files, for example, to set/unset environment variables that should only be visible during the execution of Ant. See the next section for examples.
我用的mac电脑,通过添加~/.antrc文件,文件中添加ANT_OPTS="-noverify",设置jvm参数,ant运行.xml文件的时候,会自动加载这个文件设置jvm的参数。
public void setRepository(final String repository) {
this.repository = repository;
}
public void setBranch(final String branch) {
this.branch = branch;
}
public int analyzeAll(final File file) throws IOException {
int count = 0;
if (file.isDirectory()) {
for (final File f : file.listFiles()) {
count += analyzeAll(f);
}
} else {
String filePath=file.getPath();
if(filePath.endsWith(CLASS_SUFFIX)) {
String className = filePath.substring(CLASS_PREFIX.length(), (file.getPath().length() - CLASS_SUFFIX.length()));
//只分析修改或者添加的class
if (isNewClass(className)) {
final InputStream in = new FileInputStream(file);
try {
System.out.println("正在分析的className:" + className);
count += analyzeAll(in, file.getPath());
} finally {
in.close();
}
}
}else {
}
}
return count;
}
return new MethodAnalyzer(stringPool.get(name), stringPool.get(desc),
stringPool.get(signature), probes) {
@Override
public void visitEnd() {
super.visitEnd();
final IMethodCoverage methodCoverage = getCoverage();
if (methodCoverage.getInstructionCounter().getTotalCount() > 0) {
// Only consider methods that actually contain code
final int firstLine = methodCoverage.getFirstLine();
final int lastLine = methodCoverage.getLastLine();
for (int i = firstLine; i <= lastLine; i++) {
//对于更改的类:只分析更改的方法,对于没有更改的方法不进行分析diffLines.size()>0&&diffLines.contains(i)
//对于增加的类:分析所有的diffLines.size()==0
if((diffLines.size()>0&&diffLines.contains(i))||diffLines.size()==0){
coverage.addMethod(methodCoverage,diffLines);
break;
}
}
}
}
};
public void increment(final ISourceNode child,List<Integer> diffLines) {
instructionCounter = instructionCounter.increment(child
.getInstructionCounter());
branchCounter = branchCounter.increment(child.getBranchCounter());
complexityCounter = complexityCounter.increment(child
.getComplexityCounter());
methodCounter = methodCounter.increment(child.getMethodCounter());
classCounter = classCounter.increment(child.getClassCounter());
final int firstLine = child.getFirstLine();
if (firstLine != UNKNOWN_LINE) {
final int lastLine = child.getLastLine();
ensureCapacity(firstLine, lastLine);
for (int i = firstLine; i <= lastLine; i++) {
//只分析增加和修改的line的的覆盖率
//对于更改的类的方法:diffLines.contains(i)如果包含修改的行的话,就分析这行
//对于新增加类的方法diffLines.size()==0分析所有的
if(diffLines.contains(i)||diffLines.size()==0){
final ILine line = child.getLine(i);
incrementLine(line.getInstructionCounter(),
line.getBranchCounter(), i);
}
}
}
}
研究 jacoco 的入口:
ReportGenerator
