为什么要进行故障保护:不通过“ MVN验证”执行?

时间:2019-02-13 23:49:53

标签: maven maven-failsafe-plugin

为什么不使用mvn verify自动执行“故障安全:集成测试”?

根据mvn help:describe ...maven-failsafe-plugin任务“集成测试”应该绑定到“集成测试”阶段:

failsafe:integration-test
  Description: Run integration tests using Surefire.
  Implementation: org.apache.maven.plugin.failsafe.IntegrationTestMojo
  Language: java
  Bound to phase: integration-test

mvn verify仅执行“ surefire:test”,而不执行“ failsafe:integration-test”。

我确实知道如何使用<executions>来解决此问题,因为即使在故障安全插件主页上的“用法”示例中也是如此,但是我想了解为什么两个插件的行为不同。

有些文件符合两种<include>模式:

src/test/java/de/lathspell/test/UselessThingTest.java
src/test/java/de/lathspell/test/UselessThingIT.java  

我的pom.xml看起来像这样:

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

    <groupId>de.lathspell</groupId>
    <artifactId>test1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

输出:

[INFO] -------------------------< de.lathspell:test1 >-------------------------
[INFO] Building test1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /mnt/keller_christian/workspace/test1/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /mnt/keller_christian/workspace/test1/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ test1 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running de.lathspell.test.UselessThingTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.048 s - in de.lathspell.test.UselessThingTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test1 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:2)

简单的事情是,maven-failsafe-plugin默认情况下不受任何生命周期的约束。

这意味着您必须将自己绑定到项目中一次。

文档说:

  

默认情况下绑定到生命周期阶段:集成测试。

这意味着您必须完全增加生命周期,然后插件将绑定到integration-test生命周期。

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

此后,您只需使用以下命令即可执行集成测试:

mvn clean verify

更新: 如果插件的文档说:

  

默认情况下绑定到生命周期阶段:x

这意味着,如果您添加如上尚未定义<phase>..</phaseY>的执行,则仅将其绑定到生命周期阶段。这是一种默认值,您不会在pom配置中明确给出。