我已经编写了代码,但它返回项目的平均功能级别指标。 需要修改代码以通过使用 Sonar Api 传递函数名称/方法名称来返回函数级别指标,例如循环复杂度
import java.util.List;
import org.sonar.wsclient.Sonar;
import org.sonar.wsclient.services.Measure;
import org.sonar.wsclient.services.ResourceQuery;
public class SonarApi_1 {
static String resourceKey = "Project_Name";
//Measures which will be returned
static String[] MEASURES_TO_GET = new String[]{"file_complexity", "class_complexity","function_complexity","lcom4","complexity","ncloc","functions","files","classes","packages"};
public static Sonar localSonar;
public static void main(String[] args)
{
try {
// To Create Connection Using Resource Key which is Project name , user Name and Password
localSonar = Sonar.create("http://illin018:8000", "User_name", "Password");
ResourceQuery query = ResourceQuery.createForMetrics(resourceKey, MEASURES_TO_GET);
query.setIncludeTrends(true);
Object resource = localSonar.find(query);
//To get measures
List<Measure> allMeasures = ((org.sonar.wsclient.services.Resource) resource).getMeasures();
for (Measure measure : allMeasures) {
System.out.println("Statements : " + measure.getMetricKey()
+ " === " + measure.getFormattedValue());
}
System.out.println("lcom4 means Lack of Cohesion of Functions");
System.out.println("ncloc means Non Commenting Lines of Code");
} catch(Exception e){
e.printStackTrace();
}
}
}
O/p
Statements : ncloc === 129,919
Statements : classes === 823
Statements : files === 821
Statements : packages === 86
Statements : functions === 5,205
Statements : complexity === 25,393
Statements : class_complexity === 30.9
Statements : function_complexity === 4.9
Statements : file_complexity === 30.9
Statements : lcom4 === 1.1
lcom4 means Lack of Cohesion of Functions
ncloc means Non Commenting Lines of Code
最佳答案
SonarQube 不存储类和方法的指标,而仅存储文件、目录、模块和项目的指标。我们开发了一个自定义插件,可以为您提供这些指标,名为 SourceMeter plugin for SonarQube 。目前支持的 SonarQube 版本为 v3.7.4、v4.0 和 v4.1。但我们几乎完成了新版本的插件(版本 6.0),它支持 SQ 4.2 到 4.3.2。您可以找到在线演示here .
更新:
如果您想通过Web Service API获取类或方法级别的指标,那么您应该调用 setQualifiers() ResourceQuery 对象的方法。对于 JAVA 类,方法的限定符是“MET”和“JavaClass”。您可以在浏览器中尝试,只需将 URL 栏更改为 {localhost:9000}/api/resources?metrics=ncloc&qualifiers=MET
即可。例如,在我们的在线演示中:http://sonarqube.frontendart.com/api/resources?resource=506268&depth=-1&metrics=ncloc&qualifiers=MET
关于java - 下面的代码需要进行哪些修改才能通过使用 Sonar Api 传递函数名称来返回函数级别指标(例如循环复杂度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24628423/