Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info
iconfalse
titleTable of Contents

Table of Contents
maxLevel1

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

Installation

Add these the following maven dependencies to your pom:

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

  <!-- it'sIt is recommended to use the same version thanas the requested sonar server -->
  <version>2.11</version>
</dependency>
<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
</dependency>

...

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:

...

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

Using the client

Create the client

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

...

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 Struts :

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 resources. See Javadoc for more information.

Project Sample

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