2012-08-27 42 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'。 –

相關問題