Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info
iconfalse
titleTable of Contents

Table of Contents
maxLevel1

The library sonar-ws-client is intented to help Java developers to use the SonarQube web services.

Installation

Add the following maven dependencies to your pom:

Code Block
xml
xml
<dependency>
  <groupId>org.codehaus.sonar</groupId>
  <artifactId>sonar-ws-client</artifactId>

  <!-- It is recommended to use the same version as the requested SonarQube server -->
  <version>2.11</version>
</dependency>
<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>

Be careful, Commons HTTPClient depends on commons-logging. If you use SLF4J, you should replace commons-logging by jcl-over-slf4j:

Code Block
xml
xml
<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
  <exclusions>
    <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
</dependency>

Using HttpClient 4

It's possible to use the version 4 of Commons HTTPClient instead of version 3.1:

Code Block
xml
xml
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.1.3</version>
</dependency>

But be careful of the critical bug HTTPCLIENT-1140, use version  >4.1.2. It can end up in an infinite loop. 

Using Client

Creating Client

Code Block
java
java
import org.sonar.wsclient.Sonar;
Sonar sonar = Sonar.create("http://localhost:9000");

If the security is enabled, you have to define the credentials:

Code Block
java
java
Sonar sonar = Sonar.create("http://localhost:9000", "login", "password");

When using the lib Commons HttpClient 4.0, the constructor is slightly different:

Code Block
Sonar sonar = new Sonar(new HttpClient4Connector(new Host("http://localhost:9000")));

Executing Requests

For example to the get the code coverage of the 'Struts' project:

Code Block
java
java
Resource struts = sonar.find(ResourceQuery.createForMetrics("org.apache.struts:struts-parent", "coverage", "lines", "violations"));
struts.getMeasure("coverage");

The methods find() and findAll() accept queries to get measures, code source, rule violations, configuration, metric definitions and dependencies between components. See Javadoc for more information.

Project Sample

A sample project is available on github that can be browsed or downloaded: ws/manual-measure-ws