我正在尝试排除一个目录从声纳分析。我在我的声纳项目中定义了以下属性。属性文件:

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

我的目录结构是:

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

当我运行声纳运行,我得到以下信息:

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

但是当我检查分析结果时,测试目录中的所有包仍然存在。

我只需要告诉声纳不要分析测试目录和它里面的任何包。


当前回答

如果你是一个Azure DevOps用户,正在寻找在哪里以及如何排除文件和文件夹,这里你可以去:

Edit your pipeline 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. Under the 'Advanced' section of the "Prepare analysis on SonarQube" task, you can add exclusions. See advice given by others for specific exclusion formats.

例子:

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

注意:如果你不确定路径,你可以进入sonarqube,查看你的项目,查看所有或新的“Code Smells”,你需要的路径在每个问题组上面列出。你可以获取一个文件的完整路径,或者像下面这样使用野字符:

MyProjectName / MyCodeFile cs MyProjectName / * *

如果你没有添加“运行代码分析”任务,那么就把它放在“构建解决方案**/*”之后的某个地方。sln的任务。

保存并排队,然后检查sonarqube服务器,看看排除是否有效。

其他回答

另一个配置选项是添加maven属性sonar.exclusions。下面是一个pom文件样本,不包括静态jquery目录和静态pdf查看器目录。

<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>

我能够排除多个目录使用下面的配置(逗号分隔文件夹路径):

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

在运行声纳运行器时,我在日志中得到了以下内容:

Excluded sources: 
  system/**
  test/**
  application/third_party/**
  application/logs/**

这招对我很管用:

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

它排除了“wwwroot”文件夹的任何子目录下的任何。js和。css文件,作为“src”文件夹(项目根)的子目录之一出现。

你可以用build.gradle做同样的事情

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

如果你想排除更多的文件/目录,那么:

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

将这一行添加到声纳项目中。属性文件

例:sonar.exclusions = src / * . java 如果要排除一个文件夹,并且该文件夹中有一个文件,则必须先排除这些文件或逐个添加文件 例如,假设有一个如下所示的文件夹:

src / java应用程序。 src /控制器/ home . java src /服务/ test . java

你必须这样做: sonar.exclusions = src / app.java src /控制器/ * . java, src /服务/ * . java

这对我很有效