How to Exclude Files Or Directories From SonarQube Analysis?

10 minutes read

To exclude files or directories from SonarQube analysis, you can use the SonarQube exclusions mechanism. Here's how:

  1. Open the SonarQube web interface and navigate to your project's dashboard.
  2. Go to "Administration" on the top menu, then select "Analysis Scope" from the left-hand side menu.
  3. In the "Source File Exclusions" section, you can provide patterns to exclude specific files or directories from the analysis. SonarQube uses Ant-style patterns to match file paths. For example, to exclude a specific file: "src/main/java/com/example/MyClass.java". To exclude all files in a directory: "src/main/java/com/example/*". To exclude a specific subdirectory: "src/main/java/com/example/subdir/**/*".
  4. Click the "Bulk Change" button to apply the exclusions.
  5. After saving the exclusions, the next analysis run will exclude the specified files or directories accordingly.


It's important to note that excluding files or directories should be used with caution, as it might introduce blind spots in the analysis. Ensure that you have valid reasons for excluding certain files or directories, such as generated code or third-party libraries.


Additionally, excluding files or directories can also be achieved through specific configuration files, depending on the build tool being used. For example:

  • For Maven projects, you can configure exclusions in the "pom.xml" file using the property.
  • For Gradle projects, you can configure exclusions in the "build.gradle" file using the sonarqube extension's exclusions property.
  • For other build tools, refer to the SonarQube documentation for information on how to configure exclusions using the respective tool.


Remember to regularly review your exclusions to ensure that they remain relevant and necessary for accurate code analysis.

Best Programming Books to Read in 2024

1
Clean Code: A Handbook of Agile Software Craftsmanship

Rating is 5 out of 5

Clean Code: A Handbook of Agile Software Craftsmanship

2
Cracking the Coding Interview: 189 Programming Questions and Solutions

Rating is 4.9 out of 5

Cracking the Coding Interview: 189 Programming Questions and Solutions

3
Game Programming Patterns

Rating is 4.8 out of 5

Game Programming Patterns

4
Beginner's Step-by-Step Coding Course: Learn Computer Programming the Easy Way (DK Complete Courses)

Rating is 4.7 out of 5

Beginner's Step-by-Step Coding Course: Learn Computer Programming the Easy Way (DK Complete Courses)

5
Pragmatic Programmer, The: Your journey to mastery, 20th Anniversary Edition

Rating is 4.6 out of 5

Pragmatic Programmer, The: Your journey to mastery, 20th Anniversary Edition

6
Code: The Hidden Language of Computer Hardware and Software

Rating is 4.5 out of 5

Code: The Hidden Language of Computer Hardware and Software

7
Web Design with HTML, CSS, JavaScript and jQuery Set

Rating is 4.4 out of 5

Web Design with HTML, CSS, JavaScript and jQuery Set

8
Software Engineering at Google: Lessons Learned from Programming Over Time

Rating is 4.3 out of 5

Software Engineering at Google: Lessons Learned from Programming Over Time


How to exclude files from SonarQube analysis?

To exclude files from SonarQube analysis, you can use the following options:

  1. Using the SonarQube UI: Login to your SonarQube server as an administrator. Go to "Project Settings" or "Administration" and select the specific project you want to exclude files from. Navigate to "Analysis Scope" or "Source Code" settings. Look for options like "Inclusions" or "Exclusions" and add the file patterns you want to exclude. For example, **/test/**/* would exclude any files under the "test" directory. Save the changes and trigger a new analysis for the project.
  2. Using the SonarQube analysis parameters: In your SonarQube Scanner configuration file (sonar-project.properties or pom.xml), add the sonar.exclusions property. Assign the file patterns you want to exclude using this property. For example, sonar.exclusions=**/test/**/* would exclude any files under the "test" directory. Save the file and run the SonarQube analysis again.
  3. Using file-based exclusions: Create a file named .sonarignore in the root directory of your project. Add the file patterns you want to exclude in this file, with each pattern on a separate line. For example: **/test/** **/*.html Save the file and run the SonarQube analysis.


Note that these exclusions are project-specific and will only affect the analysis for the specified project. If you want to exclude files globally across all projects, you can set the exclusions in the SonarQube server-level settings.


How to exclude third-party libraries from SonarQube analysis?

To exclude third-party libraries from SonarQube analysis, you can use the "sonar.exclusions" property in your SonarQube project configuration.

  1. Determine the path or pattern of the files you want to exclude. For example, if you have third-party libraries located in a folder named "lib" within your project directory, the pattern may be "/lib/".
  2. Open your SonarQube project configuration file, which is typically named "sonar-project.properties".
  3. Add the "sonar.exclusions" property to the file, specifying the path or pattern to exclude. For example: sonar.exclusions=**/lib/** This will exclude all files and subdirectories within the "lib" folder from the SonarQube analysis.
  4. Save the configuration file and re-run the SonarQube analysis. The excluded files should now be skipped during the analysis process.


Note: It's recommended to exclude third-party libraries from the analysis to avoid skewing the analysis results and focusing on your own code. However, you may still want to analyze the interfaces used by these libraries. In such cases, you can exclude specific files or directories within the third-party library folder by adjusting the exclusion pattern accordingly.


What is the impact of excluding files on maintainability analysis in SonarQube?

Excluding files from analysis in SonarQube can have both positive and negative impacts on maintainability analysis.


Positive Impact:

  1. Improved Performance: By excluding files that are not relevant to the analysis, you can significantly reduce the analysis time and improve the overall performance of SonarQube.
  2. Focus on Critical Areas: Excluding certain files allows developers to focus their attention on the critical and high-risk areas of the codebase. This can be helpful when dealing with large projects with a limited amount of resources.
  3. Reduced Noise: Excluding files that are auto-generated or third-party libraries can help reduce false positives and noise in the analysis reports. This allows developers to concentrate on actionable findings rather than wasting time on irrelevant issues.


Negative Impact:

  1. Hidden Issues: Excluding files might hide potential issues that could cause maintainability problems in the future. It may lead to oversight of potential vulnerabilities or code smells.
  2. Incomplete Analysis: By excluding files, you might miss out on certain code areas that are relevant to the overall maintainability of the project. This can result in incomplete analysis and incomplete insights into the code quality.
  3. Relying on Subjectivity: Deciding which files to exclude requires subjective judgment, and different individuals may have different opinions on what should be included or excluded. This subjectivity might introduce consistency and accuracy issues in the analysis.


It's important to strike a balance between excluding files to improve performance and manageability while ensuring that critical areas and potential issues are not missed out on. Regular review and updates to the excluded file list are recommended to maintain a comprehensive analysis without sacrificing performance.


How to exclude comments from SonarQube analysis?

To exclude comments from SonarQube analysis, you can follow these steps:

  1. Open the SonarQube web interface.
  2. Navigate to your project's dashboard.
  3. Click on "Administration" in the top menu.
  4. Under the "General Settings" section, click on "Analysis Scope".
  5. Scroll down to the "Source File Exclusions" section.
  6. Edit the regular expression under "Exclusions" to exclude comments. You can use a regular expression like (?i)\\.java$ to exclude all Java files, for example.
  7. Click on "Save" to apply the changes.


By excluding comments from the analysis scope, SonarQube will ignore any issues or code smells present in the comments, improving the accuracy of the analysis results. However, keep in mind that excluding comments might not be advisable if your comments also contain important information regarding the code quality or documentation. In such cases, it's better to address any issues found in the comments to maintain a comprehensive analysis.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To configure Maven to run SonarQube, you need to follow these steps:Install SonarQube: Firstly, you need to install SonarQube on your system. You can download it from the official SonarQube website and follow the installation instructions provided. Configure S...
Analyzing a .NET project using SonarQube involves the following steps:Install SonarQube: Download and install SonarQube on your machine. It requires Java to be installed as well. Set up a SonarQube server: Start the SonarQube server by running the appropriate ...
Analyzing a Maven project with SonarQube involves several steps to ensure comprehensive code analysis and reporting. Here is an overview of the process:Prerequisites: Before starting the analysis, you need to have SonarQube installed and running on your system...