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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21425012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:39:48  来源:igfitidea点击:

Configure Sonar to exclude files from Maven pom.xml

javaxmlmavensonarqube

提问by pappus

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. Those files can be identified by their class names, they contain the underscore character before the extension (they are metamodel classes). Below I give the part of the pom.xml file where I try to exclude them:

我试图在 pom.xml 文件中配置 SonarQube 以从代码分析中排除一些文件。这些文件可以通过它们的类名来标识,它们在扩展名之前包含下划线字符(它们是元模型类)。下面我给出了 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.

先感谢您。

采纳答案by Mikkel L?kke

Sonar exclusions (like other sonar properties) have to be added to the <properties>sectionof the POM file. Like so (example from excluding jOOQ autogenerated code from current project):

声纳排除(如其他声纳属性)必须添加到<properties>POM 文件部分。像这样(例如从当前项目中排除 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>

回答by Amit Kaneria

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>从 Sonar 的所有静态分析中<sonar.coverage.exclusions>排除给定的类,但仅从覆盖范围中排除给定的类/包,并且仍会分析其他参数。

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

Reference:

参考:

Source:

来源:

回答by Voicu

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

回答by Sudheesh.M.S

I was using sonar to analyse PHPcode base. Both <sonar.exclusions>and <sonar.coverage.exclusions>didn't do the trick. My solution is - Instead of specifying the exclusions, I ended up specifying the inclusion directories as below:

我正在使用声纳来分析PHP代码库。双方<sonar.exclusions><sonar.coverage.exclusions>没有做的伎俩。我的解决方案是 - 我没有指定排除项,而是指定如下包含目录:

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

回答by Dhammadip

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>