2021-05-12 14:32:11
使用sonar-ws-client獲取違規數、程式碼行數
sonar是開源的品質管理工具。 違規數、程式碼行數是sonar品質度量(Measure)的兩個指標(Metric)。 sonar-ws-client是sonar webservice的java實現。 使用sonar-ws-client獲取違規數、程式碼行數。
demo如下:
public class SonarDemo {
static String host = "http://xxx:9000";
static String username = "xxx";
static String password = "xxx";
static String resourceKey = "org.codehaus.sonar:sonar-ws-client";
static String[] MEASURES_TO_GET = new String[] { "violations", "lines" };
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#.##");
//建立Sonar
Sonar sonar = new Sonar(new HttpClient4Connector(new Host(host, username, password)));
//執行資源請求
ResourceQuery query = ResourceQuery.createForMetrics(resourceKey, MEASURES_TO_GET);
query.setIncludeTrends(true);
Resource resource = sonar.find(query);
// 迴圈遍歷獲取"violations", "lines"
List<Measure> allMeasures = resource.getMeasures();
for (Measure measure : allMeasures) {
System.out.println((measure.getMetricKey() + ": " +
df.format(measure.getValue())));
}
}
}
pom檔案dependency如下:
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-ws-client</artifactId>
<!-- 推薦使用和SonarQube server相同的版本-->
<version>4.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>
更多sonar metric請檢視官方文件:http://docs.codehaus.org/display/SONAR/Metric+definitions
Sonar 的詳細介紹:請點這裡
Sonar 的下載地址:請點這裡
本文永久更新連結地址:http://www.linuxidc.com/Linux/2015-07/120308.htm
相關文章