【问题标题】:Is it possible to only run the JaCoCo maven plugin during the site lifecycle?是否可以在站点生命周期内仅运行 JaCoCo maven 插件?
【发布时间】:2018-05-15 09:24:37
【问题描述】:

考虑以下构建配置:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <argLine>${surefireArgLine}</argLine>
      <parallel>all</parallel>
      <threadCount>2</threadCount>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <configuration>
      <argLine>${failsafeArgLine}</argLine>
      <parallel>classes</parallel>
      <threadCount>2</threadCount>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>pre-unit-test</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/jacoco-ut.exec</destFile>
          <propertyName>surefireArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
        </configuration>
      </execution>
      <execution>
        <id>pre-integration-test</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.build.directory}/jacoco-it.exec</destFile>
          <propertyName>failsafeArgLine</propertyName>
        </configuration>
      </execution>
      <execution>
        <id>post-integration-test</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>report</goal>
        </goals>
        <configuration>
          <dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
        </configuration>
      </execution>
    </executions>
  </plugin>

及报表配置:

  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <reportSets>
      <reportSet>
        <id>JaCoCo-UnitTest</id>
        <reports>
          <report>report</report>
        </reports>
        <configuration>
          <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
        </configuration>
      </reportSet>
      <reportSet>
        <id>JaCoCo-IntegrationTest</id>
        <reports>
          <report>report</report>
        </reports>
        <configuration>
          <dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
        </configuration>
      </reportSet>
    </reportSets>
  </plugin>

很棒的部分是mvn clean site 效果很好,我收到了我的报告。然而,这意味着 JaCoCo 代理在正常生命周期内启动,即:mvn clean install verify

有没有办法在正常生命周期而不是站点周期期间禁用 JaCoCo 代理?

【问题讨论】:

    标签: java maven testing code-coverage jacoco


    【解决方案1】:

    使用属性简单地跳过 JaCoCo 执行:

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco-maven-plugin.version}</version>
        <executions>
            <execution>
                <id>pre-unit-test</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
                <configuration>
                    <!-- Skip this phase if jacoco.skip property is set to true -->
                    <skip>${jacoco.skip}</skip>
                    <destFile>${project.build.directory}/jacoco-ut.exec</destFile>
                    <propertyName>surefireArgLine</propertyName>
                </configuration>
            </execution>
    
            <execution>
                <id>post-unit-test</id>
                <phase>test</phase>
                <goals>
                    <goal>report</goal>
                </goals>
                <configuration>
                    <!-- Skip this phase if jacoco.skip property is set to true -->
                    <skip>${jacoco.skip}</skip>
                    <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    您还可以在跳过测试时禁用 JaCoCo 执行:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven-surefire-plugin.version}</version>
        <configuration>
            <argLine>${surefireArgLine}</argLine>
            <!-- Skip tests if skip.unit.tests property is set to true -->
            <skipTests>${skip.unit.tests}</skipTests>
        </configuration>
    </plugin>
    
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco-maven-plugin.version}</version>
        <executions>
            <execution>
                <id>pre-unit-test</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
                <configuration>
                    <!-- Skip this phase if unit tests are skipped -->
                    <skip>${skip.unit.tests}</skip>
                    <destFile>${project.build.directory}/jacoco-ut.exec</destFile>
                    <propertyName>surefireArgLine</propertyName>
                </configuration>
            </execution>
    
            <execution>
                <id>post-unit-test</id>
                <phase>test</phase>
                <goals>
                    <goal>report</goal>
                </goals>
                <configuration>
                    <!-- Skip this phase if unit tests are skipped -->
                    <skip>${skip.unit.tests}</skip>
                    <dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

    • 我将删除 Surefire 插件中的 &lt;skipTests&gt; 覆盖,并将 &lt;skip&gt;${jacoco.skip}&lt;/skip&gt; 替换为 &lt;skip&gt;${skipTests}&lt;/skip&gt; 以尊重 Maven Surefire 插件的默认 skipTests 属性。请参阅Maven Surefire Plugin > Skipping Tests 了解更多信息。这样mvn clean install -DskipTests 将忽略 JaCoCo 执行目标。
    猜你喜欢
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多