我在maven中配置了一个项目,代码分析由SonarQube完成。
我正在尝试在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>
但是,上面的代码不起作用。有没有办法在我的pom.xml文件中配置SonarQube以在分析源代码时忽略这些文件?
提前谢谢。
答案 0 :(得分:50)
POM文件的声纳排除(与其他声纳属性一样)have to be added to the <properties>
section。像这样(例如从当前项目中排除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>
答案 1 :(得分:9)
<sonar.exclusions>
中提到的类/包从Sonar的所有静态分析中排除了给定的类,但<sonar.coverage.exclusions>
仅从coverage中排除给定的类/包,仍然可以分析其他参数。
<properties>
<sonar.coverage.exclusions>
**/domain/**/*,
**/pojos/*
</sonar.coverage.exclusions>
</properties>
参考:
来源:
答案 2 :(得分:4)
按照已接受的答案进行声纳排除时,请确保遵循SonarQube documentation中的这种模式方法:
相对路径基于组件的完全限定名称(如下面显示的名称):
src/main/java/org/sonar/batch/phases/AbstractPhaseEvent.java
示例:
# 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
因此,如果要排除所有以“ Bean”结尾的类以及“ src / main / java / org / sonar”目录(但不在其子目录中)中的所有类,请添加以下sonar.exclusions
pom的properties
的属性:
<properties>
...
<sonar.exclusions>**/*Bean.java,src/main/java/org/sonar/*</sonar.exclusions>
</properties>
答案 3 :(得分:0)
我当时使用声纳来分析 PHP 代码库。
NavHostFragment
和<sonar.exclusions>
都无法解决问题。
我的解决方法是-我没有指定排除对象,而是最终指定了包含目录,如下所示:
<sonar.coverage.exclusions>
答案 4 :(得分:0)
我们必须在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>
答案 5 :(得分:0)
共有三种方法。
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