简体   繁体   English

配置 Sonar 从 Maven pom.xml 中排除文件

[英]Configure Sonar to exclude files from Maven pom.xml

I have a project configured in maven and the code analysis is done by SonarQube.我在maven配置了一个项目,代码分析由SonarQube完成。

I am trying to configure SonarQube in the pom.xml file to exclude a few files from the code analysis.我正在尝试在 pom.xml 文件中配置 SonarQube 以从代码分析中排除一些文件。 Those files can be identified by their class names, they contain the underscore character before the extension (they are metamodel classes).这些文件可以通过它们的 class 名称来识别,它们在扩展名前包含下划线字符(它们是元模型类)。 Below I give the part of the pom.xml file where I try to exclude them:下面我给出了 pom.xml 文件中我尝试排除它们的部分:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sonar-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <sonar.sources>src/main/java</sonar.sources>
        <sonar.exclusions>file:**/src/main/java/**/*_.*</sonar.exclusions>
    </configuration>
</plugin>

However, the above code does not work.但是,上面的代码不起作用。 Is there a way to configure SonarQube from my pom.xml file to ignore those files when analysing the source code?有没有办法从我的 pom.xml 文件配置 SonarQube 以在分析源代码时忽略这些文件?

Thank you in advance.先感谢您。

Sonar exclusions (like other sonar properties) have to be added to the <properties> section of the POM file.声纳排除(与其他声纳属性一样) 必须添加到POM 文件<properties>部分 Like so (example from excluding jOOQ autogenerated code from current project):像这样(例如从当前项目中排除 jOOQ 自动生成的代码):

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <sonar.host.url>http://www.example.com/</sonar.host.url>
    <sonar.jdbc.url>jdbc:postgresql://www.example.com/sonar</sonar.jdbc.url>
    <sonar.jdbc.driver>org.postgresql.Driver</sonar.jdbc.driver>
    <sonar.jdbc.username>sonar</sonar.jdbc.username>
    <sonar.jdbc.password>sonar</sonar.jdbc.password>
    <sonar.exclusions>org/binarytherapy/generated/**/*, **/GuiceBindComposer.java</sonar.exclusions>
    <sonar.dynamic>reuseReports</sonar.dynamic>
</properties>

classes/packages mentioned in <sonar.exclusions> excludes the given classes from all static analysis by Sonar, however <sonar.coverage.exclusions> excludes given classes/packages only from coverage, and still be analyzed for other parameters. <sonar.exclusions>提到的 classes/packages 从 Sonar 的所有静态分析中排除给定的类,但是<sonar.coverage.exclusions>仅从覆盖范围中排除给定的类/包,并且仍然会分析其他参数。

<properties>
    <sonar.coverage.exclusions>
        **/domain/**/*,
        **/pojos/*
    </sonar.coverage.exclusions>
</properties>

Reference:参考:

Source:来源:

When doing your Sonar exclusions as shown in the accepted answer, make sure you follow this pattern approach from the SonarQube documentation :在按照接受的答案中所示进行声纳排除时,请确保遵循SonarQube 文档中的这种模式方法:

Relative paths are based on the fully qualified name of the component (like the one displayed below):相对路径基于组件的完全限定名称(如下所示):

src/main/java/org/sonar/batch/phases/AbstractPhaseEvent.java

Examples :例子

# Exclude all classes ending with "Bean"
# Matches org/sonar.api/MyBean.java, org/sonar/util/MyOtherBean.java, etc.
**/*Bean.java

# Exclude all classes in the "src/main/java/org/sonar" directory
# Matches src/main/java/org/sonar/MyClass.java, src/main/java/org/sonar/MyOtherClass.java
# But does not match src/main/java/org/sonar/util/MyClassUtil.java
src/main/java/org/sonar/*

# Exclude all files in the "bank" directory and its sub-directories
# Matches bank/ZTR00021.cbl, bank/data/CBR00354.cbl, bank/data/REM012345.cob
bank/**/*

# Exclude all COBOL programs in the "bank" directory and its sub-directories whose extension is .cbl
# Matches bank/ZTR00021.cbl, bank/data/CBR00354.cbl
bank/**/*.cbl

So if you want to exclude all classes ending with "Bean" and all classes in the "src/main/java/org/sonar" directory (but not in its sub-directories) add the following sonar.exclusions property to the pom's properties :因此,如果要排除以“Bean”结尾的所有类和“src/main/java/org/sonar”目录(但不在其子目录中)中的所有类,请将以下sonar.exclusions属性添加到 pom 的properties

<properties>
  ...
  <sonar.exclusions>**/*Bean.java,src/main/java/org/sonar/*</sonar.exclusions>
</properties>

There are three ways to do.有三种方法可以做到。

1) You can add these files to the properties in your pom.xml: This one is to exclude from code duplication:

<properties>
        <sonar.cpd.exclusions>
            **/dto/**/*,
            **/entity/**/*
        </sonar.cpd.exclusions>
</properties>

2)Using pom.xml We can also define exclusion rules in the pom.xml file using analysis properties.

<properties>
        <sonar.issue.ignore.multicriteria>e1</sonar.issue.ignore.multicriteria>
        <sonar.issue.ignore.multicriteria.e1.ruleKey>
          java:S4784
        </sonar.issue.ignore.multicriteria.e1.ruleKey>
        <sonar.issue.ignore.multicriteria.e1.resourceKey>
          **/commons/**/*
        </sonar.issue.ignore.multicriteria.e1.resourceKey>
</properties>

3) Using sonar-project.properties
We can also define exclusion rules in the sonar-project.properties file using analysis 
properties.Let's define and add the sonar-project.properties file to our resource dir:

sonar.issue.ignore.multicriteria=e1
sonar.issue.ignore.multicriteria.e1.ruleKey=java:S106
sonar.issue.ignore.multicriteria.e1.resourceKey=**/SonarExclude.java

how we can see the rule Key from sonarqube我们如何从 sonarqube 中看到规则 Key

An addition to the answer of @Yatindra Soni (unfortunately I have not yet enough reputation to comment your answer): @Yatindra Soni 的答案的补充(不幸的是,我还没有足够的声誉来评论你的答案):

If you want to exclude multiple sonar rules by pom.xml, you have to specify如果你想通过 pom.xml 排除多个声纳规则,你必须指定

<sonar.issue.ignore.multicriteria>e1,e2,e3</sonar.issue.ignore.multicriteria>

only once and comma separate the values只有一次和逗号分隔值

<properties>
    <sonar.issue.ignore.multicriteria>e1,e2,e3</sonar.issue.ignore.multicriteria>
    
    <!-- rule e1 -->
    <sonar.issue.ignore.multicriteria.e1.ruleKey>
        java:S4784
    </sonar.issue.ignore.multicriteria.e1.ruleKey>
    <sonar.issue.ignore.multicriteria.e1.resourceKey>
        **/commons/**/*
    </sonar.issue.ignore.multicriteria.e1.resourceKey>
    
    <!-- rule e2 -->
    <sonar.issue.ignore.multicriteria.e1.ruleKey>
        java:S2899
    </sonar.issue.ignore.multicriteria.e1.ruleKey>
    <sonar.issue.ignore.multicriteria.e1.resourceKey>
        **/entity/**/*
    </sonar.issue.ignore.multicriteria.e1.resourceKey>
    
    <!-- rule e3 -->
    <sonar.issue.ignore.multicriteria.e1.ruleKey>
        java:S3358
    </sonar.issue.ignore.multicriteria.e1.ruleKey>
    <sonar.issue.ignore.multicriteria.e1.resourceKey>
        **/*.java
    </sonar.issue.ignore.multicriteria.e1.resourceKey>
</properties>

We have to add below code in maven settings.xml file我们必须在 maven settings.xml 文件中添加以下代码

       <profile>
                <id>sonar</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>     
           <properties>
               <sonar.host.url>http://localhost:9000</sonar.host.url>

               <sonar.exclusions>
               # exclude more class under same package
                  src/main/java/co/domain/*,
                  src/main/java/co/util/JsonMapper.java

               # exclude individual class
                  src/main/java/co/util/ProviderCallBuilder.java
               </sonar.exclusions>
            </properties>
       </profile>

I was using sonar to analyse PHP code base.我正在使用声纳来分析PHP代码库。 Both <sonar.exclusions> and <sonar.coverage.exclusions> didn't do the trick. <sonar.exclusions><sonar.coverage.exclusions>没有解决问题。 My solution is - Instead of specifying the exclusions, I ended up specifying the inclusion directories as below:我的解决方案是 - 我没有指定排除项,而是指定如下包含目录:

<properties>
  .........
  <sonar.exclusions>./app/models,./app/controllers</sonar.exclusions>
  .........
</properties>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM