是否可以在站点生命周期内仅运行JaCoCo maven插件?

时间:2018-05-15 09:24:38

标签: java maven testing code-coverage jacoco

考虑以下构建配置:

  <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代理,而不是在站点周期中?

1 个答案:

答案 0 :(得分: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>