从我的java程序中调用Sonar [英] Calling Sonar from my java program
问题描述
我已经在我的localhost上安装了声纳服务器。我能够运行和分析java项目。甚至我已经在eclipse上安装了声纳插件。
I have installed sonar server on my localhost. And I am able to run and analyse the java project. Even i have installed sonar plugin on eclipse.
但是我想从我的java项目(如简单的java类)中运行声纳,并且应该检索声纳结果并能够将其保存在数据库中。我搜索了教程但无法找到答案。请任何人都可以提供示例代码或资源,以便我可以获得知识来克服此任务。
But I want to run sonar from my java project(like simple java class) and should retrieve the sonar results and able to save it in database. I searched for the tutorial but unable to find the answer for this. Please anyone can give sample code or resource where I can gain knowledge to overcome this task.
import javax.annotation.Resource;
import org.sonar.wsclient.Host;
import org.sonar.wsclient.Sonar;
import org.sonar.wsclient.connectors.HttpClient4Connector;
import org.sonar.wsclient.services.*;
public class SonarTask1{
public static void main(String[] args) {
//public void Hi(){
String url = "http://localhost:9000";
String login = "admin";
String password = "admin";
Sonar sonar = new Sonar(new HttpClient4Connector(new Host(url, login, password)));
String projectKey = "java-sonar-runner-simple";
String manualMetricKey = "burned_budget";
sonar.create(ManualMeasureCreateQuery.create(projectKey, manualMetricKey).setValue(50.0));
for (ManualMeasure manualMeasure : sonar.findAll(ManualMeasureQuery.create(projectKey))) {
System.out.println("Manual measure on project: " + manualMeasure);
}
}
}
推荐答案
您可以通过Java程序执行以下两项操作:
There are 2 things you can do from a Java program:
-
启动声纳分析:查看 Sonar Ant Task (方法#launchAnalysis)看看如何轻松地做到这一点。
launch a Sonar analysis: look into the Sonar Ant Task (in method #launchAnalysis) to see how to do that very easily.
检索结果来自Sonar服务器:为此目的检查 Web API
retrieve results from the Sonar server: check the Web API for that purpose
这篇关于从我的java程序中调用Sonar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!