从我的 java 程序调用声纳

2022-01-17 00:00:00 sonarqube sonar-runner java

我已经在我的本地主机上安装了声纳服务器.而且我能够运行和分析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 任务(在方法#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

相关文章