緣起:
比較正在開發的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