230

I am trying to exclude a directory from being analyzed by Sonar. I have the following properties defined in my sonar-project.properties file:

sonar.sources=src/java
sonar.exclusions=src/java/test/****/*.java

The directory structure I have is:

src/java/dig
src/java/test/dig

When I run the sonar-runner I get the following info:

 INFO  - Excluded sources:
 INFO  -   src/java/test/**/*.java
 INFO  - Excluded tests:
 INFO  -   **/package-info.java

But when I check the result of the analysis all the packages inside the test directory are still there.

I just need to tell Sonar to not analyze the test directory and any packages inside it.

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
user1982350
  • 2,441
  • 2
  • 14
  • 11

16 Answers16

195

Try something like this:

sonar.exclusions=src/java/test/**
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Juan Hernandez
  • 1,961
  • 1
  • 10
  • 2
  • 5
    Assure to use the Ant fileglob pattern for wildcards. Here is a good explanation: http://stackoverflow.com/a/86915/748524 – Stephan Feb 09 '16 at 09:25
  • Here is command line options definition https://docs.sonarqube.org/display/SONAR/Analysis+Parameters#AnalysisParameters-Exclusions/Inclusions – N0dGrand87 Jul 28 '17 at 12:31
  • 8
    Anyone who sees this, make sure you have `src/test/java` instead of `src/java/test/`. – Faraz Dec 03 '18 at 14:06
  • How to exclude methods in c# – nature vetri Dec 05 '18 at 07:06
  • Its important to note that this isn't treated as a regular expression and applied across your entire project `sonar.exclusions=node_modules/**` will NOT exclude nested node_modules folders you will need to do `sonar.exclusions=node_modules/**, /some/path/to/nested/node_modules/**` to explicitly exclude them – Christian Bartram Feb 13 '19 at 17:14
  • 1
    The folder structure also matters : for example : **/webapp/**/* will exclude all files under webapp directory. The "**" is for directory and "*" is for file. If you just have **/webapp/* then it won't exclude the folders under webapp folder. – Smart Coder Jun 26 '20 at 15:52
127

I'm able to exclude multiple directories using the below config (comma separated folder paths):

sonar.exclusions=system/**, test/**, application/third_party/**, application/logs/**

And while running the sonar runner I got the following in the log:

Excluded sources: 
  system/**
  test/**
  application/third_party/**
  application/logs/**
Saikat
  • 14,222
  • 20
  • 104
  • 125
Damodaran
  • 10,882
  • 10
  • 60
  • 81
  • 36
    I get the excluded message too, but files are still analyzed. – Sebas Dec 01 '16 at 10:17
  • in Teamcity parameters I used your tip, but inserting -D before (-Dsonar.exclu...). Works here. :-) – Maciel Escudero Bombonato Jan 04 '18 at 18:30
  • How to exclude all *.html files from scanning here? Tried this `sonar.exclusions=**/bin/**,*.html` but it still takes html files also in the scanning. Html files are all sub folders in several places and adding in sub folder level here with actual path map will be more work. Is there a way we can exclude all *.specific_file_type from getting scanned? – Elamurugan Apr 28 '20 at 05:54
  • It works! you need to make sure that the path and wild char is used correctly – Waqar Jul 28 '22 at 16:40
28

This will work for your case:

sonar.exclusions=**/src/java/dig/ ** , **/src/java/test/dig/ **
Dherik
  • 17,757
  • 11
  • 115
  • 164
20

If we want to skip the entire folder following can be used:

sonar.exclusions=folderName/**/*

And if we have only one particular file just give the complete path.

All the folder which needs to be exclude and be appended here.

Dherik
  • 17,757
  • 11
  • 115
  • 164
Shradha Modi
  • 201
  • 2
  • 4
  • 3
    What is the difference between that answer and `sonar.exclusions=folderName` and `sonar.exclusions=folderName/**` ? Thanks – payne Nov 25 '19 at 19:35
  • 1
    * - Match zero or more characters ** - Match zero or more directories ? - Match a single character – Keerthi Bandara Jun 03 '20 at 06:10
19

Another configuration option is adding a maven properties sonar.exclusions. Below is a sample pom file with exclusions of static jquery directory and static pdf viewer directory.

<project >
<modelVersion>4.0.0</modelVersion>
<artifactId>my Artifact</artifactId>
<!-- Enviroment variables can be referenced as such: ${env.PATH} -->
<packaging>war</packaging>
<url>http://maven.apache.org</url>

<properties>

    <junit.version>4.9</junit.version>
    <mockito.version>1.9.5</mockito.version>
    <jackson.version>1.9.7</jackson.version>
    <powermock.version>1.5</powermock.version>

    <!--Exclude the files Here-->
    <sonar.exclusions>src/main/webapp/static/jquery_ui/*,src/main/webapp/static/pdf-viewer/*,src/main/webapp/static/pdf-viewer/**,src/main/webapp/static/pdf-viewer/**/*</sonar.exclusions>
</properties>

Akin Okegbile
  • 1,108
  • 19
  • 36
7

If you're an Azure DevOps user looking for both where and how to exclude files and folders, here ya go:

  1. Edit your pipeline
  2. Make sure you have the "Prepare analysis on SonarQube" task added. You'll need to look elsewhere if you need help configuring this. Suggestion: Use the UI pipeline editor vs the yaml editor if you are missing the manage link. At present, there is no way to convert to UI from yaml. Just recreate the pipeline. If using git, you can delete the yaml from the root of your repo.
  3. Under the 'Advanced' section of the "Prepare analysis on SonarQube" task, you can add exclusions. See advice given by others for specific exclusion formats.

Example:

# Additional properties that will be passed to the scanner, 
# Put one key=value per line, example:
# sonar.exclusions=**/*.bin
sonar.exclusions=MyProjectName/MyWebContentFolder/**

Note: If you're not sure on the path, you can go into sonarqube, view your project, look at all or new 'Code Smells' and the path you need is listed above each grouping of issues. You can grab the full path to a file or use wilds like these examples:

  • MyProjectName/MyCodeFile.cs
  • MyProjectName/**

If you don't have the 'Run Code Analysis' task added, do that and place it somewhere after the 'Build solution **/*.sln' task.

Save and Queue and then check out your sonarqube server to see if the exclusions worked.

GrayDwarf
  • 2,469
  • 2
  • 20
  • 22
6

Easiest way is to go to the server URL after starting the server(localhost:8080) then login as admin,Go to settings>Exclusions> Source File Exclusions- Add your packages here. Restart the server.

Maverick
  • 238
  • 1
  • 7
  • 18
4

what version of sonar are you using? There is one option called "sonar.skippedModules=yourmodulename".

This will skip the whole module. So be aware of it.

Cypher
  • 242
  • 1
  • 7
  • 20
    WARN - 'sonar.skippedModules' property is deprecated since version 4.3 and should not be used anymore. – Zheng Kai Aug 01 '14 at 04:55
4

You can do the same with build.gradle

sonarqube {
properties {
    property "sonar.exclusions", "**/src/java/test/**/*.java"
  }
}

And if you want to exclude more files/directories then:

sonarqube {
properties {
    property "sonar.exclusions", "**/src/java/test/**/*.java, **/src/java/main/**/*.java"
  }
}
Vince D
  • 137
  • 3
  • 14
4

I typed case sensitive and used "" and it worked. Analyze time decreased to 3 minutes from 10.

# Additional properties that will be passed to the scanner, 
# Put one key=value per line, example:
sonar.exclusions=**\Scripts\**\*,**\Content\**\*
ddagsan
  • 1,786
  • 18
  • 21
3

Add comma separated folder paths sonar.exclusions=**/abc/**,**/def/**

This worked in an angular project

dasunse
  • 2,839
  • 1
  • 14
  • 32
1

Just to mention that once you excluded the files from Sonar, do the same for Jacoco plugin:

<configuration>
<excludes>     
<exclude>com/acme/model/persistence/entity/TransactionEntity*</exclude>
<exclude>com/acme/model/persistence/ModelConstants.class</exclude>
</excludes>
</configuration> 
user07
  • 319
  • 2
  • 8
1

add this line to your sonar-project.properties file

ex: sonar.exclusions=src/*.java be careful if you want to exclude a folder and inside the folder there is a file you must first exclude the files or add the files one by one for example imagine there is a folder like below:

src/app.java src/controllers/home.java src/services/test.java

you have to do this: sonar.exclusions=src/app.java,src/controllers/*.java,src/services/*.java

It worked for me

1

You can skip library like this

project(":libABC") {
    apply plugin: 'org.sonarqube'
    sonarqube {
        skipProject = true
    }
}
Qamar
  • 4,959
  • 1
  • 30
  • 49
0

The way it works in the Sonarqube now

-Dsonar.exclusions=Foodzy.Core/wwwroot/font-awesome/**

or In Jenkins

/d:sonar.exclusions=Foodzy.Core/wwwroot/font-awesome/**
Devesh
  • 4,500
  • 1
  • 17
  • 28
-1

This worked for me:

sonar.exclusions=src/**/wwwroot/**/*.js,src/**/wwwroot/**/*.css

It excludes any .js and .css files under any of the sub directories of a folder "wwwroot" appearing as one of the sub directories of the "src" folder (project root).

  • This doesn't seem to be substantially different from other answers. It uses double star wildcards just like many other answers. It doesn't match what the question was trying to exclude. – Stephen Ostermiller Apr 30 '20 at 10:35