2012-08-27 84 views
16

我在Maven有一个Sonar配置文件。一切工作正常,除了代码覆盖率指标。我想让Sonar只为代码覆盖率度量忽略一些类。我有以下的个人资料:如何让声纳忽略codeCoverage指标的某些类?

<profile> 
    <id>sonar</id> 
    <properties> 
     <sonar.exclusions>**/beans/jaxb/**</sonar.exclusions> 
    </properties> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>${maven.surefire.plugin.version}</version> 
       <configuration> 
        <redirectTestOutputToFile>true</redirectTestOutputToFile> 
        <excludes> 
         <exclude>**/*Suite*.java</exclude> 
         <exclude>**/*RemoteTest.java</exclude> 
         <exclude>**/*SpringTest.java</exclude> 
         <exclude>**/*CamelTest.java</exclude> 
         <exclude>**/*FunctionalTest.java</exclude> 
         <exclude>**/*IntegrationTest.java</exclude> 
         <exclude>**/*DaoBeanTest.java</exclude> 
        </excludes> 
       </configuration> 
      </plugin>      
     </plugins> 
    </build> 
</profile> 

请帮助。我尝试添加像

<exclude>com/qwerty/dw/publisher/Main.class</exclude> 

,但它并没有帮助

UPDATE

我有一个正确的Cobertura轮廓。我试着将它添加到声纳轮廓,但我仍然有53%,而不是在的Cobertura轮廓

<profile> 
    <id>sonar</id> 
    <properties> 
     <sonar.exclusions>**/beans/jaxb/**</sonar.exclusions> 
     <sonar.core.codeCoveragePlugin>cobertura</sonar.core.codeCoveragePlugin> 
    </properties> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>${maven.surefire.plugin.version}</version> 
       <configuration> 
        <redirectTestOutputToFile>true</redirectTestOutputToFile> 
        <excludes> 
         <exclude>**/*Suite*.java</exclude> 
         <exclude>**/*RemoteTest.java</exclude> 
         <exclude>**/*SpringTest.java</exclude> 
         <exclude>**/*CamelTest.java</exclude> 
         <exclude>**/*FunctionalTest.java</exclude> 
         <exclude>**/*IntegrationTest.java</exclude> 
         <exclude>**/*DaoBeanTest.java</exclude> 
        </excludes> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>cobertura-maven-plugin</artifactId> 
       <version>${cobertura.maven.plugin.version}</version> 
       <configuration> 
        <instrumentation> 
         <excludes> 
          <exclude>com/qwerty/dw/dao/*</exclude> 
          <exclude>com/qwerty/dw/domain/*</exclude> 
          <exclude>com/qwerty/dw/beans/**/*</exclude> 
          <exclude>com/qwerty/dw/daemon/exception/*</exclude> 
          <exclude>com/qwerty/dw/daemon/Main.class</exclude> 
          <exclude>com/qwerty/dw/sink/Main.class</exclude> 
          <exclude>com/qwerty/dw/publisher/Main.class</exclude> 
          <exclude>com/qwerty/dw/publisher/dao/*</exclude> 
          <exclude>com/qwerty/dw/publisher/domain/*</exclude> 
         </excludes> 
        </instrumentation> 
        <formats> 
         <format>html</format> 
        </formats> 
        <aggregate>true</aggregate> 
        <check> 
         <haltOnFailure>true</haltOnFailure> 
         <branchRate>60</branchRate> 
         <lineRate>60</lineRate> 
         <totalBranchRate>60</totalBranchRate> 
         <totalLineRate>60</totalLineRate> 
        </check> 
       </configuration> 
       <executions> 
        <execution> 
         <goals> 
          <goal>clean</goal> 
          <goal>check</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</profile> 
+0

可能是一个悬而未决的问题? http://jira.codehaus.org/browse/SONAR-766 – Jayan

回答

1

我觉得你寻找这个答案Exclude methods from code coverage with Cobertura 记住描述的解决方案就像约95%如果你使用Sonar 3.2,你已经指定你的覆盖工具是cobertura而不是jacoco(默认),它不支持这种功能

+0

我重新提出了问题。如何重用由cobertura生成的报告 –

1

有时候,Clover被配置为提供所有代码覆盖报告非测试代码。如果您希望覆盖这些首选项,您可以使用配置元素从被仪表排除,包括源文件:

<plugin> 
    <groupId>com.atlassian.maven.plugins</groupId> 
    <artifactId>maven-clover2-plugin</artifactId> 
    <version>${clover-version}</version> 
    <configuration> 
     <excludes> 
      <exclude>**/*Dull.java</exclude> 
     </excludes> 
    </configuration> 
</plugin> 

此外,您还可以包括以下声纳配置:

<properties> 
    <sonar.exclusions> 
     **/domain/*.java, 
     **/transfer/*.java 
    </sonar.exclusions> 
</properties> 
11

对我们来说,这工作(主要的pom.xml级属性):

 <properties> 
      <sonar.exclusions>**/Name*.java</sonar.exclusions> 
     </properties> 

根据http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus#NarrowingtheFocus-Patterns看来你可以用“的.java”结尾,或可能“ *”来获得Java类你感兴趣

+2

我也在考虑这一点,但是这排除了每个指标(记录的API,规则遵从性......)中的文件,而不仅仅是测试覆盖率。 – g00glen00b

+0

是的,这就是我们想要的。您可能可以将其添加到像pmd/findbugs或jacoco排除其他方式[?] – rogerdpack

+1

这可能是你想要的,但是问题明确的覆盖:“我想让sonar忽略一些类**仅**代码覆盖率指标“ – Lonzak

3

对于jacoco:使用这个属性:

-Dsonar.jacoco.excludes=**/*View.java 
+2

'sonar.jacoco.excludes'不再支持(自SonarQube 4.5.1开始)。请参阅[这里](http://stackoverflow.com/a/27133765/1005481)了解“新”配置。 –

27

在写这篇文章(这是与SonarQube 4.5.1)的时间,正确的属性设置为sonar.coverage.exclusions,如:

<properties> 
    <sonar.coverage.exclusions>foo/**/*,**/bar/*</sonar.coverage.exclusions> 
</properties> 

这似乎是从几更早版本的变化。请注意,这只会将给定的类从覆盖率计算中排除。所有其他指标和问题都会计算出来。

为了找物业名称为您SonarQube的版本,你可以尝试去你SonarQube实例的常规设置部分,并寻找代码覆盖率项目(在SonarQube 4.5.x,这是一般设置→排除→代码覆盖范围)。在输入字段下方,它提供了上述属性名称(“Key:sonar.coverage.exclusions”)。

+0

对于gradle,它是sonarRunner属性“sonar.coverage.exclusions”,“**/domain/**”,“**/facade/**”,“**/stub/**” ,'**/model/**','**/config/**'“ 属性”sonar.exclusions“,”**/stub/**“ } } –

+0

@Thomas但似乎这样做不用于集成测试。 –

+1

我无法在文档中的任何位置找到'sonar.coverage.exclusions',只提到'sonar.exclusions'。 –