Skip to end of metadata
Go to start of metadata

Alternatives to 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:

    sonar-scanner -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 and sonar.projectBaseDir properties). Ex:

    sonar-scanner -Dproject.settings=../myproject.properties

      

The root folder of the project to analyze can be set through the sonar.projectBaseDir property since SonarQube Scanner 2.4 (was previously project.home). 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. 

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.

sonar-project.properties
sonar.projectBaseDir=/home/ftpdrop/cobol/project1
sonar.sources=src
sonar.cobol.copy.directories=/copy

For more, see the listing of analysis parameters.

Multi-module Project Structure

If your project is a Maven project, you have nothing further to do to enable multi-module analysis. Simply execute mvn sonar:sonar from your project's root directory. If your multi-module project is not a Maven project, read on:

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

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

Way #1

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

"MyProject/sonar-project.properties" file content
# Root project information
sonar.projectKey=org.mycompany.myproject
sonar.projectName=My Project
sonar.projectVersion=1.0

# 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

"MyProject/sonar-project.properties" file content
# Root project information
sonar.projectKey=org.mycompany.myproject
sonar.projectName=My Project
sonar.projectVersion=1.0

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

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

 

Noteworthy:

  • 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 how to redefine the base directory of the modules in each:
    • the folder of a module contains white spaces or special characters:

      module1.sonar.projectBaseDir=My Module One

        

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

      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.
     
  • No labels