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

标签 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 代理?

最佳答案

使用属性简单地跳过 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>

关于java - 是否可以在站点生命周期内只运行 JaCoCo maven 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50346646/

相关文章:

java - 在 aws 服务器上启动 tomcat 8 的问题

java - 如何为 Maven 安装的 jar 编写 POM 文件?

Java Google Calendar API 按降序排列事件

java - 当项目有不同的 Java 编译器版本时会出现任何问题吗?

testing - 突变测试工具

python - 如何自动执行 Windows 驱动程序和二进制文件的防病毒/WSUS 补丁测试?

java - 反序列化时使用父对象的属性来确定子类?

Eclipse 不显示包中的类

java - IntelliJ IDEA : support both SBT and Maven on a single project

ruby-on-rails - 测试属于 Rails 中帖子的评论的 Controller