3

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.

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);
    }
  }

}
user2215139
  • 1,805
  • 2
  • 18
  • 24

2 Answers2

3

There are 2 things you can do from a Java program:

  • launch a Sonar analysis: look into the Sonar Ant Task (in method #launchAnalysis) to see how to do that very easily.

  • retrieve results from the Sonar server: check the Web API for that purpose

  • may i know which are the dependencies i require for this. Dependencies in ""web service Client API" mentioned above are not downloading to my maven repo – user2215139 May 06 '13 at 11:34
  • You probably forgot to add a version to the dependency (this is not specified in the documentation as the versions change). You can use version 3.5.1. – Fabrice - SonarSource Team May 06 '13 at 12:56
  • ya i have added sonar-ws-client 3.5.1 and commons-httpclient 3.1 as external jars. and written a program as updated above but getting exceptionlike " java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase at com.vxl.appanalytix.controllers.controllerClasses.SonarTask1.main(SonarTask1.java:17) Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpRequestBase. – user2215139 May 07 '13 at 04:28
  • may i know the reason. Whether have to add any other jars or dependencies – user2215139 May 07 '13 at 04:31
  • What is the projectKey used ? Is it the project name ? – Abhishek Dec 11 '13 at 12:40
  • The link to the Web Service Client API @ codehaus is defunct.Are there alternative sites? I'm having trouble finding them. Also is 5.1 the latest version or is there a more recent version? – mister270 Sep 12 '16 at 17:15
  • I've updated both links. To know the latest version of SonarQube, check http://www.sonarqube.org – Fabrice - SonarSource Team Sep 14 '16 at 07:49
-1

About the " java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase" you need this dependencies (Sonar WS uses them)

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.1</version>
</dependency>

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>


<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.7</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.6.2</version>
</dependency>

Also, I'd recommend when using Eclipse IDE, update maven project and check force update of snapshots/releases, because somehow org.apache.httpclient wasn't being able to be recognized by classpath

jam
  • 3,640
  • 5
  • 34
  • 50