Skip to content

Sonar Importer

Category: Importer (takes in SonarQube Instance and outputs cc.json)

The Sonar-Importer generates visualisation data from SonarQube data through an API call to a SonarQube server. This requires that the SonarQube instance is running.

  • All languages that are supported by SonarQube. A full list can be found here.
  • All metrics that are generated by SonarQube. A full list can be found here.
ParameterDescription
URLurl of sonarqube server
PROJECT_IDsonarqube project id
-h, --helpPlease locate:
- sonar.host.url (e.g. https://sonar.foo)
- sonar.login (e.g. c123d456)
- sonar.projectKey (e.g. de.foo:bar)
That you use to upload your code to sonar.
Then execute ccsh sonarimport https://sonar.foo de.foo:bar -u c123d456
-m, --metrics=<metrics>comma-separated list of metrics to import
--merge-modulesmerges modules in multi-module projects
-nc, --not-compressedsave uncompressed output File
-o, --output-file=<outputFile>output File
-u, --user-token=<userToken>user token for connecting to remote sonar instance
Usage: ccsh sonarimport [-h] [--merge-modules] [-nc] [-o=<outputFile>]
[-u=<userToken>] [-m=<metrics>]... URL PROJECT_ID
ccsh sonarimport <url> <projectKey> --user-token=<userToken> --output-file=<fileName> --merge-modules=<Boolean>

If you use SonarQube locally, an example command would look like the following:

ccsh sonarimport "http://localhost:9000/" "CodeCharta" "--user-token=squ_12345" "--output-file=output" "--merge-modules=false"

If a project is piped into the SonarImporter, the results and the piped project are merged. The merged project does not keep the SonarImporter’s project name; during the merge ProjectBuilder sets the project name to an empty (dummy) value.

CodeCharta can import project metrics from SonarQube. SonarQube can generate a wide variety of project metrics for a variety of languages. It can be used for a local analysis, or it can be added to the build pipeline. In the following, the installation and usage of a local sonar server is described.

see also SonarQube Docs

  1. Download the latest version of SonarQube
  2. Unzip the archive (use the chosen location for the next step)
  3. Start the Sonar Server with C:\sonarqube\bin\windows-x86-xx\StartSonar.bat on Windows or /opt/sonarqube/bin/[OS]/sonar.sh on Linux/MacOS
  4. Open localhost:9000 in your browser and log in with
    • login: admin
    • password: admin
  1. Download the latest version of SonarScanner
  2. Unzip the archive
  3. Add the bin directory to the %PATH%(Windows) or PATH(Linux/MacOS) environment variable

You can also run SonarQube with Docker, for further information read here.

  1. Download and install Docker Desktop if not already present on your system
  2. Download the latest version of SonarQube Docker Image
  3. Start the Sonar Server with the following command:

    $ docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest

  4. Open localhost:9000 in your browser and log in with
    • login: admin
    • password: admin

Also see for detailed instructions here.

  1. Download the latest version of SonarScanner CLI
  2. Add a sonar-project.properties file to your project’s root directory

After you have installed SonarQube, do the following steps to analyze your project:

  1. Click on the plus icon to create a new project
  2. Specify the project key and display name
  3. Click the respective button to generate a token
  4. Specify the main language for the project
  5. Follow the instructions from SonarQube

When using SonarQube version >= 9.5, you need to create a user token. In your account settings select the security settings.

Generate user token

For further information read here

To visualize your project metrics created by Sonar, use CodeCharta’s sonar importer to generate a cc.json file.