Configure private project with SonarQube.

Author
By Darío Rivera
Posted On in Static Code Analysis

If you have a private project you can configure the SonarQube analyzer (sonar-scanner) to generate static code analysis of your project and upload it to your dedicated server. If you haven't installed any of these components yet, I invite you to check out the following posts:

- SonarQube Installation
- SonarScanner Installation

Configuration

Once you have SonarScanner installed and can run it from your console, the next step will be to create the SonarScanner configuration file for the project. To do this, you must create a file called sonar-project.properties and add the following content which you can modify to your liking.

# must be unique in a given SonarQube instance
sonar.projectKey=PROJECT-KEY

# --- optional properties ---

# defaults to project key
#sonar.projectName=
# defaults to 'not provided'
#sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Defaults to .
sonar.sources=./src

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

# SonarQube host
sonar.host.url=https://sonarqube.domain.com

# Clover XML-format coverage report files
sonar.php.coverage.reportPaths=phpunit.coverage.xml

# SonarQubeube token
sonar.login=XXXXXXXXXXXXXXXXXXXXXXXXXXX

You may be wondering where all this information comes from. When you create a project from the SonarQube administrative panel, you will get the projectKey along with the login token. This key and login will help SonarQube to enter the report in the respective project on your server. The analyzer will automatically upload the report to the server you have configured in sonar.host.url. I have included some optional properties that you can fill in at your discretion according to the name of the project and its version.

On the other hand, sonar.sources refers to the code folder that sonar-scanner will analyze. If your code is encoded in UTF-8, you won't have trouble with sourceEncoding.

Finally, you should keep in mind that if you want to add your project's coverage to the report, you must configure the sonar.php.coverage.reportPaths property. If your project is in a language other than PHP, you should consult the corresponding property in the official documentation and generate this file in XML with the testing framework you use.

After this, you can calmly run the following command in the root of your project to run the analyzer and have it automatically upload the report to your SonarQube server.

sonar-scanner

If instead of configuring your own SonarQube server you want to use the SonarCloud cloud service, you can visit the following post:

Configure Project in SonarCloud


Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.