-
Write a violation rules file;
-
Import it into Sonar as a Quality Profile named as "MyRules";
-
Add the following properties to ant script:
-
Run Ant script, and you can get the report at $PROJECT_HOME/.sonar/checkstyle-result.xml;
-
Use the following python script to extract data from the report:
from xml.etree import ElementTree as ET tree = ET.parse('/path/to/checkstyle-result.xml') root = tree.getroot() cycleCnt = 0 nestCnt = 0 for error in root.iter('error'): msg = error.get('message') if msg.startswith('Cyclomatic'): cycleCnt += 1 if msg.startswith('Nested'): nestCnt += 1 print "Cyclomatic:",cycleCnt,"Nested:",nestCnt
See "Section 19.7: xml.etree.ElementTree" of documentation of Python 2.7.5 for details.