-
Notifications
You must be signed in to change notification settings - Fork 2
Home
The Sonar High Level API is an easy to use Java-API for accessing a SonarQube instance and retrieving some resource data from it. Under the hood Sonar-HLA uses the Sonar Webservice-Client. However, users of Sonar-HLA will not get in touch with it. Sonar-HLA tries to hide the complexity of the Sonar WS-API. Currently Sonar-HLA consists of two components:
- The API itself which may be integrated as a dependency into other projects.
- A Maven plugin which uses this API. By putting a plugin over the API, handling of parameters required to Sonar-HLA becomes much easier.
For accessing a SonarQube server and retrieve a list of all projects, first add the Sonar-HLA dependency to your project:
<dependency>
<groupId>com.github.badamowicz</groupId>
<artifactId>sonar-hla</artifactId>
<version>0.3.1</version>
</dependency>
Then, these lines will get all projects:
import java.util.List;
import com.github.badamowicz.sonar.hla.api.HLAMeasure;
import com.github.badamowicz.sonar.hla.api.IProject;
import com.github.badamowicz.sonar.hla.api.ISonarExtractor;
import com.github.badamowicz.sonar.hla.impl.SonarHLAFactory;
public class ExtractProjectsExample {
public void extractProjects() {
ISonarExtractor extractor = null;
List<IProject> projects = null;
extractor = SonarHLAFactory.getExtractor("http://localhost:9000");
projects = extractor.getAllProjects();
for (IProject currentProject : projects) {
for (HLAMeasure currentMeasure : currentProject.getMeasures()) {
// Read all measures and do something with them:
currentProject.getMeasureValue(currentMeasure, false);
}
}
}
}
It's that easy! Of course there are some more options:
- Provide user credentials for accessing SonarQube.
- Retrieve only a single project by specifying its key.
- Retrieve a subset of projects by specifying a pattern which must match projects keys.
Have a look at the Javadocs, it's well documented!
Going a step further, we now want to convert the projects retrieved from SonarQube into a CSV list:
import java.util.Arrays;
import java.util.List;
import com.github.badamowicz.sonar.hla.api.HLAMeasure;
import com.github.badamowicz.sonar.hla.api.IProject;
import com.github.badamowicz.sonar.hla.api.ISonarConverter;
import com.github.badamowicz.sonar.hla.api.ISonarExtractor;
import com.github.badamowicz.sonar.hla.impl.SonarHLAFactory;
public class ExtractProjectsExample {
public void extractProjects() {
ISonarExtractor extractor = null;
ISonarConverter converter = null;
List<IProject> projects = null;
String csvData = null;
extractor = SonarHLAFactory.getExtractor("http://localhost:9000");
converter = SonarHLAFactory.getConverterInstance();
projects = extractor.getAllProjects();
csvData = converter.getCSVData(projects, Arrays.asList(HLAMeasure.values()), true);
// Now do something with CSV...
}
}
Optionally, you may also create a stream object containing the CSV data for further processing. And again, have a look at the Javadocs. They should provide sufficient information. If you feel something is missing, just file an issue.
© 2014 BERND ADAMOWICZ www.bernd-adamowicz.de