簡體   English   中英

將生成的源從代碼覆蓋范圍中排除,而不會影響整個代碼覆蓋率

[英]Exclude generated-sources from code coverage without impacting overall code-coverage

我有一個項目的代碼覆蓋率非常低。 經過深入研究后,我發現jacoco代碼覆蓋率統計信息取決於目標目錄而不是src。

target/generated-sources/delombok/com/

如果排除目標目錄,覆蓋率將下降0%。 因此,不是應該根據代碼(src)而不是目標文件夾來衡量代碼覆蓋率。 下面是SonarQube的屏幕截圖。

在此處輸入圖片說明

所以問題是我如何確保代碼覆蓋率是針對src而非目標進行衡量的?

以下是沒有依賴項的pom:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>removed</groupId>
    <artifactId>xxxx</artifactId>
    <version>0.0.11-SNAPSHOT</version>
    <packaging>war</packaging>



    <build>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cassandra-maven-plugin</artifactId>
                <version>2.1.7-1</version>
                <configuration>
                    <script>${basedir}/src/main/cassandra/local_run.cql</script>
                    <cqlVersion>3.2.0</cqlVersion>
                    <startNativeTransport>true</startNativeTransport>
                    <nativeTransportPort>9142</nativeTransportPort>
                    <jmxPort>17199</jmxPort>
                    <storagePort>17000</storagePort>
                    <loadFailureIgnore>false</loadFailureIgnore>
                    <cuLoadFailureIgnore>false</cuLoadFailureIgnore>
                </configuration>
                <executions>
                    <execution>
                        <id>start</id>
                        <goals>
                            <goal>start</goal>
                            <goal>load</goal>
                        </goals>
                        <phase>process-classes</phase> <!--Bind cassandra start and load to process-classes which is the phase that is called up before tomcat is run -->
                    </execution>
                    <execution>
                        <id>stop</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                        <!--Bind cassandra stop to generate-test-sources which is the phase that is called up before
                        tests are run. This is because our tests start their own cassandra -->
                        <phase>integration-test</phase>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                        <version>1.7.7</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-maven-plugin</artifactId>
            </plugin>
            <!--Needed to add this so that src/main/java is recognized as a source folder -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>

            <!-- Unit Test -->


            <!-- Integration Test -->


            <plugin>
                <artifactId>maven-enforcer-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warName>activationloginedge</warName>
                </configuration>
            </plugin>

            <!-- Tomcat Integration Test -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <path>/activationloginedge</path>
                </configuration>
                <executions>
                    <execution>
                        <id>tomcat-run</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <systemProperties>
                                <!-- We want test configuration for running integration tests. -->
                                <archaius.deployment.environment>test</archaius.deployment.environment>
                                <logback-lib.env>filesystem</logback-lib.env>
                            </systemProperties>
                            <fork>true</fork>
                        </configuration>
                    </execution>
                    <execution>
                        <id>tomcat-shutdown</id>
                        <goals>
                            <goal>shutdown</goal>
                        </goals>
                        <phase>post-integration-test</phase>
                    </execution>
                </executions>
            </plugin>
        <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.7.4.201502262128</version>
      <executions>
        <execution>
          <id>pre-unit-test</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
          <configuration>
          <!-- Sets the path to the file which contains the execution data. -->
             <destFile>${project.build.directory}/jacoco-ut.exec</destFile>
             <!-- Sets the name of the property containing the settings for JaCoCo runtime agent.-->
             <propertyName>surefireArgLine</propertyName>
          </configuration>
        </execution>
        <!--Ensures that the code coverage report for unit tests is created after unit tests have been run.-->
        <execution>
          <id>post-unit-test</id>
          <phase>test</phase>
          <goals>
            <goal>report</goal>
          </goals>
          <configuration>
          <!-- Sets the path to the file which contains the execution data. -->
          <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
          <!-- Sets the output directory for the code coverage report. -->
          <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
          </configuration>
</execution>        <execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
    <goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
    <destFile>${project.build.directory}/jacoco-it.exec</destFile>
    <!-- Sets the name of the property containing the settings for JaCoCo runtime agent.-->
    <propertyName>failsafeArgLine</propertyName>
 <

    /configuration>
    </execution>
    <!--   Ensures that the code coverage report for integration tests after  integration tests have been run.-->
    <execution>
        <id>post-integration-test</id>
        <phase>post-integration-test</phase>
        <goals>
                <goal>report</goal>
        </goals>
     <configuration>
    <!-- Sets the path to the file which contains the execution data. -->
    <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
     <!-- Sets the output directory for the code coverage report. -->
            <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
        </configuration>
     </execution>
     </executions>
     </plugin>
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>2.12.4</version>
       <configuration>
         <!-- Sets the VM argument line used when unit tests are run. -->
         <argLine>${surefireArgLine}</argLine>
         <!-- Skips unit tests if the value of skip.unit.tests property is true -->
         <skipTests>${skip.unit.tests}</skipTests>
         <!-- Excludes integration tests when unit tests are run. -->
         <excludes>
           <exclude>**/IT*.java</exclude>
         </excludes>
       </configuration>
      </plugin>
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.15</version>      <executions>
            <execution>
                <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
                </goals>
          <configuration>
         <!-- Sets the VM argument line used when integration tests are run-->
             <argLine>${failsafeArgLine}</argLine>
         <!-- Skips integration tests if the value of skip.integration.tests property is true-->
            <skipTests>${skip.integration.tests}</skipTests>      </configuration>
              </execution>
              </executions>
      </plugin>
    </plugins>
        </build>
    </project>

好的,Lombok Maven插件在Maven構建期間會修改源文件夾列表,這就是為什么target/generated-sources/delomboktarget/generated-sources/delombok文件夾識別為源文件夾的原因。

為了強制SonarQube僅考慮您自己的源文件,應將以下屬性添加到POM:

<properties>
  <sonar.sources>src/main/java</sonar.sources>
</properties>

這樣可以解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM