Versions Compared

Key

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

Table of Contents
maxLevel2

The Sonar SonarQube Runner is recommended as the default launcher to analyze a project with SonarSonarQube.

Prerequisites

You must have previously installed the Sonar SonarQube Runner and read Analyzing Code Source.

...

titleSonar Runner 2.0+

...

You must also have the language plugins installed for each of the languages you wish analyzed in your projects. 

Usage

Simple Project

Create a configuration file in the root directory of the project: sonar-project.properties

Code Block
titlesonar-project.properties
languagebash
# requiredRequired metadata
sonar.projectKey=my:project
sonar.projectName=My project
sonar.projectVersion=1.0

# optionalPath description
sonar.projectDescription=Fake description

# path to source directories (required)
sonar.sources=srcDir1,srcDir2

# path to test source directories (optional)
sonar.tests=testDir1,testDir2

# path to project binaries (optional), for example directory of Java bytecode
sonar.binaries=binDir

# optional comma-separated list of paths to libraries. Only path to JAR file and path to directory of classes are supported.
sonar.libraries=path/to/library/*.jar,path/to/classes/dir,path/to/specific/library/myLibrary.jar,parent/*/*.jar

# The value of the property must be the key of the language.
sonar.language=cobolto the parent source code directory.
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set. 
# If not set, SonarQube starts looking for source code from the directory containing 
# the sonar-project.properties file.
sonar.sources=src

# Encoding of the source code
sonar.sourceEncoding=UTF-8

# Additional parameters
sonar.my.property=value

Run the following command from the project base directory to launch the Sonar analysis:

Code Block
languagenone
sonar-runner

Security

Since Sonar 3.4, if a project cannot be accessed anonymously, the 'sonar.login' and 'sonar.password' properties are required to run an analysis on this project. These properties have to be set to the credentials of a user having the 'User' role on this project. You can set them either:

  • directly on the command line by adding -Dsonar.login=myUser -Dsonar.password=myPassword
  • or in the 'sonar-project.properties' project configuration file
  • or in the 'sonar-runner.properties' global configuration file

A project cannot be anonymously accessed when either:

Project Samples

To help you getting started, for each language, a simple project sample is available on github that can be browsed or downloaded: projects/languages

...

Include Page
Include - Analysis - Security
Include - Analysis - Security

Project Samples

To help you get started, simple project samples are available for most languages on github. They can be browsed or downloaded. You'll find them filed under projects/languages.

Multi-language Project

Since SonarQube 4.2, it is possible to run an analysis on a multi-language project. To do so, the sonar.language property just has to be removed. Conversely, if for some reason you want to perform a single language-only analysis, make sure sonar.language is specified.

To help you get started, a multi-language project sample can be browsed or downloaded from github: projects/languages/multi-language/multi-language-java-javascript-sonar-runner

Include Page
Include - Converting a Mono-language Project to a Multi-language Project
Include - Converting a Mono-language Project to a Multi-language Project

Multi-module Project

There are two ways to define a multi-module structure in SonarSonarQube:

 Using the given file structure...... with the given 'properties' files

Way #1

Set all the configuration in the properties file in the root folder

Code Block
title"MyProject/sonar-project.properties" file content
linenumberstrue
languagebash
# Root project information
sonar.projectKey=org.mycompany.myproject
sonar.projectName=My Project
sonar.projectVersion=1.0-SNAPSHOT

# Some properties that will be inherited by the modules
sonar.sources=src

# List of the module identifiers
sonar.modules=module1,module2

# Properties can obviously be overriden for
# each module - just prefix them with the module ID
module1.sonar.projectName=Module 1
module2.sonar.projectName=Module 2

Way #2

Set the configuration in multiple properties files

Code Block
title"MyProject/sonar-project.properties" file content
linenumberstrue
languagebash
# Root project information
sonar.projectKey=org.mycompany.myproject
sonar.projectName=My Project
sonar.projectVersion=1.0-SNAPSHOT

# Some properties that will be inherited by the modules
sonar.sources=src

# List of the module identifiers
sonar.modules=module1,module2
Code Block
title"MyProject/module1/sonar-project.properties" file content
linenumberstrue
languagebash
# Redefine properties
# Note that you do not need to prefix the property here
sonar.projectName=Module 1
Code Block
title"MyProject/module2/sonar-project.properties" file content
linenumberstrue
languagebash
# Redefine properties
# Note that you do not need to prefix the property here
sonar.projectName=Module 2

 

Noteworthy:

  • Properties are inherited from parent to children 
    They can obviously Children inherit their parent's properties 
    Inherited properties can be overriden:
    • By prefixing them with the module identifier (way #1)
    • Simply by defining them in the 'sonar-project.properties' file located in the module (way #2)

  • Module base directory can be specified for special cases
    By default, the module base directory is guessed from the module identifier (like in the examples above). But it can be redefined using the 'sonar.projectBaseDir' property.
    For instance, here are two use cases and the way how to redefine the base directory of the modules in each:
    • the folder of a module contains white spaces or special characters:

      Code Block
      languagebash
      module1.sonar.projectBaseDir=My Module One

        

    • the module is not located directly in the parent folder, but in a deeper directory structure:

      Code Block
      languagebash
      module1.sonar.projectBaseDir=modules/mod1
      module2.sonar.projectBaseDir=modules/mod2

        

  • A project that defines modules (or a module that defines sub-modules) cannot define a source code folder to be analyzed by Sonar.
     

To help you getting get started, multi-module project samples are available on github that can be browsed or downloaded from github:

  • Modules with the same structure: projects/multi-module/sonar-runner/java-sonar-runner-modules-same-structure
  • Modules with different structures: projects/multi-module/sonar-runner/java-sonar-runner-modules-different-structures
  • A configuration file for each module: projects/multi-module/sonar-runner/java-sonar-runner-modules-own-configuration-file

Multi-module and Multi-language Project

Since Sonar 3.3, it is possible to run an analysis on a multi-module project whose modules contains source code from different languages.

In addition to the multi-module configuration, the only mandatory property to set is the language for each module:

Code Block
titlemodule.sonar.language
linenumberstrue
languagebash
module1.sonar.language=java
module2.sonar.language=javascript
...

To help you getting started, a multi-language project sample is available on github that can be browsed or downloaded: projects/languages/multi-language/multi-language-java-javascript-sonar-runner

Advanced Usage

Running Other Tasks

Code Block
languagebash
# To run the computation of views (Views plugin is required)
sonar-runner views

# To run the computation of reports (Report plugin is required)
sonar-runner report

# To run the computation of developer data (Developer Cockpit plugin is required)
sonar-runner devcockpit

Advanced Usage

sonar-project.properties

If a sonar-project.properties file cannot be created in the root directory of the project, there are several alternatives:

  • The properties can be specified directly through the command line. Ex:

    Code Block
    languagenone
    sonar-runner -Dsonar.projectKey=myproject -Dsonar.sources=src1

      

  • The property 'project.settings' can be used to specify the path to the project configuration file (this option is incompatible with the 'project.home' property and sonar.projectBaseDir properties). Ex:

    Code Block
    languagenone
    sonar-runner -Dproject.settings=../myproject.properties

      

The root folder of the project to analyze can be set through the ' sonar.projectBaseDir property since SonarQube Runner 2.4 (was previously project.home' property). This folder must contain a sonar-project.properties file if the mandatory properties (like 'sonar.projectKey') are not specified on the command line.

Additional analysis parameters can be defined in this project configuration file or through command-line parameters. 

Troubleshootings

Alternate Analysis Directory

If the files to be analyzed are not in the directory where the analysis starts from, use the sonar.projectBaseDir property to move analysis to a different directory. E.G. analysis begins from jenkins/jobs/myjob/workspace but the files to be analyzed are in ftpdrop/cobol/project1.

Code Block
titlesonar-project.properties
languagebash
sonar.project.BaseDir=/home/ftpdrop/cobol/project1
sonar.sources=src
sonar.cobol.copy.directories=/copy

For more, see the listing of analysis parameters.

Troubleshooting

Note

If you get an a Java heap space error or java.lang.OutOfMemoryError, you can set increase the memory via the SONAR_RUNNER_OPTS environment variable, like this in *nix environments:

Code Block
languagenone
export SONAR_RUNNER_OPTS="-Xmx512m -XX:MaxPermSize=128m"

On Windows environments, avoid the double-quotes, since they get misinterpreted , turning and combine the two parameters into a single one.

Code Block
languagenone
set SONAR_RUNNER_OPTS=-Xmx512m -XX:MaxPermSize=128m

Migrating from Sonar Runner 1.X to Sonar Runner 2.0

Replace the following properties in the sonar-project.properties file:

  • sources => sonar.sources
  • tests => sonar.tests
  • binaries => sonar.binaries
  • libraries => sonar.libraries
Explicitly set the sonar.sourceEncoding property in the sonar-project.properties file.